IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40912


Ignore:
Timestamp:
Oct 4, 2019, 4:50:00 PM (7 years ago)
Author:
fairlamb
Message:

that function was used - moved it to generic; separated out jira/conf tar

Location:
branches/ipp-259_genericise_backups/tools/backups
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/ipp-259_genericise_backups/tools/backups/backup_atlassian_applications.py

    r40911 r40912  
    9090
    9191        # 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 = []
    9395
    9496        # copy jira
    95         jira_return = self._copy_jira_backup(self.verbose)
     97        return_status.append(self._copy_jira_backup(self.verbose))
    9698
    9799        # copy confluence
    98         conf_return = self._copy_confluence_backup(self.verbose)
     100        return_status.append(self._copy_confluence_backup(self.verbose))
    99101
    100102        # tarfiles:
    101         tar_return = self._tar_attachments()
     103        return_status.append(self._tar_jira_attachments())
     104        return_status.append(self._tar_conf_attachments())
    102105
    103106        # mysqldump
    104         mysqldump_return = self.make_mysql_dump(self.mysqldump_password)
     107        return_status.append(self.make_mysql_dump(self.mysqldump_password))
    105108
    106109        # 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)
    109113        else:
    110114            # 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)
    112116            return 5
    113117
     
    177181        return 0
    178182
    179     def _tar_attachments(self):
     183    def _tar_jira_attachments(self):
    180184        # tar jira
    181185        latest_paths = self.target_paths("latest")
     
    191195        sub_utils.cp_wrapper([ jira_tar_filepath, latest_paths[1]])
    192196        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
    193204
    194205        # tar confluence
  • branches/ipp-259_genericise_backups/tools/backups/backup_atlassian_applications_test.py

    r40898 r40912  
    99import backups.full_atlassian_backup_test as fab_test
    1010import backups.utils.subprocess_utils as sub_utils
     11import backups.utils.generic_utils as gen_utils
    1112
    1213from backups.backup_atlassian_applications import AtlassianBackups
     14from backups.utils.errors import ValidationError
    1315
    1416TEST_DEFAULT_CONFIG_FILE = os.path.join(os.path.dirname(__file__), './testing/test.config')
     
    215217        destination_paths = [ "/Two", "/paths"]
    216218
    217         return_is_non_zero = baa._move_dir_contents_to_dir(source_paths, destination_paths)
    218         assert return_is_non_zero != 0
     219        with pytest.raises(ValidationError):
     220            gen_utils.move_dirs(source_paths, destination_paths)
    219221
    220222    def test_moving_old_backups(self, tmpdir):
     
    275277
    276278        # 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)
    278280        assert return_result == 0
    279281
     
    357359class TestTaringOfAttachments(object):
    358360
    359     def test_attachments_tar(self, tmpdir):
     361    def test_jira_attachments_tar(self, tmpdir):
    360362
    361363        ab = AtlassianBackups(config_file=TEST_DEFAULT_CONFIG_FILE)
     
    364366        today = datetime.date.today().strftime("%Y_%m_%d")
    365367        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")
    366388        conf_tar_name = today + "_confluence_attachments.tar"
    367         expected_jira_host_tar = os.path.join(ab.host_backup_path, "latest", jira_tar_name)
    368389        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)
    370390        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)
    372391        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)
    374392        assert not os.path.exists(expected_conf_host_tar)
    375         assert not os.path.exists(expected_jira_bak1_tar)
    376393        assert not os.path.exists(expected_conf_bak1_tar)
    377         assert not os.path.exists(expected_jira_bak2_tar)
    378394        assert not os.path.exists(expected_conf_bak2_tar)
    379395
    380         tar_result = ab._tar_attachments()
     396        tar_result = ab._tar_conf_attachments()
    381397        assert tar_result == 0
    382398
    383         assert os.path.exists(expected_jira_host_tar)
    384399        assert os.path.exists(expected_conf_host_tar)
    385         assert os.path.exists(expected_jira_bak1_tar)
    386400        assert os.path.exists(expected_conf_bak1_tar)
    387         assert os.path.exists(expected_jira_bak2_tar)
    388401        assert os.path.exists(expected_conf_bak2_tar)
    389402
  • branches/ipp-259_genericise_backups/tools/backups/utils/generic_utils.py

    r40898 r40912  
     1import glob
    12import os.path as path
    23import shutil
    34
    45from distutils.dir_util import copy_tree
     6
     7import backups.utils.subprocess_utils as sub_utils
    58from backups.utils.errors import ValidationError
    69
     
    3538
    3639
     40def _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
     51def 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
    3770def copy_multiple_items_to_multiple_dirs(source_paths: [str], destination_paths: [str], verbose: bool=False):
    3871    """ Copies the sources to the destination paths.
    3972
    4073This 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)
    5175
    5276    for dpath in destination_paths:
     
    6690
    6791    return 0
     92
     93
     94def move_dirs(source_dirs: [str], destination_dirs: [str], verbose: bool=False):
     95    """ Moves all files from inside source paths to the destination dits.
     96It's important to note that it moves index to index
     97i.e. source[0] -> dest[0]; source[1] -> dest[1]
     98
     99This 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.