diff --git a/freezer/backup.py b/freezer/backup.py index 1bed513d..8e322603 100644 --- a/freezer/backup.py +++ b/freezer/backup.py @@ -32,7 +32,7 @@ from freezer.winutils import is_windows home = expanduser("~") -def backup_mode_sql_server(backup_opt_dict): +def backup_mode_sql_server(backup_opt_dict, storage): """ Execute a SQL Server DB backup. Currently only backups with shadow copy are supported. This mean, as soon as the shadow copy is created @@ -60,8 +60,7 @@ def backup_mode_sql_server(backup_opt_dict): try: stop_sql_server(backup_opt_dict.sql_server_instance) - backup(backup_opt_dict, backup_opt_dict.storage, - backup_opt_dict.engine) + backup(backup_opt_dict, storage, backup_opt_dict.engine) finally: if not backup_opt_dict.vssadmin: # if vssadmin is false, wait until the backup is complete @@ -69,7 +68,7 @@ def backup_mode_sql_server(backup_opt_dict): start_sql_server(backup_opt_dict.sql_server_instance) -def backup_mode_mysql(backup_opt_dict): +def backup_mode_mysql(backup_opt_dict, storage): """ Execute a MySQL DB backup. currently only backup with lvm snapshots are supported. This mean, just before the lvm snap vol is created, @@ -132,10 +131,10 @@ def backup_mode_mysql(backup_opt_dict): raise Exception('[*] MySQL: {0}'.format(error)) # Execute backup - backup(backup_opt_dict, backup_opt_dict.storage, backup_opt_dict.engine) + backup(backup_opt_dict, storage, backup_opt_dict.engine) -def backup_mode_mongo(backup_opt_dict): +def backup_mode_mongo(backup_opt_dict, storage): """ Execute the necessary tasks for file system backup mode """ @@ -156,8 +155,7 @@ def backup_mode_mongo(backup_opt_dict): mongo_primary = master_dict['primary'] if mongo_me == mongo_primary: - backup(backup_opt_dict, backup_opt_dict.storage, - backup_opt_dict.engine) + backup(backup_opt_dict, storage, backup_opt_dict.engine) else: logging.warning('[*] localhost {0} is not Master/Primary,\ exiting...'.format(local_hostname)) diff --git a/freezer/job.py b/freezer/job.py index 5ce3e2bb..af22248d 100644 --- a/freezer/job.py +++ b/freezer/job.py @@ -33,9 +33,9 @@ class Job: :type engine: freezer.engine.engine.BackupEngine """ - def __init__(self, conf_dict): + def __init__(self, conf_dict, storage): self.conf = conf_dict - self.storage = conf_dict.storage + self.storage = storage self.engine = conf_dict.engine def execute(self): @@ -77,16 +77,16 @@ class BackupJob(Job): logging.error('Error while sync exec: {0}'.format(err)) except Exception as error: logging.error('Error while sync exec: {0}'.format(error)) - self.conf.storage.prepare() + self.storage.prepare() if self.conf.mode == 'fs': backup.backup(self.conf, self.storage, self.engine) elif self.conf.mode == 'mongo': - backup.backup_mode_mongo(self.conf) + backup.backup_mode_mongo(self.conf, self.storage) elif self.conf.mode == 'mysql': - backup.backup_mode_mysql(self.conf) + backup.backup_mode_mysql(self.conf, self.storage) elif self.conf.mode == 'sqlserver': - backup.backup_mode_sql_server(self.conf) + backup.backup_mode_sql_server(self.conf, self.storage) else: raise ValueError('Please provide a valid backup mode') @@ -169,15 +169,15 @@ class ExecJob(Job): return True -def create_job(conf): +def create_job(conf, storage): if conf.action == 'backup': - return BackupJob(conf) + return BackupJob(conf, storage) if conf.action == 'restore': - return RestoreJob(conf) + return RestoreJob(conf, storage) if conf.action == 'info': - return InfoJob(conf) + return InfoJob(conf, storage) if conf.action == 'admin': - return AdminJob(conf) + return AdminJob(conf, storage) if conf.action == 'exec': - return ExecJob(conf) + return ExecJob(conf, storage) raise Exception('Action "{0}" not supported'.format(conf.action)) diff --git a/freezer/main.py b/freezer/main.py index 8c2e6957..14c84b84 100644 --- a/freezer/main.py +++ b/freezer/main.py @@ -130,7 +130,6 @@ def freezer_main(backup_args, arg_parse): else: raise Exception("Not storage found for name " + backup_args.storage) - backup_args.__dict__['storage'] = storage backup_args.__dict__['engine'] = tar_engine.TarBackupEngine( backup_args.compression, backup_args.dereference_symlink, @@ -145,7 +144,7 @@ def freezer_main(backup_args, arg_parse): if int(os.environ.get("tricklecount")) > 1: logging.critical("[*] Trickle seems to be not working," " Switching to normal mode ") - run_job(backup_args) + run_job(backup_args, storage) freezer_command = '{0} {1}'.format(backup_args.trickle_command, ' '.join(sys.argv)) @@ -160,14 +159,14 @@ def freezer_main(backup_args, arg_parse): if process.returncode: logging.error("[*] Trickle Error: {0}".format(error)) logging.critical("[*] Switching to work without trickle ...") - run_job(backup_args) + run_job(backup_args, storage) else: - run_job(backup_args) + run_job(backup_args, storage) -def run_job(backup_args): - freezer_job = job.create_job(backup_args) +def run_job(backup_args, storage): + freezer_job = job.create_job(backup_args, storage) freezer_job.execute() if backup_args.metadata_out == '-': diff --git a/tests/test_job.py b/tests/test_job.py index 29390bc4..0f433784 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -30,7 +30,8 @@ class TestJob(unittest.TestCase): fakebackup = FakeBackup() def test_execute(self): - job = Job(BackupOpt1()) + opt = BackupOpt1() + job = Job(opt, opt.storage) assert job.execute() is None @@ -38,13 +39,13 @@ class TestInfoJob(TestJob): def test_execute_nothing_to_do(self): backup_opt = BackupOpt1() - job = InfoJob(backup_opt) + job = InfoJob(backup_opt, backup_opt.storage) job.execute() def test_execute_list_containers(self): backup_opt = BackupOpt1() backup_opt.list_containers = True - job = InfoJob(backup_opt) + job = InfoJob(backup_opt, backup_opt.storage) job.execute() @@ -54,7 +55,7 @@ class TestBackupJob(TestJob): backup_opt = BackupOpt1() backup_opt.mode = 'fs' backup_opt.no_incremental = True - job = BackupJob(backup_opt) + job = BackupJob(backup_opt, backup_opt.storage) self.assertRaises(Exception, job.execute) def test_execute_backup_mongo(self): @@ -62,7 +63,7 @@ class TestBackupJob(TestJob): backup_opt = BackupOpt1() backup_opt.no_incremental = False backup_opt.mode = 'mongo' - job = BackupJob(backup_opt) + job = BackupJob(backup_opt, backup_opt.storage) assert job.execute() is None def test_execute_backup_mysql(self): @@ -70,21 +71,21 @@ class TestBackupJob(TestJob): backup_opt = BackupOpt1() backup_opt.no_incremental = False backup_opt.mode = 'mysql' - job = BackupJob(backup_opt) + job = BackupJob(backup_opt, backup_opt.storage) assert job.execute() is None def test_execute_raise(self): backup_opt = BackupOpt1() backup_opt.no_incremental = False backup_opt.mode = None - job = BackupJob(backup_opt) + job = BackupJob(backup_opt, backup_opt.storage) self.assertRaises(ValueError, job.execute) class TestAdminJob(TestJob): def test_execute(self): backup_opt = BackupOpt1() - job = AdminJob(backup_opt) + job = AdminJob(backup_opt, backup_opt.storage) assert job.execute() is None @@ -103,14 +104,14 @@ class TestExecJob(TestJob): def test_execute_nothing_to_do(self): backup_opt = BackupOpt1() - job = ExecJob(backup_opt) + job = ExecJob(backup_opt, backup_opt.storage) assert job.execute() is False def test_execute_script(self): self.mock_popen.return_value.returncode = 0 backup_opt = BackupOpt1() backup_opt.command='echo test' - job = ExecJob(backup_opt) + job = ExecJob(backup_opt, backup_opt.storage) assert job.execute() is True def test_execute_raise(self): @@ -119,7 +120,7 @@ class TestExecJob(TestJob): self.mock_popen.return_value.returncode = 1 backup_opt = BackupOpt1() backup_opt.command = 'echo test' - job = ExecJob(backup_opt) + job = ExecJob(backup_opt, backup_opt.storage) self.assertRaises(Exception, job.execute) def test_create_job(self): @@ -128,21 +129,21 @@ class TestExecJob(TestJob): self.assertRaises(Exception, create_job, backup_opt) backup_opt.action = 'backup' - job = create_job(backup_opt) + job = create_job(backup_opt, backup_opt.storage) assert isinstance(job, BackupJob) backup_opt.action = 'restore' - job = create_job(backup_opt) + job = create_job(backup_opt, backup_opt.storage) assert isinstance(job, RestoreJob) backup_opt.action = 'info' - job = create_job(backup_opt) + job = create_job(backup_opt, backup_opt.storage) assert isinstance(job, InfoJob) backup_opt.action = 'admin' - job = create_job(backup_opt) + job = create_job(backup_opt, backup_opt.storage) assert isinstance(job, AdminJob) backup_opt.action = 'exec' - job = create_job(backup_opt) + job = create_job(backup_opt, backup_opt.storage) assert isinstance(job, ExecJob)