IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40966


Ignore:
Timestamp:
Oct 24, 2019, 1:19:12 PM (7 years ago)
Author:
fairlamb
Message:

Backups write out a tiny log saying when it was last run

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

Legend:

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

    r40962 r40966  
    115115
    116116    def run_backup_processes(self):
    117 
     117        self._write_begin_log()
    118118        if self.should_run_copy:
    119119            self._run_copy()
     
    124124        if self.should_run_mysqldump:
    125125            self._run_mysqldump()
     126        self._write_end_log()
    126127
    127128    def hostname_is_valid(self) -> bool:
     
    260261        return 0
    261262
     263    def _write_begin_log(self):
     264        backup_paths = self.default_dict["backup_paths"]
     265        for bp in backup_paths:
     266            log_file = path.join(bp, "latest_run.log")
     267            if path.exists(log_file):
     268                os.remove(log_file)
     269            with open(log_file, "w+") as lf:
     270                lf.write(f'Run started at: {datetime.date.today().strftime("%Y_%b_%d_%H:%M")}\n')
     271
     272    def _write_end_log(self):
     273        backup_paths = self.default_dict["backup_paths"]
     274        for bp in backup_paths:
     275            log_file = path.join(bp, "latest_run.log")
     276            if not path.exists(log_file):
     277                return
     278            with open(log_file, "a") as lf:
     279                lf.write(f'Last successful backup completed at : {datetime.date.today().strftime("%Y_%b_%d_%H:%M")}\n')
     280
    262281
    263282def get_date(custom_date=None, custom_format="%Y_%m_%d"):
  • branches/ipp-259_genericise_backups/tools/backups/backup_test.py

    r40962 r40966  
    354354        config['TAR'] = \
    355355        { 'sources' : f"{source}"
    356         , 'additional_args' : "-C / -cf"
     356        , 'additional_args' : "-cf"
    357357        , 'target_filename' : 'cool_tar.tar'
    358358        , 'prefix_date' : 'False'
     
    366366
    367367        expected_tar1 = os.path.join(tmpdir, 'bck1', 'cool_tar.tar')
     368        print(expected_tar1)
    368369        assert not os.path.exists(expected_tar1)
    369370        backy._run_tar()
     
    382383        config['TAR'] = \
    383384        { 'sources' : f"{source1}, {source2}"
    384         , 'additional_args' : "-C / -cf"
     385        , 'additional_args' : "-cf"
    385386        , 'target_filename' : 'cool_tar.tar'
    386387        , 'prefix_date' : 'True'
     
    596597
    597598
    598 
    599599@pytest.mark.skip(reason="Requires a fake database to be setup")
    600600class TestMysqlBackup(object):
     
    656656        with pytest.raises(errs.SubprocessError):
    657657            backy._run_mysqldump()
     658
     659
     660# class TestRunningAll(object):
     661
     662#     def test_running_copy_tar_and_rsyc(self):
     663        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     664
     665
     666class TestSimpleLogWriting(object):
     667
     668    def test_log_writing_success(self, tmpdir):
     669        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     670        schema = ConfigSchema(None)
     671        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     672
     673        backy = Backup(config, schema)
     674
     675        expected_log1 = os.path.join(tmpdir, 'bck1', 'latest_run.log')
     676        expected_log2 = os.path.join(tmpdir, 'bck2', 'latest_run.log')
     677        assert not os.path.exists(expected_log1)
     678        assert not os.path.exists(expected_log2)
     679        backy.run_backup_processes()
     680        assert os.path.exists(expected_log1)
     681        assert os.path.exists(expected_log2)
     682
     683        # Read file
     684        with open(expected_log1, "r") as logf:
     685            lines = logf.readlines()
     686            assert len(lines) == 2
     687            assert "Run started at: " in lines[0]
     688            assert "Last successful backup completed at :" in lines[1]
     689
     690        with open(expected_log2, "r") as logf:
     691            lines = logf.readlines()
     692            assert len(lines) == 2
     693            assert "Run started at: " in lines[0]
     694            assert "Last successful backup completed at :" in lines[1]
     695
     696    def test_log_writes_only_attempted_if_failure_encountered(self, tmpdir):
     697
     698        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     699        source = os.path.join(tmpdir, "and_specific_file.jpeg")
     700        config['COPY'] = \
     701        { 'sources' : f"{source}"
     702        , 'additional_args' : '-r'
     703        }
     704        schema = ConfigSchema(None)
     705        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     706        schema.add_section(bckup.COPY_SCHEMA_SECTION)
     707
     708        backy = Backup(config, schema)
     709
     710        expected_log1 = os.path.join(tmpdir, 'bck1', 'latest_run.log')
     711        expected_log2 = os.path.join(tmpdir, 'bck2', 'latest_run.log')
     712        assert not os.path.exists(expected_log1)
     713        assert not os.path.exists(expected_log2)
     714        # fails due to missing source file in copy
     715        with pytest.raises(errs.ValidationError):
     716            backy.run_backup_processes()
     717
     718        assert os.path.exists(expected_log1)
     719        assert os.path.exists(expected_log2)
     720
     721        with open(expected_log1, "r") as logf:
     722            lines = logf.readlines()
     723            assert len(lines) == 1
     724            assert "Run started at: " in lines[0]
     725
     726        with open(expected_log2, "r") as logf:
     727            lines = logf.readlines()
     728            assert len(lines) == 1
     729            assert "Run started at: " in lines[0]
Note: See TracChangeset for help on using the changeset viewer.