Changeset 40924
- Timestamp:
- Oct 8, 2019, 5:05:19 PM (7 years ago)
- Location:
- branches/ipp-259_genericise_backups/tools/backups
- Files:
-
- 2 edited
-
backup.py (modified) (2 diffs)
-
backup_test.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/ipp-259_genericise_backups/tools/backups/backup.py
r40923 r40924 1 1 import configparser 2 import datetime 2 3 import os.path as path 3 4 import socket … … 153 154 def _run_tar(self): 154 155 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 157 180 158 181 def _run_rsync(self): 159 if self.run_rsync is None or False: 160 return False 161 return True 182 raise Exception("Not implemented") 162 183 163 184 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 188 def 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 5 5 from pathlib import Path 6 6 7 import backups.backup as bckup 7 8 import backups.utils.errors as errs 8 9 from backups.backup import Backup … … 243 244 244 245 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 246 class 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) 252 300 253 301
Note:
See TracChangeset
for help on using the changeset viewer.
