Changeset 40912
- Timestamp:
- Oct 4, 2019, 4:50:00 PM (7 years ago)
- Location:
- branches/ipp-259_genericise_backups/tools/backups
- Files:
-
- 3 edited
-
backup_atlassian_applications.py (modified) (3 diffs)
-
backup_atlassian_applications_test.py (modified) (5 diffs)
-
utils/generic_utils.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/ipp-259_genericise_backups/tools/backups/backup_atlassian_applications.py
r40911 r40912 90 90 91 91 # Move latest to tmp (and delete if move ok) 92 gen_utils.copy_multiple_items_to_multiple_dirs(latest_paths, tmp_paths, self.verbose) 92 gen_utils.move_dirs(latest_paths, tmp_paths, self.verbose) 93 94 return_status = [] 93 95 94 96 # copy jira 95 jira_return = self._copy_jira_backup(self.verbose)97 return_status.append(self._copy_jira_backup(self.verbose)) 96 98 97 99 # copy confluence 98 conf_return = self._copy_confluence_backup(self.verbose)100 return_status.append(self._copy_confluence_backup(self.verbose)) 99 101 100 102 # tarfiles: 101 tar_return = self._tar_attachments() 103 return_status.append(self._tar_jira_attachments()) 104 return_status.append(self._tar_conf_attachments()) 102 105 103 106 # mysqldump 104 mysqldump_return = self.make_mysql_dump(self.mysqldump_password)107 return_status.append(self.make_mysql_dump(self.mysqldump_password)) 105 108 106 109 # If successful: move tmp to old 107 if jira_return == 0 and conf_return == 0 and tar_return == 0 and mysqldump_return == 0: 108 gen_utils.copy_multiple_items_to_multiple_dirs(tmp_paths, old_paths, self.verbose) 110 failures = [ rs for rs in return_status if rs != 0 ] 111 if len(failures) == 0: 112 gen_utils.move_dirs(tmp_paths, old_paths, self.verbose) 109 113 else: 110 114 # If any failed: delete latest, move tmp back 111 gen_utils. copy_multiple_items_to_multiple_dirs(tmp_paths, latest_paths, self.verbose)115 gen_utils.move_dirs(tmp_paths, latest_paths, self.verbose) 112 116 return 5 113 117 … … 177 181 return 0 178 182 179 def _tar_ attachments(self):183 def _tar_jira_attachments(self): 180 184 # tar jira 181 185 latest_paths = self.target_paths("latest") … … 191 195 sub_utils.cp_wrapper([ jira_tar_filepath, latest_paths[1]]) 192 196 sub_utils.cp_wrapper([ jira_tar_filepath, latest_paths[2]]) 197 return 0 198 199 200 def _tar_conf_attachments(self): 201 latest_paths = self.target_paths("latest") 202 if not self.target_backup_paths_ok(): 203 return 1 193 204 194 205 # tar confluence -
branches/ipp-259_genericise_backups/tools/backups/backup_atlassian_applications_test.py
r40898 r40912 9 9 import backups.full_atlassian_backup_test as fab_test 10 10 import backups.utils.subprocess_utils as sub_utils 11 import backups.utils.generic_utils as gen_utils 11 12 12 13 from backups.backup_atlassian_applications import AtlassianBackups 14 from backups.utils.errors import ValidationError 13 15 14 16 TEST_DEFAULT_CONFIG_FILE = os.path.join(os.path.dirname(__file__), './testing/test.config') … … 215 217 destination_paths = [ "/Two", "/paths"] 216 218 217 return_is_non_zero = baa._move_dir_contents_to_dir(source_paths, destination_paths)218 assert return_is_non_zero != 0219 with pytest.raises(ValidationError): 220 gen_utils.move_dirs(source_paths, destination_paths) 219 221 220 222 def test_moving_old_backups(self, tmpdir): … … 275 277 276 278 # run the move 277 return_result = baa._move_dir_contents_to_dir(source_paths, destination_paths)279 return_result = gen_utils.move_dirs(source_paths, destination_paths) 278 280 assert return_result == 0 279 281 … … 357 359 class TestTaringOfAttachments(object): 358 360 359 def test_ attachments_tar(self, tmpdir):361 def test_jira_attachments_tar(self, tmpdir): 360 362 361 363 ab = AtlassianBackups(config_file=TEST_DEFAULT_CONFIG_FILE) … … 364 366 today = datetime.date.today().strftime("%Y_%m_%d") 365 367 jira_tar_name = today + "_jira_attachments.tar" 368 expected_jira_host_tar = os.path.join(ab.host_backup_path, "latest", jira_tar_name) 369 expected_jira_bak1_tar = os.path.join(ab.backup_1_path, "latest", jira_tar_name) 370 expected_jira_bak2_tar = os.path.join(ab.backup_2_path, "latest", jira_tar_name) 371 assert not os.path.exists(expected_jira_host_tar) 372 assert not os.path.exists(expected_jira_bak1_tar) 373 assert not os.path.exists(expected_jira_bak2_tar) 374 375 tar_result = ab._tar_jira_attachments() 376 assert tar_result == 0 377 378 assert os.path.exists(expected_jira_host_tar) 379 assert os.path.exists(expected_jira_bak1_tar) 380 assert os.path.exists(expected_jira_bak2_tar) 381 382 def test_conf_attachments_tar(self, tmpdir): 383 384 ab = AtlassianBackups(config_file=TEST_DEFAULT_CONFIG_FILE) 385 ab = fab_test.create_all_paths_and_folders(ab, tmpdir) 386 387 today = datetime.date.today().strftime("%Y_%m_%d") 366 388 conf_tar_name = today + "_confluence_attachments.tar" 367 expected_jira_host_tar = os.path.join(ab.host_backup_path, "latest", jira_tar_name)368 389 expected_conf_host_tar = os.path.join(ab.host_backup_path, "latest", conf_tar_name) 369 expected_jira_bak1_tar = os.path.join(ab.backup_1_path, "latest", jira_tar_name)370 390 expected_conf_bak1_tar = os.path.join(ab.backup_1_path, "latest", conf_tar_name) 371 expected_jira_bak2_tar = os.path.join(ab.backup_2_path, "latest", jira_tar_name)372 391 expected_conf_bak2_tar = os.path.join(ab.backup_2_path, "latest", conf_tar_name) 373 assert not os.path.exists(expected_jira_host_tar)374 392 assert not os.path.exists(expected_conf_host_tar) 375 assert not os.path.exists(expected_jira_bak1_tar)376 393 assert not os.path.exists(expected_conf_bak1_tar) 377 assert not os.path.exists(expected_jira_bak2_tar)378 394 assert not os.path.exists(expected_conf_bak2_tar) 379 395 380 tar_result = ab._tar_ attachments()396 tar_result = ab._tar_conf_attachments() 381 397 assert tar_result == 0 382 398 383 assert os.path.exists(expected_jira_host_tar)384 399 assert os.path.exists(expected_conf_host_tar) 385 assert os.path.exists(expected_jira_bak1_tar)386 400 assert os.path.exists(expected_conf_bak1_tar) 387 assert os.path.exists(expected_jira_bak2_tar)388 401 assert os.path.exists(expected_conf_bak2_tar) 389 402 -
branches/ipp-259_genericise_backups/tools/backups/utils/generic_utils.py
r40898 r40912 1 import glob 1 2 import os.path as path 2 3 import shutil 3 4 4 5 from distutils.dir_util import copy_tree 6 7 import backups.utils.subprocess_utils as sub_utils 5 8 from backups.utils.errors import ValidationError 6 9 … … 35 38 36 39 40 def _list_checks(sources: [str], targets: [str]): 41 if sources is None or targets is None: 42 raise ValidationError("Error: source for copy cannot be None type") 43 if targets is None: 44 raise ValidationError("Error: copy destination cannot be None type") 45 if type(sources) is not list or len(sources) == 0: 46 raise ValidationError("Error: sources must be given as a list with at least one entry") 47 if type(targets) is not list or len(targets) == 0: 48 raise ValidationError("Error: targets must be given as a list with at least one entry") 49 50 51 def copy_multiple_items_as_one_to_one(source_paths: [str], destination_paths: [str], verbose: bool=False): 52 _list_checks() 53 54 for i in range(0, len(source_paths)): 55 56 if not path.exists(destination_paths[i]): 57 if verbose: 58 print("Copy Warning: target path does not exist (skipping): ", destination_paths[i]) 59 continue 60 if not path.isdir(destination_paths[i]): 61 if verbose: 62 print("Copy Warning: target path is not a directory (skipping): ", destination_paths[i]) 63 continue 64 65 result = copy_to_dir(source_paths[i], destination_paths[i], verbose) 66 if result != 0: 67 return result 68 69 37 70 def copy_multiple_items_to_multiple_dirs(source_paths: [str], destination_paths: [str], verbose: bool=False): 38 71 """ Copies the sources to the destination paths. 39 72 40 73 This is a generic function that just wraps shutil.copy2""" 41 42 if source_paths is None: 43 raise ValidationError("Error: source for copy cannot be None type") 44 if destination_paths is None: 45 raise ValidationError("Error: copy destination cannot be None type") 46 47 if type(source_paths) is not list or len(source_paths) == 0: 48 raise ValidationError("Error: sources must be given as a list with at least one entry") 49 if type(destination_paths) is not list or len(destination_paths) == 0: 50 raise ValidationError("Error: destinations must be given as a list with at least one entry") 74 _list_checks(source_paths, destination_paths) 51 75 52 76 for dpath in destination_paths: … … 66 90 67 91 return 0 92 93 94 def move_dirs(source_dirs: [str], destination_dirs: [str], verbose: bool=False): 95 """ Moves all files from inside source paths to the destination dits. 96 It's important to note that it moves index to index 97 i.e. source[0] -> dest[0]; source[1] -> dest[1] 98 99 This is actually a pretty generic function, should be moved into a utils class. 100 """ 101 102 _list_checks(source_dirs, destination_dirs) 103 104 if len(source_dirs) != len(destination_dirs): 105 raise ValidationError("Error: source and target paths are not equal; aborting file movement") 106 107 for i in range(0, len(source_dirs)): 108 # Identify files in path, move each one: 109 wildcard_search = "{0}/*".format(source_dirs[i]) 110 filepaths = glob.glob(wildcard_search) 111 if len(filepaths) == 0: 112 print("Warning: directory is empty: ", source_dirs[i]) 113 continue 114 try: 115 for fp in filepaths: 116 if verbose: 117 print("Moving: {0} -> {1}".format(fp, destination_dirs[i])) 118 command_and_args = ["mv", fp, destination_dirs[i]] 119 # shutil.move(fp, destination_dirs[i]) 120 sub_utils.simple_unix_wrapper(command_and_args) 121 except IOError as e: 122 print("IOError copying: ", e.filename, e.filename2) 123 return 2 124 125 return 0
Note:
See TracChangeset
for help on using the changeset viewer.
