def task_restore(self, localfile, restoreDb=True, withAttachments=True): """ Restore all information not stored in version control from a tarball on the invoking users machine. """ restoreDb = str(restoreDb).lower() in ('true', '1', 'yes', 'ok', 'y') if restoreDb: msg = ( 'All existing files present in the backup will be overwritten and\n' 'the database dropped and recreated.') else: msg = ( 'All existing files present in the backup will be overwritten\n' '(the database will not be touched).') print('') if confirm(msg): # TODO: Ask for confirmation here if restoreDb: postgres.dropDb('trac') postgres.createDb('trac', 'trac') with settings(user=self.serviceUser): with utils.tempfile() as temp: files = { 'db.dump': temp, } if withAttachments is True: files['attachments'] = 'attachments' archive.restore(files, localfile) if restoreDb: postgres.restoreFromPath('trac', temp)
def task_dump(self, localfile, withAttachments=True): """ Create a tarball containing all information not currently stored in version control and download it to the given C{localfile}. C{localfile} is a path on your local system. Keep it in sync with task_restore. For it to work you need your SSH public key added to /srv/trac/.ssh/authorized_keys """ with settings(user=self.serviceUser): with utils.tempfile() as temp: postgres.dumpToPath('trac', temp) files = { 'db.dump': temp, } if withAttachments is True: #files['attachments'] = 'attachments' files['trac-attachments'] = 'config/trac-env/files/attachments' archive.dump(files, localfile)
def restore(spec, localfile): with utils.tempfile(uploadFrom=localfile) as tar: cmd = [ '/bin/tar', '--extract', '--file={}'.format(tar), '--verbose', '--show-transformed-names', ] with hide('output'): pwd = run('pwd') for source, destination in spec.iteritems(): dirname, basename = os.path.split(destination) # Each dirname has to be absolute, otherwise it refers to the # previously set --directory. dirname = os.path.join(pwd, dirname) cmd.extend([ '\\\n', ' --directory {:20s}'.format(dirname), '{:15s}'.format(source), '--transform', quote('s!^{}!{}!'.format(source, basename)), ]) run(' '.join(cmd))
def task_restore(self, localfile, restoreDb=True): """ Restore all information not stored in version control from a tarball on the invoking users machine. """ restoreDb = str(restoreDb).lower() in ('true', '1', 'yes', 'ok', 'y') if restoreDb: msg = ( 'All existing files present in the backup will be overwritten and\n' 'the database dropped and recreated.' ) else: msg = ( 'All existing files present in the backup will be overwritten\n' '(the database will not be touched).' ) print '' if confirm(msg): # TODO: Ask for confirmation here if restoreDb: postgres.dropDb('trac') postgres.createDb('trac', 'trac') with settings(user=self.serviceUser): with utils.tempfile() as temp: archive.restore({ 'htpasswd': 'config/htpasswd', 'attachments': 'attachments', 'db.dump': temp, }, localfile) if restoreDb: postgres.restoreFromPath('trac', temp)
def task_dump(self, localfile): """ Dump codespeed database and download it to the given C{localfile}. """ with settings(user=self.serviceUser): with utils.tempfile() as temp: run('/usr/bin/sqlite3 ~/data/codespeed.db .dump >{}'.format(temp)) archive.dump({ 'db.dump': temp, }, localfile)
def dump(database, dumpPath, user=None): """ Download a dump of the specified database to C{dumpPath}. This has to be executed as a user with enough privileges on the selected database. Alternatively a user can be manually provided. """ if user is None: user = env.user with settings(user=user): with utils.tempfile(saveTo=dumpPath) as temp: dumpToPath(database, temp)
def task_restore(self, localfile): """ Restore codespeed database from the given C{localfile}. """ msg = 'The whole database will be replaced with the backup.' if utils.confirm(msg): with settings(user=self.serviceUser): with utils.tempfile() as temp: archive.restore({ 'db.dump': temp, }, localfile) run('/bin/rm -f ~/data/codespeed.db') run('/usr/bin/sqlite3 ~/data/codespeed.db ".read {}"'.format(temp))
def task_dump(self, localfile): """ Create a tarball containing all information not currently stored in version control and download it to the given C{localfile}. """ with settings(user=self.serviceUser): with utils.tempfile() as temp: postgres.dumpToPath('trac', temp) archive.dump({ 'htpasswd': 'config/htpasswd', 'attachments': 'attachments', 'db.dump': temp, }, localfile)
def task_dump(self, localfile, withAttachments=True): """ Create a tarball containing all information not currently stored in version control and download it to the given C{localfile}. """ with settings(user=self.serviceUser): with utils.tempfile() as temp: postgres.dumpToPath('trac', temp) files = { 'db.dump': temp, } if withAttachments is True: files['attachments'] = 'attachments' archive.dump(files, localfile)
def restore(database, dumpPath, user=None, clean=False): """ Upload a local dump and restore it to the named database. If no user is specified, set the owner to the current active SSH user. This function only works for postgres users which have a corresponding system user. If clean is specified, the database will be dropped and recreated. The database will always be created if it does not exits. """ if user is None: user = env.user if clean: dropDb(database) createDb(database, user) with settings(user=user): with utils.tempfile(uploadFrom=dumpPath) as temp: restoreFromPath(database, temp)
def dump(spec, localfile, exclude=()): """ C{spec} is a dictionary of filenames/dirnames in the tarball to locations on the disk from where the contents have to be retrieved. Home relative (~/...) file names are not supported by the transformation rules as they are converted by the shell before being passed to tar. The C{exclude} argument is a list of filename patterns to exclude from the dump. """ _, ext = os.path.splitext(localfile) with utils.tempfile(suffix=ext) as temp: cmd = [ '/bin/tar', '--create', '--file={}'.format(temp), '--auto-compress', '-h', # Follow symbolic links '--verbose', '--show-transformed-names', ] cmd += ['--exclude={}'.format(quote(e)) for e in exclude] for destination, source in spec.iteritems(): cmd.extend([ '\\\n', ' {:30s}'.format(source), '--transform', quote('s!^{}!{}!'.format(source.lstrip('/'), destination)), ]) run(' '.join(cmd)) get(temp, localfile)
def setDebconfValue(package, question, type, value): selections = StringIO(' '.join([package, question, type, value])) with tempfile(selections) as controlFile: sudo('/usr/bin/debconf-set-selections {}'.format(controlFile))
def installEquiv(package, provides, description=None): control = _generateControlFile(package, provides, description) with tempfile(control) as controlFile, cd('/root'): sudo('/usr/bin/equivs-build {}'.format(controlFile)) sudo('/usr/bin/dpkg -i {}_1.0_all.deb'.format(package))