Fix metadata curr_level
Resolves bug: 1551734 Change-Id: I77f9d3827497b80e9b620543f2f4a805c00ff0ad
This commit is contained in:
parent
c9ac953c77
commit
564d0ccab4
@ -46,7 +46,7 @@ def backup_mode_sql_server(backup_opt_dict, storage):
|
|||||||
backup_opt_dict.sql_server_instance = sql_server_instance
|
backup_opt_dict.sql_server_instance = sql_server_instance
|
||||||
try:
|
try:
|
||||||
winutils.stop_sql_server(sql_server_instance)
|
winutils.stop_sql_server(sql_server_instance)
|
||||||
backup(backup_opt_dict, storage, backup_opt_dict.engine)
|
return backup(backup_opt_dict, storage, backup_opt_dict.engine)
|
||||||
finally:
|
finally:
|
||||||
if not backup_opt_dict.snapshot:
|
if not backup_opt_dict.snapshot:
|
||||||
# if snapshot is false, wait until the backup is complete
|
# if snapshot is false, wait until the backup is complete
|
||||||
@ -87,7 +87,7 @@ def backup_mode_mysql(backup_opt_dict, storage):
|
|||||||
raise Exception('[*] MySQL: {0}'.format(error))
|
raise Exception('[*] MySQL: {0}'.format(error))
|
||||||
|
|
||||||
# Execute backup
|
# Execute backup
|
||||||
backup(backup_opt_dict, storage, backup_opt_dict.engine)
|
return backup(backup_opt_dict, storage, backup_opt_dict.engine)
|
||||||
|
|
||||||
|
|
||||||
def backup_mode_mongo(backup_opt_dict, storage):
|
def backup_mode_mongo(backup_opt_dict, storage):
|
||||||
@ -111,11 +111,11 @@ def backup_mode_mongo(backup_opt_dict, storage):
|
|||||||
mongo_primary = master_dict['primary']
|
mongo_primary = master_dict['primary']
|
||||||
|
|
||||||
if mongo_me == mongo_primary:
|
if mongo_me == mongo_primary:
|
||||||
backup(backup_opt_dict, storage, backup_opt_dict.engine)
|
return backup(backup_opt_dict, storage, backup_opt_dict.engine)
|
||||||
else:
|
else:
|
||||||
logging.warning('[*] localhost {0} is not Master/Primary,\
|
logging.warning('[*] localhost {0} is not Master/Primary,\
|
||||||
exiting...'.format(local_hostname))
|
exiting...'.format(local_hostname))
|
||||||
return True
|
return None
|
||||||
|
|
||||||
|
|
||||||
class BackupOs:
|
class BackupOs:
|
||||||
@ -282,12 +282,12 @@ def backup(backup_opt_dict, storage, engine):
|
|||||||
backup_opt_dict.restart_always_level,
|
backup_opt_dict.restart_always_level,
|
||||||
time_stamp=time_stamp)
|
time_stamp=time_stamp)
|
||||||
engine.backup(filepath, backup_instance)
|
engine.backup(filepath, backup_instance)
|
||||||
|
return backup_instance
|
||||||
finally:
|
finally:
|
||||||
# whether an error occurred or not, remove the snapshot anyway
|
# whether an error occurred or not, remove the snapshot anyway
|
||||||
if snapshot_taken:
|
if snapshot_taken:
|
||||||
snapshot_remove(backup_opt_dict, backup_opt_dict.shadow,
|
snapshot_remove(backup_opt_dict, backup_opt_dict.shadow,
|
||||||
backup_opt_dict.windows_volume)
|
backup_opt_dict.windows_volume)
|
||||||
return
|
|
||||||
|
|
||||||
backup_os = BackupOs(backup_opt_dict.client_manager,
|
backup_os = BackupOs(backup_opt_dict.client_manager,
|
||||||
backup_opt_dict.container,
|
backup_opt_dict.container,
|
||||||
@ -304,3 +304,4 @@ def backup(backup_opt_dict, storage, engine):
|
|||||||
backup_os.backup_cinder_by_glance(backup_opt_dict.cinder_vol_id)
|
backup_os.backup_cinder_by_glance(backup_opt_dict.cinder_vol_id)
|
||||||
else:
|
else:
|
||||||
raise Exception('unknown parameter backup_media %s' % backup_media)
|
raise Exception('unknown parameter backup_media %s' % backup_media)
|
||||||
|
return None
|
||||||
|
@ -33,7 +33,7 @@ logging = log.getLogger(__name__)
|
|||||||
|
|
||||||
class Job:
|
class Job:
|
||||||
"""
|
"""
|
||||||
:type storage: freezer.storage.Storage
|
:type storage: freezer.storage.base.Storage
|
||||||
:type engine: freezer.engine.engine.BackupEngine
|
:type engine: freezer.engine.engine.BackupEngine
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -45,9 +45,6 @@ class Job:
|
|||||||
def execute(self):
|
def execute(self):
|
||||||
logging.info('[*] Action not implemented')
|
logging.info('[*] Action not implemented')
|
||||||
|
|
||||||
def get_metadata(self):
|
|
||||||
return None
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def executemethod(func):
|
def executemethod(func):
|
||||||
def wrapper(self):
|
def wrapper(self):
|
||||||
@ -83,19 +80,24 @@ class BackupJob(Job):
|
|||||||
logging.error('Error while sync exec: {0}'.format(error))
|
logging.error('Error while sync exec: {0}'.format(error))
|
||||||
|
|
||||||
if self.conf.mode == 'fs':
|
if self.conf.mode == 'fs':
|
||||||
backup.backup(self.conf, self.storage, self.engine)
|
backup_instance = \
|
||||||
|
backup.backup(self.conf, self.storage, self.engine)
|
||||||
elif self.conf.mode == 'mongo':
|
elif self.conf.mode == 'mongo':
|
||||||
backup.backup_mode_mongo(self.conf, self.storage)
|
backup_instance = \
|
||||||
|
backup.backup_mode_mongo(self.conf, self.storage)
|
||||||
elif self.conf.mode == 'mysql':
|
elif self.conf.mode == 'mysql':
|
||||||
backup.backup_mode_mysql(self.conf, self.storage)
|
backup_instance = \
|
||||||
|
backup.backup_mode_mysql(self.conf, self.storage)
|
||||||
elif self.conf.mode == 'sqlserver':
|
elif self.conf.mode == 'sqlserver':
|
||||||
backup.backup_mode_sql_server(self.conf, self.storage)
|
backup_instance = \
|
||||||
|
backup.backup_mode_sql_server(self.conf, self.storage)
|
||||||
else:
|
else:
|
||||||
raise ValueError('Please provide a valid backup mode')
|
raise ValueError('Please provide a valid backup mode')
|
||||||
|
|
||||||
def get_metadata(self):
|
level = backup_instance.level if backup_instance else 0
|
||||||
|
|
||||||
metadata = {
|
metadata = {
|
||||||
'curr_backup_level': 0,
|
'curr_backup_level': level,
|
||||||
'fs_real_path': (self.conf.lvm_auto_snap or
|
'fs_real_path': (self.conf.lvm_auto_snap or
|
||||||
self.conf.path_to_backup),
|
self.conf.path_to_backup),
|
||||||
'vol_snap_path':
|
'vol_snap_path':
|
||||||
@ -142,6 +144,7 @@ class RestoreJob(Job):
|
|||||||
res.restore_cinder(conf.cindernative_vol_id, restore_timestamp)
|
res.restore_cinder(conf.cindernative_vol_id, restore_timestamp)
|
||||||
else:
|
else:
|
||||||
raise Exception("unknown backup type: %s" % conf.backup_media)
|
raise Exception("unknown backup type: %s" % conf.backup_media)
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
class AdminJob(Job):
|
class AdminJob(Job):
|
||||||
@ -156,6 +159,7 @@ class AdminJob(Job):
|
|||||||
|
|
||||||
self.storage.remove_older_than(timestamp,
|
self.storage.remove_older_than(timestamp,
|
||||||
self.conf.hostname_backup_name)
|
self.conf.hostname_backup_name)
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
class ExecJob(Job):
|
class ExecJob(Job):
|
||||||
@ -168,19 +172,4 @@ class ExecJob(Job):
|
|||||||
else:
|
else:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
'[*] No command info options were set. Exiting.')
|
'[*] No command info options were set. Exiting.')
|
||||||
return False
|
return {}
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def create_job(conf, storage):
|
|
||||||
if conf.action == 'backup':
|
|
||||||
return BackupJob(conf, storage)
|
|
||||||
if conf.action == 'restore':
|
|
||||||
return RestoreJob(conf, storage)
|
|
||||||
if conf.action == 'info':
|
|
||||||
return InfoJob(conf, storage)
|
|
||||||
if conf.action == 'admin':
|
|
||||||
return AdminJob(conf, storage)
|
|
||||||
if conf.action == 'exec':
|
|
||||||
return ExecJob(conf, storage)
|
|
||||||
raise Exception('Action "{0}" not supported'.format(conf.action))
|
|
||||||
|
@ -121,14 +121,18 @@ def freezer_main(backup_args):
|
|||||||
run_job(backup_args, storage)
|
run_job(backup_args, storage)
|
||||||
|
|
||||||
|
|
||||||
def run_job(backup_args, storage):
|
def run_job(conf, storage):
|
||||||
freezer_job = job.create_job(backup_args, storage)
|
freezer_job = {
|
||||||
freezer_job.execute()
|
'backup': job.BackupJob,
|
||||||
|
'restore': job.RestoreJob,
|
||||||
|
'info': job.InfoJob,
|
||||||
|
'admin': job.AdminJob,
|
||||||
|
'exec': job.ExecJob}[conf.action](conf, storage)
|
||||||
|
response = freezer_job.execute()
|
||||||
|
|
||||||
if backup_args.metadata_out == '-':
|
if conf.metadata_out == '-':
|
||||||
metadata = freezer_job.get_metadata()
|
if response:
|
||||||
if metadata:
|
sys.stdout.write(json.dumps(response))
|
||||||
sys.stdout.write(json.dumps(metadata))
|
|
||||||
|
|
||||||
|
|
||||||
def fail(exit_code, e, quiet, do_log=True):
|
def fail(exit_code, e, quiet, do_log=True):
|
||||||
|
@ -20,26 +20,13 @@ from freezer.tests.commons import *
|
|||||||
from freezer.job import ExecJob
|
from freezer.job import ExecJob
|
||||||
from freezer import backup
|
from freezer import backup
|
||||||
|
|
||||||
from freezer.job import Job, InfoJob, AdminJob, BackupJob, RestoreJob, \
|
from freezer.job import Job, InfoJob, AdminJob, BackupJob
|
||||||
create_job
|
|
||||||
from mock import patch, Mock
|
from mock import patch, Mock
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class FakeBackup:
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def fake_backup_mode_mongo(self, *args, **kwargs):
|
|
||||||
return True
|
|
||||||
|
|
||||||
def fake_backup_mode_mysql(self, *args, **kwargs):
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class TestJob(unittest.TestCase):
|
class TestJob(unittest.TestCase):
|
||||||
fakebackup = FakeBackup()
|
|
||||||
|
|
||||||
def test_execute(self):
|
def test_execute(self):
|
||||||
opt = BackupOpt1()
|
opt = BackupOpt1()
|
||||||
@ -69,22 +56,6 @@ class TestBackupJob(TestJob):
|
|||||||
job = BackupJob(backup_opt, backup_opt.storage)
|
job = BackupJob(backup_opt, backup_opt.storage)
|
||||||
self.assertRaises(Exception, job.execute)
|
self.assertRaises(Exception, job.execute)
|
||||||
|
|
||||||
def test_execute_backup_mongo(self):
|
|
||||||
backup.backup_mode_mongo = self.fakebackup.fake_backup_mode_mongo
|
|
||||||
backup_opt = BackupOpt1()
|
|
||||||
backup_opt.no_incremental = False
|
|
||||||
backup_opt.mode = 'mongo'
|
|
||||||
job = BackupJob(backup_opt, backup_opt.storage)
|
|
||||||
assert job.execute() is None
|
|
||||||
|
|
||||||
def test_execute_backup_mysql(self):
|
|
||||||
backup.backup_mode_mysql = self.fakebackup.fake_backup_mode_mysql
|
|
||||||
backup_opt = BackupOpt1()
|
|
||||||
backup_opt.no_incremental = False
|
|
||||||
backup_opt.mode = 'mysql'
|
|
||||||
job = BackupJob(backup_opt, backup_opt.storage)
|
|
||||||
assert job.execute() is None
|
|
||||||
|
|
||||||
def test_execute_raise(self):
|
def test_execute_raise(self):
|
||||||
backup_opt = BackupOpt1()
|
backup_opt = BackupOpt1()
|
||||||
backup_opt.no_incremental = False
|
backup_opt.no_incremental = False
|
||||||
@ -96,8 +67,7 @@ class TestBackupJob(TestJob):
|
|||||||
class TestAdminJob(TestJob):
|
class TestAdminJob(TestJob):
|
||||||
def test_execute(self):
|
def test_execute(self):
|
||||||
backup_opt = BackupOpt1()
|
backup_opt = BackupOpt1()
|
||||||
job = AdminJob(backup_opt, backup_opt.storage)
|
job = AdminJob(backup_opt, backup_opt.storage).execute()
|
||||||
assert job.execute() is None
|
|
||||||
|
|
||||||
|
|
||||||
class TestExecJob(TestJob):
|
class TestExecJob(TestJob):
|
||||||
@ -115,15 +85,13 @@ class TestExecJob(TestJob):
|
|||||||
|
|
||||||
def test_execute_nothing_to_do(self):
|
def test_execute_nothing_to_do(self):
|
||||||
backup_opt = BackupOpt1()
|
backup_opt = BackupOpt1()
|
||||||
job = ExecJob(backup_opt, backup_opt.storage)
|
ExecJob(backup_opt, backup_opt.storage).execute()
|
||||||
assert job.execute() is False
|
|
||||||
|
|
||||||
def test_execute_script(self):
|
def test_execute_script(self):
|
||||||
self.mock_popen.return_value.returncode = 0
|
self.mock_popen.return_value.returncode = 0
|
||||||
backup_opt = BackupOpt1()
|
backup_opt = BackupOpt1()
|
||||||
backup_opt.command='echo test'
|
backup_opt.command='echo test'
|
||||||
job = ExecJob(backup_opt, backup_opt.storage)
|
ExecJob(backup_opt, backup_opt.storage).execute()
|
||||||
assert job.execute() is True
|
|
||||||
|
|
||||||
def test_execute_raise(self):
|
def test_execute_raise(self):
|
||||||
self.popen=patch('freezer.exec_cmd.subprocess.Popen')
|
self.popen=patch('freezer.exec_cmd.subprocess.Popen')
|
||||||
@ -133,28 +101,3 @@ class TestExecJob(TestJob):
|
|||||||
backup_opt.command = 'echo test'
|
backup_opt.command = 'echo test'
|
||||||
job = ExecJob(backup_opt, backup_opt.storage)
|
job = ExecJob(backup_opt, backup_opt.storage)
|
||||||
self.assertRaises(Exception, job.execute)
|
self.assertRaises(Exception, job.execute)
|
||||||
|
|
||||||
def test_create_job(self):
|
|
||||||
backup_opt = BackupOpt1()
|
|
||||||
backup_opt.action = None
|
|
||||||
self.assertRaises(Exception, create_job, backup_opt)
|
|
||||||
|
|
||||||
backup_opt.action = 'backup'
|
|
||||||
job = create_job(backup_opt, backup_opt.storage)
|
|
||||||
assert isinstance(job, BackupJob)
|
|
||||||
|
|
||||||
backup_opt.action = 'restore'
|
|
||||||
job = create_job(backup_opt, backup_opt.storage)
|
|
||||||
assert isinstance(job, RestoreJob)
|
|
||||||
|
|
||||||
backup_opt.action = 'info'
|
|
||||||
job = create_job(backup_opt, backup_opt.storage)
|
|
||||||
assert isinstance(job, InfoJob)
|
|
||||||
|
|
||||||
backup_opt.action = 'admin'
|
|
||||||
job = create_job(backup_opt, backup_opt.storage)
|
|
||||||
assert isinstance(job, AdminJob)
|
|
||||||
|
|
||||||
backup_opt.action = 'exec'
|
|
||||||
job = create_job(backup_opt, backup_opt.storage)
|
|
||||||
assert isinstance(job, ExecJob)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user