IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 41060


Ignore:
Timestamp:
Nov 5, 2019, 10:34:28 AM (7 years ago)
Author:
fairlamb
Message:

Added a new option to place rsyncs in specific subdirs (for clearer naming)

Location:
branches/ipp-350_add_to_jira_conf_backups
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/ipp-350_add_to_jira_conf_backups/backup.py

    r41059 r41060  
    3333    [ ConfigSchemaLine('sources', True, list, SpecialSchemaProcessing.commma_separated)
    3434    , ConfigSchemaLine('additional_args', False, list, SpecialSchemaProcessing.space_separated)
     35    , ConfigSchemaLine('sub-dirs', False, list, SpecialSchemaProcessing.commma_separated)
    3536    ]
    3637)
     
    205206        sources = self.rsync_dict['sources']
    206207        for i in range(0, len(sources)):
    207             s = sources[i]
    208             if not path.exists(s):
    209                     raise errs.ValidationError(f"ValidationError: target path does not exist: {s}")
     208            source = sources[i]
     209            if not path.exists(source):
     210                    raise errs.ValidationError(f"ValidationError: target path does not exist: {source}")
    210211
    211212            for bp in backup_paths:
    212213                try:
    213                     args = [s, bp]
    214 
     214                    additional_args = []
    215215                    if 'additional_args' in self.rsync_dict.keys():
    216                         args = args + self.rsync_dict['additional_args']
     216                        additional_args = self.rsync_dict['additional_args']
     217
     218                    source_and_target_args = [source, bp]
     219
     220                    # sub-dirs option allows items to be backed up to subdirs within
     221                    # the source for better naming
     222                    if 'sub-dirs' in self.rsync_dict:
     223                        sub_dirs = self.rsync_dict['sub-dirs']
     224                        if len(sub_dirs) != len(sources):
     225                            raise errs.ValidationError(f"ValidationError: if using " +
     226                                "subdirs the number of subdirs must equal the number of sources")
     227                        bp_with_subdir = os.path.join(bp, sub_dirs[i])
     228                        if not os.path.exists(bp_with_subdir):
     229                            os.makedirs(bp_with_subdir)
     230                        source_and_target_args = [source, bp_with_subdir]
     231
     232                    args = source_and_target_args + additional_args
    217233                    sub_utils.local_rsync_wrapper(args)
    218234
    219235                except errs.SubprocessError as e:
    220236                    raise errs.SubprocessError( f"Error: rsync\n"
    221                                                 f"    from: {s}\n"
    222                                                 f"    to: {bp}\n"
     237                                                f"    from: {source_and_target_args[0]}\n"
     238                                                f"    to: {source_and_target_args[1]}\n"
    223239                                              ) from e
    224240        return 0
  • branches/ipp-350_add_to_jira_conf_backups/backup_test.py

    r40967 r41060  
    596596        assert os.path.exists(expected4dir)
    597597
     598    def test_rsync_with_subdirs_option(self, tmpdir):
     599        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     600        source1 = os.path.join(tmpdir, "some_dir")
     601        source2 = os.path.join(tmpdir, "some_other_dir")
     602        file_in_dir = os.path.join(source1, "and_specific_file.jpeg")
     603        file_in_other_dir = os.path.join(source2, "another_file.jpeg")
     604        os.makedirs(source1)
     605        os.makedirs(source2)
     606        Path(file_in_dir).touch()
     607        Path(file_in_other_dir).touch()
     608
     609        config['RSYNC'] = \
     610        { 'sources' : f"{source1}, {source2}"
     611        , 'additional_args' : "-a --progress -v"
     612        , 'sub-dirs' : "subdir_with_better_name1, subdir_with_better_name2"
     613        }
     614
     615        schema = ConfigSchema(None)
     616        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     617        schema.add_section(bckup.RSYNC_SCHEMA_SECTION)
     618
     619        backy = Backup(config, schema)
     620
     621        expected_bak1_src1_dir  = os.path.join(tmpdir, 'bck1', 'subdir_with_better_name1', 'some_dir')
     622        expected_bak1_src1_file = os.path.join(tmpdir, 'bck1', 'subdir_with_better_name1', 'some_dir', 'and_specific_file.jpeg')
     623        expected_bak1_src2_dir  = os.path.join(tmpdir, 'bck1', 'subdir_with_better_name2', 'some_other_dir')
     624        expected_bak1_src2_file = os.path.join(tmpdir, 'bck1', 'subdir_with_better_name2', 'some_other_dir', 'another_file.jpeg')
     625        expected_bak2_src1_dir  = os.path.join(tmpdir, 'bck2', 'subdir_with_better_name1', 'some_dir')
     626        expected_bak2_src1_file = os.path.join(tmpdir, 'bck2', 'subdir_with_better_name1', 'some_dir', 'and_specific_file.jpeg')
     627        expected_bak2_src2_dir  = os.path.join(tmpdir, 'bck2', 'subdir_with_better_name2', 'some_other_dir')
     628        expected_bak2_src2_file = os.path.join(tmpdir, 'bck2', 'subdir_with_better_name2', 'some_other_dir', 'another_file.jpeg')
     629        assert not os.path.exists(expected_bak1_src1_dir)
     630        assert not os.path.exists(expected_bak1_src1_file)
     631        assert not os.path.exists(expected_bak1_src2_dir)
     632        assert not os.path.exists(expected_bak1_src2_file)
     633        assert not os.path.exists(expected_bak2_src1_dir)
     634        assert not os.path.exists(expected_bak2_src1_file)
     635        assert not os.path.exists(expected_bak2_src2_dir)
     636        assert not os.path.exists(expected_bak2_src2_file)
     637        backy._run_rsync()
     638        assert os.path.exists(expected_bak1_src1_dir)
     639        assert os.path.exists(expected_bak1_src1_file)
     640        assert os.path.exists(expected_bak1_src2_dir)
     641        assert os.path.exists(expected_bak1_src2_file)
     642        assert os.path.exists(expected_bak2_src1_dir)
     643        assert os.path.exists(expected_bak2_src1_file)
     644        assert os.path.exists(expected_bak2_src2_dir)
     645        assert os.path.exists(expected_bak2_src2_file)
    598646
    599647@pytest.mark.skip(reason="Requires a fake database to be setup")
Note: See TracChangeset for help on using the changeset viewer.