IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40924


Ignore:
Timestamp:
Oct 8, 2019, 5:05:19 PM (7 years ago)
Author:
fairlamb
Message:

run_tar added to backup class + tests

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

Legend:

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

    r40923 r40924  
    11import configparser
     2import datetime
    23import os.path as path
    34import socket
     
    153154    def _run_tar(self):
    154155        if self.run_tar is None or False:
    155             return False
    156         return True
     156            return 0
     157
     158        dests = self.default_dict['backup_paths']
     159
     160        filename = self.tar_dict['target_filename']
     161        if 'prefix_date' in self.tar_dict.keys() and self.tar_dict['prefix_date']:
     162            filename = f"{get_date()}_{filename}"
     163        filename = path.join(dests[0], filename)  # may need some tar suffix magic
     164
     165        add_args = [] if 'additional_args' not in self.tar_dict.keys() else self.tar_dict['additional_args']
     166        all_args = add_args + [filename] + self.tar_dict['sources']
     167
     168        try:
     169            print(all_args)
     170            sub_utils.tar_wrapper(all_args)
     171            sub_utils.chmod_wrapper(['774', filename])
     172            # copy to backups
     173            if len(dests) == 1:
     174                return 0
     175            for d in range(1, len(dests)):
     176                sub_utils.cp_wrapper([ filename, dests[d]])
     177        except errs.SubprocessError as e:
     178            raise errs.SubprocessError() from e
     179        return 0
    157180
    158181    def _run_rsync(self):
    159         if self.run_rsync is None or False:
    160             return False
    161         return True
     182        raise Exception("Not implemented")
    162183
    163184    def _run_mysqldump(self):
    164         if self.run_mysqldump is None or False:
    165             return False
    166         return True
     185        raise Exception("Not implemented")
     186
     187
     188def get_date(custom_date=None, custom_format="%Y-%b-%d"):
     189        # Format is currently: 2019-10-08
     190        if custom_date is not None:
     191            return custom_date.strftime(custom_format)
     192        return datetime.date.today().strftime(custom_format)
  • branches/ipp-259_genericise_backups/tools/backups/backup_test.py

    r40923 r40924  
    55from pathlib import Path
    66
     7import backups.backup as bckup
    78import backups.utils.errors as errs
    89from backups.backup import Backup
     
    243244
    244245
    245 # class TestRunningTar():
    246 
    247 #     def test_simply_tar(self):
    248 #         assert True is False
    249 
    250 #     def test_tar_with_additional_args(self):
    251 #         assert True is False
     246class TestRunningTar():
     247
     248    def test_simply_tar(self, tmpdir):
     249        config = make_default_config(tmpdir, ['bck1'])
     250        source = os.path.join(tmpdir, "folder_to_tar")
     251        os.makedirs(source)
     252        # put a file in it
     253        Path(os.path.join(source, "a_file.txt"))
     254
     255        config['TAR'] = \
     256        { 'sources' : f"{source}"
     257        , 'additional_args' : "-C / -cf"
     258        , 'target_filename' : 'cool_tar.tar'
     259        , 'prefix_date' : 'False'
     260        }
     261
     262        backy = Backup(config)
     263
     264        expected_tar1 = os.path.join(tmpdir, 'bck1', 'cool_tar.tar')
     265        assert not os.path.exists(expected_tar1)
     266        backy._run_tar()
     267        print(expected_tar1)
     268        assert os.path.exists(expected_tar1)
     269
     270    def test_tar_with_multiple_sources_and_date(self, tmpdir):
     271        config = make_default_config(tmpdir, ['bck1', 'bck2'])
     272        source1 = os.path.join(tmpdir, "folder_to_tar")
     273        source2 = os.path.join(tmpdir, "another_folder_to_tar")
     274        os.makedirs(source1)
     275        os.makedirs(source2)
     276        # put a file in it
     277        Path(os.path.join(source1, "a_file.txt"))
     278        Path(os.path.join(source2, "other.file"))
     279
     280        config['TAR'] = \
     281        { 'sources' : f"{source1}, {source2}"
     282        , 'additional_args' : "-C / -cf"
     283        , 'target_filename' : 'cool_tar.tar'
     284        , 'prefix_date' : 'True'
     285        }
     286
     287        backy = Backup(config)
     288
     289        date = bckup.get_date()
     290
     291        expected_tar1 = os.path.join(tmpdir, 'bck1', f'{date}_cool_tar.tar')
     292        expected_tar2 = os.path.join(tmpdir, 'bck2', f'{date}_cool_tar.tar')
     293        assert not os.path.exists(expected_tar1)
     294        assert not os.path.exists(expected_tar2)
     295        backy._run_tar()
     296        print(expected_tar1)
     297        print(expected_tar2)
     298        assert os.path.exists(expected_tar1)
     299        assert os.path.exists(expected_tar2)
    252300
    253301
Note: See TracChangeset for help on using the changeset viewer.