test_postgresql.py 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks.data_source import postgresql as module
  5. def test_make_extra_environment_maps_options_to_environment():
  6. database = {
  7. 'name': 'foo',
  8. 'password': 'pass',
  9. 'ssl_mode': 'require',
  10. 'ssl_cert': 'cert.crt',
  11. 'ssl_key': 'key.key',
  12. 'ssl_root_cert': 'root.crt',
  13. 'ssl_crl': 'crl.crl',
  14. }
  15. expected = {
  16. 'PGPASSWORD': 'pass',
  17. 'PGSSLMODE': 'require',
  18. 'PGSSLCERT': 'cert.crt',
  19. 'PGSSLKEY': 'key.key',
  20. 'PGSSLROOTCERT': 'root.crt',
  21. 'PGSSLCRL': 'crl.crl',
  22. }
  23. extra_env = module.make_extra_environment(database)
  24. assert extra_env == expected
  25. def test_make_extra_environment_with_cli_password_sets_correct_password():
  26. database = {'name': 'foo', 'restore_password': 'trustsome1', 'password': 'anotherpassword'}
  27. extra = module.make_extra_environment(
  28. database, restore_connection_params={'password': 'clipassword'}
  29. )
  30. assert extra['PGPASSWORD'] == 'clipassword'
  31. def test_make_extra_environment_without_cli_password_or_configured_password_does_not_set_password():
  32. database = {'name': 'foo'}
  33. extra = module.make_extra_environment(
  34. database, restore_connection_params={'username': 'someone'}
  35. )
  36. assert 'PGPASSWORD' not in extra
  37. def test_make_extra_environment_without_ssl_mode_does_not_set_ssl_mode():
  38. database = {'name': 'foo'}
  39. extra = module.make_extra_environment(database)
  40. assert 'PGSSLMODE' not in extra
  41. def test_database_names_to_dump_passes_through_individual_database_name():
  42. database = {'name': 'foo'}
  43. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  44. 'foo',
  45. )
  46. def test_database_names_to_dump_passes_through_individual_database_name_with_format():
  47. database = {'name': 'foo', 'format': 'custom'}
  48. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  49. 'foo',
  50. )
  51. def test_database_names_to_dump_passes_through_all_without_format():
  52. database = {'name': 'all'}
  53. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  54. 'all',
  55. )
  56. def test_database_names_to_dump_with_all_and_format_and_dry_run_bails():
  57. database = {'name': 'all', 'format': 'custom'}
  58. flexmock(module).should_receive('execute_command_and_capture_output').never()
  59. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=True) == ()
  60. def test_database_names_to_dump_with_all_and_format_lists_databases():
  61. database = {'name': 'all', 'format': 'custom'}
  62. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  63. 'foo,test,\nbar,test,"stuff and such"'
  64. )
  65. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  66. 'foo',
  67. 'bar',
  68. )
  69. def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostname_and_port():
  70. database = {'name': 'all', 'format': 'custom', 'hostname': 'localhost', 'port': 1234}
  71. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  72. (
  73. 'psql',
  74. '--list',
  75. '--no-password',
  76. '--no-psqlrc',
  77. '--csv',
  78. '--tuples-only',
  79. '--host',
  80. 'localhost',
  81. '--port',
  82. '1234',
  83. ),
  84. extra_environment=object,
  85. ).and_return('foo,test,\nbar,test,"stuff and such"')
  86. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  87. 'foo',
  88. 'bar',
  89. )
  90. def test_database_names_to_dump_with_all_and_format_lists_databases_with_username():
  91. database = {'name': 'all', 'format': 'custom', 'username': 'postgres'}
  92. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  93. (
  94. 'psql',
  95. '--list',
  96. '--no-password',
  97. '--no-psqlrc',
  98. '--csv',
  99. '--tuples-only',
  100. '--username',
  101. 'postgres',
  102. ),
  103. extra_environment=object,
  104. ).and_return('foo,test,\nbar,test,"stuff and such"')
  105. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  106. 'foo',
  107. 'bar',
  108. )
  109. def test_database_names_to_dump_with_all_and_format_lists_databases_with_options():
  110. database = {'name': 'all', 'format': 'custom', 'list_options': '--harder'}
  111. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  112. ('psql', '--list', '--no-password', '--no-psqlrc', '--csv', '--tuples-only', '--harder'),
  113. extra_environment=object,
  114. ).and_return('foo,test,\nbar,test,"stuff and such"')
  115. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  116. 'foo',
  117. 'bar',
  118. )
  119. def test_database_names_to_dump_with_all_and_format_excludes_particular_databases():
  120. database = {'name': 'all', 'format': 'custom'}
  121. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  122. 'foo,test,\ntemplate0,test,blah'
  123. )
  124. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  125. 'foo',
  126. )
  127. def test_database_names_to_dump_with_all_and_psql_command_uses_custom_command():
  128. database = {
  129. 'name': 'all',
  130. 'format': 'custom',
  131. 'psql_command': 'docker exec --workdir * mycontainer psql',
  132. }
  133. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  134. (
  135. 'docker',
  136. 'exec',
  137. '--workdir',
  138. "'*'", # Should get shell escaped to prevent injection attacks.
  139. 'mycontainer',
  140. 'psql',
  141. '--list',
  142. '--no-password',
  143. '--no-psqlrc',
  144. '--csv',
  145. '--tuples-only',
  146. ),
  147. extra_environment=object,
  148. ).and_return('foo,text').once()
  149. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  150. 'foo',
  151. )
  152. def test_use_streaming_true_for_any_non_directory_format_databases():
  153. assert module.use_streaming(
  154. databases=[{'format': 'stuff'}, {'format': 'directory'}, {}],
  155. config=flexmock(),
  156. )
  157. def test_use_streaming_false_for_all_directory_format_databases():
  158. assert not module.use_streaming(
  159. databases=[{'format': 'directory'}, {'format': 'directory'}],
  160. config=flexmock(),
  161. )
  162. def test_use_streaming_false_for_no_databases():
  163. assert not module.use_streaming(databases=[], config=flexmock())
  164. def test_dump_data_sources_runs_pg_dump_for_each_database():
  165. databases = [{'name': 'foo'}, {'name': 'bar'}]
  166. processes = [flexmock(), flexmock()]
  167. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  168. flexmock(module).should_receive('make_dump_path').and_return('')
  169. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  170. ('bar',)
  171. )
  172. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  173. 'databases/localhost/foo'
  174. ).and_return('databases/localhost/bar')
  175. flexmock(module.os.path).should_receive('exists').and_return(False)
  176. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  177. for name, process in zip(('foo', 'bar'), processes):
  178. flexmock(module).should_receive('execute_command').with_args(
  179. (
  180. 'pg_dump',
  181. '--no-password',
  182. '--clean',
  183. '--if-exists',
  184. '--format',
  185. 'custom',
  186. name,
  187. '>',
  188. f'databases/localhost/{name}',
  189. ),
  190. shell=True,
  191. extra_environment={'PGSSLMODE': 'disable'},
  192. run_to_completion=False,
  193. ).and_return(process).once()
  194. assert (
  195. module.dump_data_sources(
  196. databases,
  197. {},
  198. 'test.yaml',
  199. config_paths=('test.yaml',),
  200. borgmatic_runtime_directory='/run/borgmatic',
  201. patterns=[],
  202. dry_run=False,
  203. )
  204. == processes
  205. )
  206. def test_dump_data_sources_raises_when_no_database_names_to_dump():
  207. databases = [{'name': 'foo'}, {'name': 'bar'}]
  208. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  209. flexmock(module).should_receive('make_dump_path').and_return('')
  210. flexmock(module).should_receive('database_names_to_dump').and_return(())
  211. with pytest.raises(ValueError):
  212. module.dump_data_sources(
  213. databases,
  214. {},
  215. 'test.yaml',
  216. config_paths=('test.yaml',),
  217. borgmatic_runtime_directory='/run/borgmatic',
  218. patterns=[],
  219. dry_run=False,
  220. )
  221. def test_dump_data_sources_does_not_raise_when_no_database_names_to_dump():
  222. databases = [{'name': 'foo'}, {'name': 'bar'}]
  223. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  224. flexmock(module).should_receive('make_dump_path').and_return('')
  225. flexmock(module).should_receive('database_names_to_dump').and_return(())
  226. module.dump_data_sources(
  227. databases,
  228. {},
  229. 'test.yaml',
  230. config_paths=('test.yaml',),
  231. borgmatic_runtime_directory='/run/borgmatic',
  232. patterns=[],
  233. dry_run=True,
  234. ) == []
  235. def test_dump_data_sources_with_duplicate_dump_skips_pg_dump():
  236. databases = [{'name': 'foo'}, {'name': 'bar'}]
  237. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  238. flexmock(module).should_receive('make_dump_path').and_return('')
  239. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  240. ('bar',)
  241. )
  242. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  243. 'databases/localhost/foo'
  244. ).and_return('databases/localhost/bar')
  245. flexmock(module.os.path).should_receive('exists').and_return(True)
  246. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  247. flexmock(module).should_receive('execute_command').never()
  248. assert (
  249. module.dump_data_sources(
  250. databases,
  251. {},
  252. 'test.yaml',
  253. config_paths=('test.yaml',),
  254. borgmatic_runtime_directory='/run/borgmatic',
  255. patterns=[],
  256. dry_run=False,
  257. )
  258. == []
  259. )
  260. def test_dump_data_sources_with_dry_run_skips_pg_dump():
  261. databases = [{'name': 'foo'}, {'name': 'bar'}]
  262. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  263. flexmock(module).should_receive('make_dump_path').and_return('')
  264. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  265. ('bar',)
  266. )
  267. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  268. 'databases/localhost/foo'
  269. ).and_return('databases/localhost/bar')
  270. flexmock(module.os.path).should_receive('exists').and_return(False)
  271. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  272. flexmock(module).should_receive('execute_command').never()
  273. assert (
  274. module.dump_data_sources(
  275. databases,
  276. {},
  277. 'test.yaml',
  278. config_paths=('test.yaml',),
  279. borgmatic_runtime_directory='/run/borgmatic',
  280. patterns=[],
  281. dry_run=True,
  282. )
  283. == []
  284. )
  285. def test_dump_data_sources_runs_pg_dump_with_hostname_and_port():
  286. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  287. process = flexmock()
  288. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  289. flexmock(module).should_receive('make_dump_path').and_return('')
  290. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  291. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  292. 'databases/database.example.org/foo'
  293. )
  294. flexmock(module.os.path).should_receive('exists').and_return(False)
  295. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  296. flexmock(module).should_receive('execute_command').with_args(
  297. (
  298. 'pg_dump',
  299. '--no-password',
  300. '--clean',
  301. '--if-exists',
  302. '--host',
  303. 'database.example.org',
  304. '--port',
  305. '5433',
  306. '--format',
  307. 'custom',
  308. 'foo',
  309. '>',
  310. 'databases/database.example.org/foo',
  311. ),
  312. shell=True,
  313. extra_environment={'PGSSLMODE': 'disable'},
  314. run_to_completion=False,
  315. ).and_return(process).once()
  316. assert module.dump_data_sources(
  317. databases,
  318. {},
  319. 'test.yaml',
  320. config_paths=('test.yaml',),
  321. borgmatic_runtime_directory='/run/borgmatic',
  322. patterns=[],
  323. dry_run=False,
  324. ) == [process]
  325. def test_dump_data_sources_runs_pg_dump_with_username_and_password():
  326. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  327. process = flexmock()
  328. flexmock(module).should_receive('make_extra_environment').and_return(
  329. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  330. )
  331. flexmock(module).should_receive('make_dump_path').and_return('')
  332. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  333. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  334. 'databases/localhost/foo'
  335. )
  336. flexmock(module.os.path).should_receive('exists').and_return(False)
  337. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  338. flexmock(module).should_receive('execute_command').with_args(
  339. (
  340. 'pg_dump',
  341. '--no-password',
  342. '--clean',
  343. '--if-exists',
  344. '--username',
  345. 'postgres',
  346. '--format',
  347. 'custom',
  348. 'foo',
  349. '>',
  350. 'databases/localhost/foo',
  351. ),
  352. shell=True,
  353. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  354. run_to_completion=False,
  355. ).and_return(process).once()
  356. assert module.dump_data_sources(
  357. databases,
  358. {},
  359. 'test.yaml',
  360. config_paths=('test.yaml',),
  361. borgmatic_runtime_directory='/run/borgmatic',
  362. patterns=[],
  363. dry_run=False,
  364. ) == [process]
  365. def test_dump_data_sources_with_username_injection_attack_gets_escaped():
  366. databases = [{'name': 'foo', 'username': 'postgres; naughty-command', 'password': 'trustsome1'}]
  367. process = flexmock()
  368. flexmock(module).should_receive('make_extra_environment').and_return(
  369. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  370. )
  371. flexmock(module).should_receive('make_dump_path').and_return('')
  372. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  373. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  374. 'databases/localhost/foo'
  375. )
  376. flexmock(module.os.path).should_receive('exists').and_return(False)
  377. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  378. flexmock(module).should_receive('execute_command').with_args(
  379. (
  380. 'pg_dump',
  381. '--no-password',
  382. '--clean',
  383. '--if-exists',
  384. '--username',
  385. "'postgres; naughty-command'",
  386. '--format',
  387. 'custom',
  388. 'foo',
  389. '>',
  390. 'databases/localhost/foo',
  391. ),
  392. shell=True,
  393. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  394. run_to_completion=False,
  395. ).and_return(process).once()
  396. assert module.dump_data_sources(
  397. databases,
  398. {},
  399. 'test.yaml',
  400. config_paths=('test.yaml',),
  401. borgmatic_runtime_directory='/run/borgmatic',
  402. patterns=[],
  403. dry_run=False,
  404. ) == [process]
  405. def test_dump_data_sources_runs_pg_dump_with_directory_format():
  406. databases = [{'name': 'foo', 'format': 'directory'}]
  407. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  408. flexmock(module).should_receive('make_dump_path').and_return('')
  409. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  410. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  411. 'databases/localhost/foo'
  412. )
  413. flexmock(module.os.path).should_receive('exists').and_return(False)
  414. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  415. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  416. flexmock(module).should_receive('execute_command').with_args(
  417. (
  418. 'pg_dump',
  419. '--no-password',
  420. '--clean',
  421. '--if-exists',
  422. '--format',
  423. 'directory',
  424. '--file',
  425. 'databases/localhost/foo',
  426. 'foo',
  427. ),
  428. shell=True,
  429. extra_environment={'PGSSLMODE': 'disable'},
  430. ).and_return(flexmock()).once()
  431. assert (
  432. module.dump_data_sources(
  433. databases,
  434. {},
  435. 'test.yaml',
  436. config_paths=('test.yaml',),
  437. borgmatic_runtime_directory='/run/borgmatic',
  438. patterns=[],
  439. dry_run=False,
  440. )
  441. == []
  442. )
  443. def test_dump_data_sources_runs_pg_dump_with_options():
  444. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  445. process = flexmock()
  446. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  447. flexmock(module).should_receive('make_dump_path').and_return('')
  448. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  449. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  450. 'databases/localhost/foo'
  451. )
  452. flexmock(module.os.path).should_receive('exists').and_return(False)
  453. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  454. flexmock(module).should_receive('execute_command').with_args(
  455. (
  456. 'pg_dump',
  457. '--no-password',
  458. '--clean',
  459. '--if-exists',
  460. '--format',
  461. 'custom',
  462. '--stuff=such',
  463. 'foo',
  464. '>',
  465. 'databases/localhost/foo',
  466. ),
  467. shell=True,
  468. extra_environment={'PGSSLMODE': 'disable'},
  469. run_to_completion=False,
  470. ).and_return(process).once()
  471. assert module.dump_data_sources(
  472. databases,
  473. {},
  474. 'test.yaml',
  475. config_paths=('test.yaml',),
  476. borgmatic_runtime_directory='/run/borgmatic',
  477. patterns=[],
  478. dry_run=False,
  479. ) == [process]
  480. def test_dump_data_sources_runs_pg_dumpall_for_all_databases():
  481. databases = [{'name': 'all'}]
  482. process = flexmock()
  483. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  484. flexmock(module).should_receive('make_dump_path').and_return('')
  485. flexmock(module).should_receive('database_names_to_dump').and_return(('all',))
  486. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  487. 'databases/localhost/all'
  488. )
  489. flexmock(module.os.path).should_receive('exists').and_return(False)
  490. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  491. flexmock(module).should_receive('execute_command').with_args(
  492. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  493. shell=True,
  494. extra_environment={'PGSSLMODE': 'disable'},
  495. run_to_completion=False,
  496. ).and_return(process).once()
  497. assert module.dump_data_sources(
  498. databases,
  499. {},
  500. 'test.yaml',
  501. config_paths=('test.yaml',),
  502. borgmatic_runtime_directory='/run/borgmatic',
  503. patterns=[],
  504. dry_run=False,
  505. ) == [process]
  506. def test_dump_data_sources_runs_non_default_pg_dump():
  507. databases = [{'name': 'foo', 'pg_dump_command': 'special_pg_dump --compress *'}]
  508. process = flexmock()
  509. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  510. flexmock(module).should_receive('make_dump_path').and_return('')
  511. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  512. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  513. 'databases/localhost/foo'
  514. )
  515. flexmock(module.os.path).should_receive('exists').and_return(False)
  516. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  517. flexmock(module).should_receive('execute_command').with_args(
  518. (
  519. 'special_pg_dump',
  520. '--compress',
  521. "'*'", # Should get shell escaped to prevent injection attacks.
  522. '--no-password',
  523. '--clean',
  524. '--if-exists',
  525. '--format',
  526. 'custom',
  527. 'foo',
  528. '>',
  529. 'databases/localhost/foo',
  530. ),
  531. shell=True,
  532. extra_environment={'PGSSLMODE': 'disable'},
  533. run_to_completion=False,
  534. ).and_return(process).once()
  535. assert module.dump_data_sources(
  536. databases,
  537. {},
  538. 'test.yaml',
  539. config_paths=('test.yaml',),
  540. borgmatic_runtime_directory='/run/borgmatic',
  541. patterns=[],
  542. dry_run=False,
  543. ) == [process]
  544. def test_restore_data_source_dump_runs_pg_restore():
  545. hook_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
  546. extract_process = flexmock(stdout=flexmock())
  547. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  548. flexmock(module).should_receive('make_dump_path')
  549. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  550. flexmock(module).should_receive('execute_command_with_processes').with_args(
  551. (
  552. 'pg_restore',
  553. '--no-password',
  554. '--if-exists',
  555. '--exit-on-error',
  556. '--clean',
  557. '--dbname',
  558. 'foo',
  559. ),
  560. processes=[extract_process],
  561. output_log_level=logging.DEBUG,
  562. input_file=extract_process.stdout,
  563. extra_environment={'PGSSLMODE': 'disable'},
  564. ).once()
  565. flexmock(module).should_receive('execute_command').with_args(
  566. (
  567. 'psql',
  568. '--no-password',
  569. '--no-psqlrc',
  570. '--quiet',
  571. '--dbname',
  572. 'foo',
  573. '--command',
  574. 'ANALYZE',
  575. ),
  576. extra_environment={'PGSSLMODE': 'disable'},
  577. ).once()
  578. module.restore_data_source_dump(
  579. hook_config,
  580. {},
  581. 'test.yaml',
  582. data_source={'name': 'foo'},
  583. dry_run=False,
  584. extract_process=extract_process,
  585. connection_params={
  586. 'hostname': None,
  587. 'port': None,
  588. 'username': None,
  589. 'password': None,
  590. },
  591. borgmatic_runtime_directory='/run/borgmatic',
  592. )
  593. def test_restore_data_source_dump_runs_pg_restore_with_hostname_and_port():
  594. hook_config = [
  595. {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
  596. ]
  597. extract_process = flexmock(stdout=flexmock())
  598. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  599. flexmock(module).should_receive('make_dump_path')
  600. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  601. flexmock(module).should_receive('execute_command_with_processes').with_args(
  602. (
  603. 'pg_restore',
  604. '--no-password',
  605. '--if-exists',
  606. '--exit-on-error',
  607. '--clean',
  608. '--dbname',
  609. 'foo',
  610. '--host',
  611. 'database.example.org',
  612. '--port',
  613. '5433',
  614. ),
  615. processes=[extract_process],
  616. output_log_level=logging.DEBUG,
  617. input_file=extract_process.stdout,
  618. extra_environment={'PGSSLMODE': 'disable'},
  619. ).once()
  620. flexmock(module).should_receive('execute_command').with_args(
  621. (
  622. 'psql',
  623. '--no-password',
  624. '--no-psqlrc',
  625. '--quiet',
  626. '--host',
  627. 'database.example.org',
  628. '--port',
  629. '5433',
  630. '--dbname',
  631. 'foo',
  632. '--command',
  633. 'ANALYZE',
  634. ),
  635. extra_environment={'PGSSLMODE': 'disable'},
  636. ).once()
  637. module.restore_data_source_dump(
  638. hook_config,
  639. {},
  640. 'test.yaml',
  641. data_source=hook_config[0],
  642. dry_run=False,
  643. extract_process=extract_process,
  644. connection_params={
  645. 'hostname': None,
  646. 'port': None,
  647. 'username': None,
  648. 'password': None,
  649. },
  650. borgmatic_runtime_directory='/run/borgmatic',
  651. )
  652. def test_restore_data_source_dump_runs_pg_restore_with_username_and_password():
  653. hook_config = [
  654. {'name': 'foo', 'username': 'postgres', 'password': 'trustsome1', 'schemas': None}
  655. ]
  656. extract_process = flexmock(stdout=flexmock())
  657. flexmock(module).should_receive('make_extra_environment').and_return(
  658. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  659. )
  660. flexmock(module).should_receive('make_dump_path')
  661. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  662. flexmock(module).should_receive('execute_command_with_processes').with_args(
  663. (
  664. 'pg_restore',
  665. '--no-password',
  666. '--if-exists',
  667. '--exit-on-error',
  668. '--clean',
  669. '--dbname',
  670. 'foo',
  671. '--username',
  672. 'postgres',
  673. ),
  674. processes=[extract_process],
  675. output_log_level=logging.DEBUG,
  676. input_file=extract_process.stdout,
  677. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  678. ).once()
  679. flexmock(module).should_receive('execute_command').with_args(
  680. (
  681. 'psql',
  682. '--no-password',
  683. '--no-psqlrc',
  684. '--quiet',
  685. '--username',
  686. 'postgres',
  687. '--dbname',
  688. 'foo',
  689. '--command',
  690. 'ANALYZE',
  691. ),
  692. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  693. ).once()
  694. module.restore_data_source_dump(
  695. hook_config,
  696. {},
  697. 'test.yaml',
  698. data_source=hook_config[0],
  699. dry_run=False,
  700. extract_process=extract_process,
  701. connection_params={
  702. 'hostname': None,
  703. 'port': None,
  704. 'username': None,
  705. 'password': None,
  706. },
  707. borgmatic_runtime_directory='/run/borgmatic',
  708. )
  709. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  710. hook_config = [
  711. {
  712. 'name': 'foo',
  713. 'hostname': 'database.example.org',
  714. 'port': 5433,
  715. 'username': 'postgres',
  716. 'password': 'trustsome1',
  717. 'restore_hostname': 'restorehost',
  718. 'restore_port': 'restoreport',
  719. 'restore_username': 'restoreusername',
  720. 'restore_password': 'restorepassword',
  721. 'schemas': None,
  722. }
  723. ]
  724. extract_process = flexmock(stdout=flexmock())
  725. flexmock(module).should_receive('make_extra_environment').and_return(
  726. {'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'}
  727. )
  728. flexmock(module).should_receive('make_dump_path')
  729. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  730. flexmock(module).should_receive('execute_command_with_processes').with_args(
  731. (
  732. 'pg_restore',
  733. '--no-password',
  734. '--if-exists',
  735. '--exit-on-error',
  736. '--clean',
  737. '--dbname',
  738. 'foo',
  739. '--host',
  740. 'clihost',
  741. '--port',
  742. 'cliport',
  743. '--username',
  744. 'cliusername',
  745. ),
  746. processes=[extract_process],
  747. output_log_level=logging.DEBUG,
  748. input_file=extract_process.stdout,
  749. extra_environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  750. ).once()
  751. flexmock(module).should_receive('execute_command').with_args(
  752. (
  753. 'psql',
  754. '--no-password',
  755. '--no-psqlrc',
  756. '--quiet',
  757. '--host',
  758. 'clihost',
  759. '--port',
  760. 'cliport',
  761. '--username',
  762. 'cliusername',
  763. '--dbname',
  764. 'foo',
  765. '--command',
  766. 'ANALYZE',
  767. ),
  768. extra_environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  769. ).once()
  770. module.restore_data_source_dump(
  771. hook_config,
  772. {},
  773. 'test.yaml',
  774. data_source={'name': 'foo'},
  775. dry_run=False,
  776. extract_process=extract_process,
  777. connection_params={
  778. 'hostname': 'clihost',
  779. 'port': 'cliport',
  780. 'username': 'cliusername',
  781. 'password': 'clipassword',
  782. },
  783. borgmatic_runtime_directory='/run/borgmatic',
  784. )
  785. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  786. hook_config = [
  787. {
  788. 'name': 'foo',
  789. 'hostname': 'database.example.org',
  790. 'port': 5433,
  791. 'username': 'postgres',
  792. 'password': 'trustsome1',
  793. 'schemas': None,
  794. 'restore_hostname': 'restorehost',
  795. 'restore_port': 'restoreport',
  796. 'restore_username': 'restoreusername',
  797. 'restore_password': 'restorepassword',
  798. }
  799. ]
  800. extract_process = flexmock(stdout=flexmock())
  801. flexmock(module).should_receive('make_extra_environment').and_return(
  802. {'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'}
  803. )
  804. flexmock(module).should_receive('make_dump_path')
  805. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  806. flexmock(module).should_receive('execute_command_with_processes').with_args(
  807. (
  808. 'pg_restore',
  809. '--no-password',
  810. '--if-exists',
  811. '--exit-on-error',
  812. '--clean',
  813. '--dbname',
  814. 'foo',
  815. '--host',
  816. 'restorehost',
  817. '--port',
  818. 'restoreport',
  819. '--username',
  820. 'restoreusername',
  821. ),
  822. processes=[extract_process],
  823. output_log_level=logging.DEBUG,
  824. input_file=extract_process.stdout,
  825. extra_environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  826. ).once()
  827. flexmock(module).should_receive('execute_command').with_args(
  828. (
  829. 'psql',
  830. '--no-password',
  831. '--no-psqlrc',
  832. '--quiet',
  833. '--host',
  834. 'restorehost',
  835. '--port',
  836. 'restoreport',
  837. '--username',
  838. 'restoreusername',
  839. '--dbname',
  840. 'foo',
  841. '--command',
  842. 'ANALYZE',
  843. ),
  844. extra_environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  845. ).once()
  846. module.restore_data_source_dump(
  847. hook_config,
  848. {},
  849. 'test.yaml',
  850. data_source=hook_config[0],
  851. dry_run=False,
  852. extract_process=extract_process,
  853. connection_params={
  854. 'hostname': None,
  855. 'port': None,
  856. 'username': None,
  857. 'password': None,
  858. },
  859. borgmatic_runtime_directory='/run/borgmatic',
  860. )
  861. def test_restore_data_source_dump_runs_pg_restore_with_options():
  862. hook_config = [
  863. {
  864. 'name': 'foo',
  865. 'restore_options': '--harder',
  866. 'analyze_options': '--smarter',
  867. 'schemas': None,
  868. }
  869. ]
  870. extract_process = flexmock(stdout=flexmock())
  871. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  872. flexmock(module).should_receive('make_dump_path')
  873. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  874. flexmock(module).should_receive('execute_command_with_processes').with_args(
  875. (
  876. 'pg_restore',
  877. '--no-password',
  878. '--if-exists',
  879. '--exit-on-error',
  880. '--clean',
  881. '--dbname',
  882. 'foo',
  883. '--harder',
  884. ),
  885. processes=[extract_process],
  886. output_log_level=logging.DEBUG,
  887. input_file=extract_process.stdout,
  888. extra_environment={'PGSSLMODE': 'disable'},
  889. ).once()
  890. flexmock(module).should_receive('execute_command').with_args(
  891. (
  892. 'psql',
  893. '--no-password',
  894. '--no-psqlrc',
  895. '--quiet',
  896. '--dbname',
  897. 'foo',
  898. '--smarter',
  899. '--command',
  900. 'ANALYZE',
  901. ),
  902. extra_environment={'PGSSLMODE': 'disable'},
  903. ).once()
  904. module.restore_data_source_dump(
  905. hook_config,
  906. {},
  907. 'test.yaml',
  908. data_source=hook_config[0],
  909. dry_run=False,
  910. extract_process=extract_process,
  911. connection_params={
  912. 'hostname': None,
  913. 'port': None,
  914. 'username': None,
  915. 'password': None,
  916. },
  917. borgmatic_runtime_directory='/run/borgmatic',
  918. )
  919. def test_restore_data_source_dump_runs_psql_for_all_database_dump():
  920. hook_config = [{'name': 'all', 'schemas': None}]
  921. extract_process = flexmock(stdout=flexmock())
  922. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  923. flexmock(module).should_receive('make_dump_path')
  924. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  925. flexmock(module).should_receive('execute_command_with_processes').with_args(
  926. (
  927. 'psql',
  928. '--no-password',
  929. '--no-psqlrc',
  930. ),
  931. processes=[extract_process],
  932. output_log_level=logging.DEBUG,
  933. input_file=extract_process.stdout,
  934. extra_environment={'PGSSLMODE': 'disable'},
  935. ).once()
  936. flexmock(module).should_receive('execute_command').with_args(
  937. ('psql', '--no-password', '--no-psqlrc', '--quiet', '--command', 'ANALYZE'),
  938. extra_environment={'PGSSLMODE': 'disable'},
  939. ).once()
  940. module.restore_data_source_dump(
  941. hook_config,
  942. {},
  943. 'test.yaml',
  944. data_source={'name': 'all'},
  945. dry_run=False,
  946. extract_process=extract_process,
  947. connection_params={
  948. 'hostname': None,
  949. 'port': None,
  950. 'username': None,
  951. 'password': None,
  952. },
  953. borgmatic_runtime_directory='/run/borgmatic',
  954. )
  955. def test_restore_data_source_dump_runs_psql_for_plain_database_dump():
  956. hook_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}]
  957. extract_process = flexmock(stdout=flexmock())
  958. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  959. flexmock(module).should_receive('make_dump_path')
  960. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  961. flexmock(module).should_receive('execute_command_with_processes').with_args(
  962. ('psql', '--no-password', '--no-psqlrc', '--dbname', 'foo'),
  963. processes=[extract_process],
  964. output_log_level=logging.DEBUG,
  965. input_file=extract_process.stdout,
  966. extra_environment={'PGSSLMODE': 'disable'},
  967. ).once()
  968. flexmock(module).should_receive('execute_command').with_args(
  969. (
  970. 'psql',
  971. '--no-password',
  972. '--no-psqlrc',
  973. '--quiet',
  974. '--dbname',
  975. 'foo',
  976. '--command',
  977. 'ANALYZE',
  978. ),
  979. extra_environment={'PGSSLMODE': 'disable'},
  980. ).once()
  981. module.restore_data_source_dump(
  982. hook_config,
  983. {},
  984. 'test.yaml',
  985. data_source=hook_config[0],
  986. dry_run=False,
  987. extract_process=extract_process,
  988. connection_params={
  989. 'hostname': None,
  990. 'port': None,
  991. 'username': None,
  992. 'password': None,
  993. },
  994. borgmatic_runtime_directory='/run/borgmatic',
  995. )
  996. def test_restore_data_source_dump_runs_non_default_pg_restore_and_psql():
  997. hook_config = [
  998. {
  999. 'name': 'foo',
  1000. 'pg_restore_command': 'docker exec --workdir * mycontainer pg_restore',
  1001. 'psql_command': 'docker exec --workdir * mycontainer psql',
  1002. 'schemas': None,
  1003. }
  1004. ]
  1005. extract_process = flexmock(stdout=flexmock())
  1006. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  1007. flexmock(module).should_receive('make_dump_path')
  1008. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  1009. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1010. (
  1011. 'docker',
  1012. 'exec',
  1013. '--workdir',
  1014. "'*'", # Should get shell escaped to prevent injection attacks.
  1015. 'mycontainer',
  1016. 'pg_restore',
  1017. '--no-password',
  1018. '--if-exists',
  1019. '--exit-on-error',
  1020. '--clean',
  1021. '--dbname',
  1022. 'foo',
  1023. ),
  1024. processes=[extract_process],
  1025. output_log_level=logging.DEBUG,
  1026. input_file=extract_process.stdout,
  1027. extra_environment={'PGSSLMODE': 'disable'},
  1028. ).once()
  1029. flexmock(module).should_receive('execute_command').with_args(
  1030. (
  1031. 'docker',
  1032. 'exec',
  1033. '--workdir',
  1034. "'*'", # Should get shell escaped to prevent injection attacks.
  1035. 'mycontainer',
  1036. 'psql',
  1037. '--no-password',
  1038. '--no-psqlrc',
  1039. '--quiet',
  1040. '--dbname',
  1041. 'foo',
  1042. '--command',
  1043. 'ANALYZE',
  1044. ),
  1045. extra_environment={'PGSSLMODE': 'disable'},
  1046. ).once()
  1047. module.restore_data_source_dump(
  1048. hook_config,
  1049. {},
  1050. 'test.yaml',
  1051. data_source=hook_config[0],
  1052. dry_run=False,
  1053. extract_process=extract_process,
  1054. connection_params={
  1055. 'hostname': None,
  1056. 'port': None,
  1057. 'username': None,
  1058. 'password': None,
  1059. },
  1060. borgmatic_runtime_directory='/run/borgmatic',
  1061. )
  1062. def test_restore_data_source_dump_with_dry_run_skips_restore():
  1063. hook_config = [{'name': 'foo', 'schemas': None}]
  1064. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  1065. flexmock(module).should_receive('make_dump_path')
  1066. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  1067. flexmock(module).should_receive('execute_command_with_processes').never()
  1068. module.restore_data_source_dump(
  1069. hook_config,
  1070. {},
  1071. 'test.yaml',
  1072. data_source={'name': 'foo'},
  1073. dry_run=True,
  1074. extract_process=flexmock(),
  1075. connection_params={
  1076. 'hostname': None,
  1077. 'port': None,
  1078. 'username': None,
  1079. 'password': None,
  1080. },
  1081. borgmatic_runtime_directory='/run/borgmatic',
  1082. )
  1083. def test_restore_data_source_dump_without_extract_process_restores_from_disk():
  1084. hook_config = [{'name': 'foo', 'schemas': None}]
  1085. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  1086. flexmock(module).should_receive('make_dump_path')
  1087. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  1088. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1089. (
  1090. 'pg_restore',
  1091. '--no-password',
  1092. '--if-exists',
  1093. '--exit-on-error',
  1094. '--clean',
  1095. '--dbname',
  1096. 'foo',
  1097. '/dump/path',
  1098. ),
  1099. processes=[],
  1100. output_log_level=logging.DEBUG,
  1101. input_file=None,
  1102. extra_environment={'PGSSLMODE': 'disable'},
  1103. ).once()
  1104. flexmock(module).should_receive('execute_command').with_args(
  1105. (
  1106. 'psql',
  1107. '--no-password',
  1108. '--no-psqlrc',
  1109. '--quiet',
  1110. '--dbname',
  1111. 'foo',
  1112. '--command',
  1113. 'ANALYZE',
  1114. ),
  1115. extra_environment={'PGSSLMODE': 'disable'},
  1116. ).once()
  1117. module.restore_data_source_dump(
  1118. hook_config,
  1119. {},
  1120. 'test.yaml',
  1121. data_source={'name': 'foo'},
  1122. dry_run=False,
  1123. extract_process=None,
  1124. connection_params={
  1125. 'hostname': None,
  1126. 'port': None,
  1127. 'username': None,
  1128. 'password': None,
  1129. },
  1130. borgmatic_runtime_directory='/run/borgmatic',
  1131. )
  1132. def test_restore_data_source_dump_with_schemas_restores_schemas():
  1133. hook_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  1134. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  1135. flexmock(module).should_receive('make_dump_path')
  1136. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  1137. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1138. (
  1139. 'pg_restore',
  1140. '--no-password',
  1141. '--if-exists',
  1142. '--exit-on-error',
  1143. '--clean',
  1144. '--dbname',
  1145. 'foo',
  1146. '/dump/path',
  1147. '--schema',
  1148. 'bar',
  1149. '--schema',
  1150. 'baz',
  1151. ),
  1152. processes=[],
  1153. output_log_level=logging.DEBUG,
  1154. input_file=None,
  1155. extra_environment={'PGSSLMODE': 'disable'},
  1156. ).once()
  1157. flexmock(module).should_receive('execute_command').with_args(
  1158. (
  1159. 'psql',
  1160. '--no-password',
  1161. '--no-psqlrc',
  1162. '--quiet',
  1163. '--dbname',
  1164. 'foo',
  1165. '--command',
  1166. 'ANALYZE',
  1167. ),
  1168. extra_environment={'PGSSLMODE': 'disable'},
  1169. ).once()
  1170. module.restore_data_source_dump(
  1171. hook_config,
  1172. {},
  1173. 'test.yaml',
  1174. data_source=hook_config[0],
  1175. dry_run=False,
  1176. extract_process=None,
  1177. connection_params={
  1178. 'hostname': None,
  1179. 'port': None,
  1180. 'username': None,
  1181. 'password': None,
  1182. },
  1183. borgmatic_runtime_directory='/run/borgmatic',
  1184. )