test_postgresql.py 42 KB

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