def reload(self, which): ba = BackupArchive.objects.only('backup_object', 'id', 'path').get(backup_object=self, id__exact=which) if ba.file_hash != generate_file_hash(ba.path): raise ArchiveHashesDoNotMatch call_command('reset', self.app_label, interactive=False) call_command('loaddata', ba.path, verbosity=0)
def backup(self): dt = datetime.datetime.now() name = u'%s_%s%s.%s' % (self.app_label, dt.strftime('%Y%j-'), dt.microsecond, FORMAT) if self.compress == 'gz': from gzip import GzipFile name = name + u'.gz' elif self.compress == 'bz2': from bz2 import BZ2File name = name + u'.bz2' path = os.path.join(settings.VZ_BACKUP_DIR, name) try: if self.compress == 'gz': b_file = GzipFile(path, 'wb') elif self.compress == 'bz2': b_file = BZ2File(path, 'w') else: b_file = open(path, 'w') dump = dumpdata.Command() b_file.write(dump.handle( self.app_label, use_natural_keys=self.use_natural_keys, indent=INDENT, format=FORMAT)) b_file.close() file_hash = generate_file_hash(path) try: ba = BackupArchive.objects.get(file_hash__exact=file_hash, backup_object__exact=self) os.unlink(path) except BackupArchive.DoesNotExist: BackupArchive.objects.create( backup_object=self, name=name, path=path, size=os.path.getsize(path), file_hash=file_hash) except IOError: raise UnableToCreateArchive
def test_models_file_hash(self): #test file hash ba = BackupArchive.objects.get(id__exact=1) self.failUnlessEqual(ba.file_hash, generate_file_hash(ba.path))