def start(self): """One-time backup""" paths = self.parsePaths(self.config) if not paths: self.logger.logmsg("WARNING", _("There are no paths to backup!")) return False if self.options["DestinationType"] == "remote (ssh)": # check if server settings are OK self.checkRemoteServer() if self.options["PkgListsToFile"]: pkgListfiles = self.createPkgLists() else: pkgListfiles = [] if self.options["DiskInfoToFile"]: pkgListfiles.append(self.createDiskInfo()) if not ( self.options["Engine"] == "rsync" and self.options["Incremental"] and not self.options["DestinationType"] == "remote (ssh)" ): if not self.prepareDestinationFolder(self.options["Destination"]): return False if os.path.exists(encode(self.dest)): self.logger.logmsg("WARNING", _("`%s' exists and will be overwritten.") % self.dest) shutil_modded.rmtree(encode(self.dest), onerror=self.onError) self.ifCancel() command = self.parseCommand(self.config) self.addListFilesToBackup(pkgListfiles, command, self.options["Engine"], paths) # Now that the paths & commands are set up... retval = self.backupPaths(paths, command) self.deleteListFiles(pkgListfiles) self.ifCancel() if self.options["DestinationType"] == "local": try: os.chmod(self.dest, 0711) except: pass # All done! self.logger.logmsg("INFO", _("Finished one-time backup")) return retval
def start(self): """One-time backup""" paths = self.parsePaths(self.config) if not paths: self.logger.logmsg('WARNING', _('There are no paths to backup!')) return False if self.options['DestinationType'] == 'remote (ssh)': # check if server settings are OK self.checkRemoteServer() if self.options['PkgListsToFile']: pkgListfiles = self.createPkgLists() else: pkgListfiles = [] if self.options['DiskInfoToFile']: pkgListfiles.append(self.createDiskInfo()) if not (self.options['Engine'] == 'rsync' and self.options['Incremental'] and \ not self.options['DestinationType'] == 'remote (ssh)'): if not self.prepareDestinationFolder(self.options['Destination']): return False if os.path.exists(encode(self.dest)): self.logger.logmsg('WARNING', _('`%s\' exists and will be overwritten.') % self.dest) shutil_modded.rmtree(encode(self.dest), onerror=self.onError) self.ifCancel() command = self.parseCommand() self.addListFilesToBackup(pkgListfiles, command, self.options['Engine'], paths) # Now that the paths & commands are set up... retval = self.backupPaths(paths, command) self.deleteListFiles(pkgListfiles) self.ifCancel() if self.options['DestinationType'] == 'local': try: os.chmod(self.dest, 0711) except: pass # All done! self.logger.logmsg('INFO', _('Finished one-time backup')) return retval
def removeOldBackups(self): """Get list of old backups and remove them""" # get listing, local or remote if self.options["DestinationType"] == "remote (ssh)": client, sftpClient = sftp.connect( self.options["RemoteHost"], self.options["RemoteUsername"], self.options["RemotePassword"], self.options["RemotePort"], ) listing = sftpClient.listdir(self.options["RemoteFolder"]) else: listing = os.listdir(encode(self.options["Destination"])) # Normalize the unicode strings storing the filenames. This fixes a problem # on HFS+ filesystems where supplying a set name on the command line # resulted in a different Unicode string than the filename of the set. listing = [decode(i, filename=True) for i in listing] listing.sort() oldbackups = [] for i in listing: backupPrefix = "%s-%s-" % (_("Backup"), self.config.getSetName()) if i.startswith(backupPrefix): oldbackups.append(i) # ...And remove them. oldbackups.reverse() if self.options["DestinationType"] == "remote (ssh)": for i in oldbackups[self.options["OldToKeep"] :]: remoteBackup = os.path.join(self.options["RemoteFolder"], i) self.logger.logmsg( "DEBUG", _("Removing old backup `%(a)s' on %(b)s") % {"a": remoteBackup, "b": self.options["RemoteHost"]}, ) sftp.remove(sftpClient, remoteBackup) sftpClient.close() client.close() else: if self.options["Engine"] == "rsync" and self.options["Incremental"] and oldbackups: for i in oldbackups[:-1]: self.logger.logmsg("DEBUG", _("Removing old backup `%s'") % i) path = os.path.join(self.options["Destination"], i) shutil_modded.rmtree(encode(path), onerror=self.onError) oldIncrementalBackup = os.path.join(self.options["Destination"], oldbackups[-1]) if ( not oldIncrementalBackup.endswith(".tar") and not oldIncrementalBackup.endswith(".tar.gz") and not oldIncrementalBackup.endswith(".tar.bz2") ): # oldIncrementalBackup = rsync self.logger.logmsg("DEBUG", _("Moving `%s' to `%s'") % (oldIncrementalBackup, self.dest)) shutil_modded.move(encode(oldIncrementalBackup), encode(self.dest)) else: # source = is not a rsync backup - remove it and start fresh self.logger.logmsg("DEBUG", _("`%s' is not an rsync backup - removing.") % oldIncrementalBackup) shutil_modded.rmtree(encode(oldIncrementalBackup), onerror=self.onError) else: for i in oldbackups[self.options["OldToKeep"] :]: self.logger.logmsg("DEBUG", _("Removing old backup `%s'") % i) path = os.path.join(self.options["Destination"], i) shutil_modded.rmtree(encode(path), onerror=self.onError)
def removeOldBackups(self): """Get list of old backups and remove them""" # get listing, local or remote if self.options['DestinationType'] == 'remote (ssh)': client, sftpClient = sftp.connect(self.options['RemoteHost'], self.options['RemoteUsername'], self.options['RemotePassword'], self.options['RemotePort']) listing = sftpClient.listdir(self.options['RemoteFolder']) else: listing = os.listdir(encode(self.options['Destination'])) # Normalize the unicode strings storing the filenames. This fixes a problem # on HFS+ filesystems where supplying a set name on the command line # resulted in a different Unicode string than the filename of the set. listing = [decode(i, filename=True) for i in listing] listing.sort() oldbackups = [] for i in listing: backupPrefix = '%s-%s-' % (_('Backup'), self.config.getSetName()) if i.startswith(backupPrefix): oldbackups.append(i) # ...And remove them. oldbackups.reverse() if self.options['DestinationType'] == 'remote (ssh)': for i in oldbackups[self.options['OldToKeep']:]: remoteBackup = os.path.join(self.options['RemoteFolder'], i) self.logger.logmsg('DEBUG', _('Removing old backup `%(a)s\' on %(b)s') % {'a': remoteBackup, 'b': self.options['RemoteHost']}) sftp.remove(sftpClient, remoteBackup) sftpClient.close() client.close() else: if self.options['Engine'] == 'rsync' and self.options['Incremental'] and oldbackups: for i in oldbackups[:-1]: self.logger.logmsg('DEBUG', _('Removing old backup `%s\'') % i) path = os.path.join(self.options['Destination'], i) shutil_modded.rmtree(encode(path), onerror=self.onError) oldIncrementalBackup = os.path.join(self.options['Destination'], oldbackups[-1]) if not oldIncrementalBackup.endswith('.tar') and not oldIncrementalBackup.endswith('.tar.gz') and \ not oldIncrementalBackup.endswith('.tar.bz2'): # oldIncrementalBackup = rsync self.logger.logmsg('DEBUG', _('Moving `%s\' to `%s\'') % (oldIncrementalBackup, self.dest)) shutil_modded.move(encode(oldIncrementalBackup), encode(self.dest)) else: # source = is not a rsync backup - remove it and start fresh self.logger.logmsg('DEBUG', _('`%s\' is not an rsync backup - removing.') % oldIncrementalBackup) shutil_modded.rmtree(encode(oldIncrementalBackup), onerror=self.onError) else: for i in oldbackups[self.options['OldToKeep']:]: self.logger.logmsg('DEBUG', _('Removing old backup `%s\'') % i) path = os.path.join(self.options['Destination'], i) shutil_modded.rmtree(encode(path), onerror=self.onError)
DESTDIR_BACKUP = os.path.join(TESTDIR, 'destination') DESTDIR_RESTORE = os.path.join(TESTDIR, 'restores') paths = [SOURCEDIR] print _("*** Initializing files") for directory in [TESTDIR, SOURCEDIR, DESTDIR_BACKUP, DESTDIR_RESTORE]: if os.path.exists(directory): print _("Folder %s exists and will be emptied. Press <Enter> to confirm or <ctrl+c> to cancel...") % directory try: raw_input() except KeyboardInterrupt: print sys.exit(0) print _("Cleaning folder %s...") % directory shutil_modded.rmtree(directory) os.mkdir(directory) # Initialize 50MB of files for num in range(0, 50): fh = open(os.path.join(SOURCEDIR, "file%s" % num), 'w') fh.write(random.choice('abcdefghijklmnopqrstuvwxyz1234567890-=+_;"}{[]|<>/?.,`~')*1048576) fh.close() print _("*** Remote settings") hostname = raw_input(_("Hostname [localhost]: ")) or 'localhost' while True: port = raw_input(_("Port [22]: ")) or '22' try: int(port) break
paths = [SOURCEDIR] print _("*** Initializing files") for directory in [TESTDIR, SOURCEDIR, DESTDIR_BACKUP, DESTDIR_RESTORE]: if os.path.exists(directory): print _( "Folder %s exists and will be emptied. Press <Enter> to confirm or <ctrl+c> to cancel..." ) % directory try: raw_input() except KeyboardInterrupt: print sys.exit(0) print _("Cleaning folder %s...") % directory shutil_modded.rmtree(directory) os.mkdir(directory) # Initialize 50MB of files for num in range(0, 50): fh = open(os.path.join(SOURCEDIR, "file%s" % num), 'w') fh.write( random.choice( 'abcdefghijklmnopqrstuvwxyz1234567890-=+_;"}{[]|<>/?.,`~') * 1048576) fh.close() print _("*** Remote settings") hostname = raw_input(_("Hostname [localhost]: ")) or 'localhost' while True: port = raw_input(_("Port [22]: ")) or '22'
def start(self): """Start the backup process. Should be called after executing user command.""" if not self.options['Enabled']: # set is disabled return True self.logger.logmsg('INFO', _('Starting automatic backup operation of set `%s\'') % self.config.getSetName()) if self.options["CommandBefore"]: self._status = STATUS_EXECING_USER_COMMAND # Find tokens and substitute them tokenized_command = self.tokens_replace(self.options["CommandBefore"], self.date) self.execute_user_command(1, tokenized_command) try: # Get the list of paths... paths = self.parsePaths(self.config) if not paths: self.logger.logmsg('WARNING', _('There are no paths to backup!')) return False self.ifCancel() if self.options['DestinationType'] == 'remote (ssh)': # check if server settings are OK self.checkRemoteServer() self._status = STATUS_CLEANING_OLD if not (self.options['Engine'] == 'rsync' and self.options['Incremental']) and \ not self.options['DestinationType'] == 'remote (ssh)': if not self.prepareDestinationFolder(self.options['Destination']): return False if not (self.options['Engine'] == 'rsync' and self.options['Incremental']) \ and os.path.exists(encode(self.dest)): self.logger.logmsg('WARNING', _('`%s\' exists and will be overwritten.') % self.dest) shutil_modded.rmtree(encode(self.dest), onerror=self.onError) self.ifCancel() # Remove old stuff self.removeOldBackups() self.ifCancel() self._status = STATUS_INITIALIZING if self.options['PkgListsToFile']: pkgListfiles = self.createPkgLists() else: pkgListfiles = [] if self.options['DiskInfoToFile']: pkgListfiles.append(self.createDiskInfo()) self.ifCancel() command = self.parseCommand() self.addListFilesToBackup(pkgListfiles, command, self.options['Engine'], paths) # Now that the paths & commands are set up... retval = self.backupPaths(paths, command) self.deleteListFiles(pkgListfiles) if self.options['DestinationType'] == 'local': try: os.chmod(self.dest, 0711) except: pass # Exception handlers in FuncAsThread() must return retval same values except exceptions.SystemExit: # cancelled; the only time we skip the after command return -2 except: retval = False import traceback (etype, value, tb) = sys.exc_info() self.traceback = ''.join(traceback.format_exception(etype, value, tb)) self.logger.logmsg('WARNING', _('There was an error while performing the backup!')) self.logger.logmsg('ERROR', self.traceback) # just incase we have leftover stuff running self.cancelOperation() if self.options["CommandAfter"]: self._status = STATUS_EXECING_USER_COMMAND # Find tokens and substitute them tokenized_command = self.tokens_replace(self.options["CommandAfter"], self.date, retval) self.execute_user_command(2, tokenized_command) # All done! self.logger.logmsg('INFO', _("Finished automatic backup operation of set '%s'") % self.config.getSetName()) return retval