test_postgresql.py 41 KB

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