def vss_delete_shadow_copy(shadow_id, volume): """ Delete a shadow copy from the volume with the given shadow_id :param shadow_id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX :return: bool """ with DisableFileSystemRedirection(): cmd = [ 'vssadmin', 'delete', 'shadows', '/shadow={0}'.format(shadow_id), '/quiet' ] (out, err) = create_subprocess(cmd) if err != '': raise Exception('[*] Error deleting shadow copy with id {0}' ', error {1}'.format(shadow_id, err)) try: os.rmdir(os.path.join(volume, 'shadowcopy')) except Exception: logging.error('Failed to delete shadow copy symlink {0}'.format( os.path.join(volume, 'shadowcopy'))) logging.info('[*] Deleting shadow copy {0}'.format(shadow_id)) return True
def vss_delete_shadow_copy(shadow_id, windows_volume): """ Delete a shadow copy from the volume with the given shadow_id :param shadow_id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX :return: bool """ with DisableFileSystemRedirection(): cmd = ['vssadmin', 'delete', 'shadows', '/shadow={0}'.format(shadow_id), '/quiet'] (out, err) = create_subprocess(cmd) if err != '': raise Exception('[*] Error deleting shadow copy with id {0}' ', error {1}' .format(shadow_id, err)) try: os.rmdir(os.path.join(windows_volume, 'freezer_shadowcopy')) except Exception: logging.error('Failed to delete shadow copy symlink {0}'. format(os.path.join(windows_volume, 'freezer_shadowcopy'))) logging.info('[*] Deleting shadow copy {0}'. format(shadow_id)) return True
def stop(self): """Stop the windows service by using sc queryex command, if we use win32serviceutil.StoptService(self.service_name) it never gets stopped becuase freezer_scheduler.start() blocks the windows service and prevents any new signal to reach the service. """ query = 'sc queryex {0}'.format(self.service_name) out = utils.create_subprocess(query)[0] pid = None for line in out.split('\n'): if 'PID' in line: pid = line.split(':')[1].strip() command = 'taskkill /f /pid {0}'.format(pid) utils.create_subprocess(command) print('Freezer Service has stopped')
def vss_create_shadow_copy(volume): """ Create a new shadow copy for the specified volume Windows registry path for vss: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VSS\Settings MaxShadowCopies Windows is limited in how many shadow copies can create per volume. The default amount of shadow copies is 64, the minimum is 1 and the maxi- mum is 512, if you want to change the default value you need to add/edit the key MaxShadowCopies and set the amount of shadow copies per volume. MinDiffAreaFileSize The minimum size of the shadow copy storage area is a per-computer setting that can be specified by using the MinDiffAreaFileSize registry value. If the MinDiffAreaFileSize registry value is not set, the minimum size of the shadow copy storage area is 32 MB for volumes that are smaller than 500 MB and 320 MB for volumes that are larger than 500 MB. If you have not set a maximum size, there is no limit to the amount of space that can be used. If the MinDiffAreaFileSize registry value does not exist, the backup application can create it under the following registry key: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VolSnap Freezer create a shadow copy for each time the client runs it's been removed after the backup is complete. :param volume: The letter of the windows volume e.g. c:\\ :return: shadow_id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX :return: shadow_path: shadow copy path """ shadow_path = None shadow_id = None with DisableFileSystemRedirection(): path = os.path.dirname(os.path.abspath(__file__)) script = '{0}\\scripts\\vss.ps1'.format(path) (out, err) = create_subprocess([ 'powershell.exe', '-executionpolicy', 'unrestricted', '-command', script, '-volume', volume ]) if err != '': raise Exception('[*] Error creating a new shadow copy on {0}' ', error {1}'.format(volume, err)) for line in out.split('\n'): if 'symbolic' in line: shadow_path = line.split('>>')[1].strip() if '__RELPATH' in line: shadow_id = line.split('=')[1].strip().lower() + '}' shadow_id = shadow_id[1:] logging.info('[*] Created shadow copy {0}'.format(shadow_id)) return shadow_path, shadow_id
def start_sql_server(sql_server_instance): """ Start the SQL Server instance after the backup is completed """ with DisableFileSystemRedirection(): cmd = 'net start "SQL Server ({0})"'.format(sql_server_instance) (out, err) = create_subprocess(cmd) if err != "": raise Exception("[*] Error while starting SQL Server" ", error {0}".format(err)) logging.info("[*] SQL Server back to normal")
def stop_sql_server(backup_opt_dict): """ Stop a SQL Server instance to perform the backup of the db files """ logging.info("[*] Stopping SQL Server for backup") with DisableFileSystemRedirection(): cmd = 'net stop "SQL Server ({0})"'.format(backup_opt_dict.sql_server_instance) (out, err) = create_subprocess(cmd) if err != "": raise Exception("[*] Error while stopping SQL Server," ", error {0}".format(err))
def stop_sql_server(sql_server_instance): """ Stop a SQL Server instance to perform the backup of the db files """ logging.info('[*] Stopping SQL Server for backup') with DisableFileSystemRedirection(): cmd = 'net stop "SQL Server ({0})"'\ .format(sql_server_instance) (out, err) = create_subprocess(cmd) if err != '': raise Exception('[*] Error while stopping SQL Server,' ', error {0}'.format(err))
def start_sql_server(backup_opt_dict): """ Start the SQL Server instance after the backup is completed """ with DisableFileSystemRedirection(): cmd = 'net start "SQL Server ({0})"'\ .format(backup_opt_dict.sql_server_instance) (out, err) = create_subprocess(cmd) if err != '': raise Exception('[*] Error while starting SQL Server' ', error {0}'.format(err)) logging.info('[*] SQL Server back to normal')
def stop_sql_server(backup_opt_dict): """ Stop a SQL Server instance to perform the backup of the db files """ logging.info('[*] Stopping SQL Server for backup') with DisableFileSystemRedirection(): cmd = 'net stop "SQL Server ({0})"'\ .format(backup_opt_dict.sql_server_instance) (out, err) = create_subprocess(cmd) if err != '': raise Exception('[*] Error while stopping SQL Server,' ', error {0}'.format(err))
def execute(self): try: (out, err) = utils.create_subprocess('sync') if err: 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() if self.conf.mode == 'fs': backup.backup(self.conf, self.storage, self.engine) elif self.conf.mode == 'mongo': backup.backup_mode_mongo(self.conf) elif self.conf.mode == 'mysql': backup.backup_mode_mysql(self.conf) elif self.conf.mode == 'sqlserver': backup.backup_mode_sql_server(self.conf) else: raise ValueError('Please provide a valid backup mode')
def vss_delete_shadow_copy(shadow_id, windows_volume): """ Delete a shadow copy from the volume with the given shadow_id :param shadow_id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX :return: bool """ with DisableFileSystemRedirection(): cmd = ['vssadmin', 'delete', 'shadows', '/shadow={0}'.format(shadow_id), '/quiet'] (out, err) = create_subprocess(cmd) if err != '': raise Exception('[*] Error deleting shadow copy with id {0}' ', error {1}' .format(shadow_id, err)) vss_delete_symlink(windows_volume) logging.info('[*] Deleting shadow copy {0}'. format(shadow_id)) return True
def execute(self): try: (out, err) = utils.create_subprocess('sync') if err: logging.error('Error while sync exec: {0}'.format(err)) except Exception as error: logging.error('Error while sync exec: {0}'.format(error)) if not self.conf.mode: raise ValueError("Empty mode") mod_name = 'freezer.mode.{0}.{1}'.format( self.conf.mode, self.conf.mode.capitalize() + 'Mode') app_mode = importutils.import_object(mod_name, self.conf) backup_instance = backup.backup( self.conf, self.storage, self.engine, app_mode) level = backup_instance.level if backup_instance else 0 metadata = { 'curr_backup_level': level, 'fs_real_path': (self.conf.lvm_auto_snap or self.conf.path_to_backup), 'vol_snap_path': self.conf.path_to_backup if self.conf.lvm_auto_snap else '', 'client_os': sys.platform, 'client_version': self.conf.__version__, 'time_stamp': self.conf.time_stamp } fields = ['action', 'always_level', 'backup_media', 'backup_name', 'container', 'container_segments', 'dry_run', 'hostname', 'path_to_backup', 'max_level', 'mode', 'backup_name', 'hostname', 'time_stamp', 'log_file', 'storage', 'mode', 'os_auth_version', 'proxy', 'compression', 'ssh_key', 'ssh_username', 'ssh_host', 'ssh_port'] for field_name in fields: metadata[field_name] = self.conf.__dict__.get(field_name, '') or '' return metadata
def vss_create_shadow_copy(windows_volume): """ Create a new shadow copy for the specified volume Windows registry path for vssadmin: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VSS\Settings MaxShadowCopies Windows is limited in how many shadow copies can create per volume. The default amount of shadow copies is 64, the minimum is 1 and the maxi- mum is 512, if you want to change the default value you need to add/edit the key MaxShadowCopies and set the amount of shadow copies per volume. MinDiffAreaFileSize The minimum size of the shadow copy storage area is a per-computer setting that can be specified by using the MinDiffAreaFileSize registry value. If the MinDiffAreaFileSize registry value is not set, the minimum size of the shadow copy storage area is 32 MB for volumes that are smaller than 500 MB and 320 MB for volumes that are larger than 500 MB. If you have not set a maximum size, there is no limit to the amount of space that can be used. If the MinDiffAreaFileSize registry value does not exist, the backup application can create it under the following registry key: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VolSnap Freezer create a shadow copy for each time the client runs it's been removed after the backup is complete. :param volume: The letter of the windows volume e.g. c:\\ :return: shadow_id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX :return: shadow_path: shadow copy path """ shadow_path = None shadow_id = None with DisableFileSystemRedirection(): path = os.path.dirname(os.path.abspath(__file__)) script = '{0}\\scripts\\vss.ps1'.format(path) (out, err) = create_subprocess(['powershell.exe', '-executionpolicy', 'unrestricted', '-command', script, '-volume', windows_volume]) if err != '': raise Exception('[*] Error creating a new shadow copy on {0}' ', error {1}' .format(windows_volume, err)) for line in out.split('\n'): if 'symbolic' in line: shadow_path = line.split('>>')[1].strip() if '__RELPATH' in line: shadow_id = line.split('=')[1].strip().lower() + '}' shadow_id = shadow_id[1:] logging.info('[*] Created shadow copy {0}'. format(shadow_id)) return shadow_path, shadow_id