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 the db writes will be blocked and a checkpoint will be created, as soon as the backup finish the db will be unlocked and the backup will be uploaded. A sql_server.conf_file is required for this operation. """ with open(backup_opt_dict.sql_server_conf, 'r') as sql_conf_file_fd: for line in sql_conf_file_fd: line = line.strip().split('#', 1)[0] if not line: continue key, value = line.split('=') # remove white spaces key = key.strip() value = value.strip() if key == 'instance': db_instance = utils.dequote(value) backup_opt_dict.sql_server_instance = db_instance continue else: raise Exception('Please indicate a valid SQL Server instance') try: stop_sql_server(backup_opt_dict.sql_server_instance) 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 # to start sql server again start_sql_server(backup_opt_dict.sql_server_instance)
def snapshot_create(backup_opt_dict): if is_windows(): if backup_opt_dict.vssadmin: # Create a shadow copy. backup_opt_dict.shadow_path, backup_opt_dict.shadow = \ vss_create_shadow_copy(backup_opt_dict.windows_volume) # execute this after the snapshot creation if backup_opt_dict.mode == 'sqlserver': start_sql_server(backup_opt_dict) else: # If lvm_auto_snap is true, the volume group and volume name will # be extracted automatically if backup_opt_dict.lvm_auto_snap: lvm_list = get_lvm_info( backup_opt_dict.lvm_auto_snap) backup_opt_dict.lvm_volgroup = lvm_list[0] backup_opt_dict.lvm_srcvol = lvm_list[2] # Generate the lvm_snap if lvm arguments are available lvm_snap(backup_opt_dict) if is_windows() and backup_opt_dict.vssadmin: backup_opt_dict.path_to_backup = use_shadow( backup_opt_dict.path_to_backup, backup_opt_dict.windows_volume) return backup_opt_dict
def snapshot_create(backup_opt_dict): """ Calls the code to take fs snapshots, depending on the platform :param backup_opt_dict: :return: boolean value, True if snapshot has been taken, false otherwise """ if is_windows(): # vssadmin is to be deprecated in favor of the --snapshot flag if backup_opt_dict.snapshot: backup_opt_dict.vssadmin = True if backup_opt_dict.vssadmin: # Create a shadow copy. backup_opt_dict.shadow_path, backup_opt_dict.shadow = \ vss_create_shadow_copy(backup_opt_dict.windows_volume) backup_opt_dict.path_to_backup = use_shadow( backup_opt_dict.path_to_backup, backup_opt_dict.windows_volume) # execute this after the snapshot creation if backup_opt_dict.mode == 'sqlserver': start_sql_server(backup_opt_dict.sql_server_instance) return True return False else: return lvm.lvm_snap(backup_opt_dict)
def test_start_sql_server(self, monkeypatch): fake_disable_redirection = FakeDisableFileSystemRedirection() backup_opt = BackupOpt1() fakelogging = FakeLogging() fakesubprocess = FakeSubProcess() fakesubprocesspopen = fakesubprocess.Popen() monkeypatch.setattr( subprocess.Popen, 'communicate', fakesubprocesspopen.communicate) monkeypatch.setattr( subprocess, 'Popen', fakesubprocesspopen) monkeypatch.setattr( winutils.DisableFileSystemRedirection, '__enter__', fake_disable_redirection.__enter__) monkeypatch.setattr( winutils.DisableFileSystemRedirection, '__exit__', fake_disable_redirection.__exit__) monkeypatch.setattr(logging, 'info', fakelogging.info) assert winutils.start_sql_server(backup_opt) is not False fakesubprocess = FakeSubProcess3() fakesubprocesspopen = fakesubprocess.Popen() monkeypatch.setattr( subprocess.Popen, 'communicate', fakesubprocesspopen.communicate) monkeypatch.setattr( subprocess, 'Popen', fakesubprocesspopen) pytest.raises(Exception, winutils.start_sql_server(backup_opt))