Skip to content

Instantly share code, notes, and snippets.



Last active Oct 10, 2020
What would you like to do?
rotating backups in python
import subprocess
import pipes
import time
ssh_host = 'john@john-nas'
backup_dest = '/media/raid/backup'
backup_source = '/'
backup_exclude = "backupExclude.txt"
backup_name = 'backup'
backup_count = 100
def remoteExists(inputfile):
return['ssh', ssh_host, 'test', '-e', inputfile]) == 0
def remoteDelete(inputfile):
print "Deleting remote: {}".format(inputfile)['ssh', ssh_host, 'rm', '-rf', inputfile])
def remoteMove(fromFile, toFile):
print "Moving remote from: {} to: ".format(fromFile, toFile)['ssh', ssh_host, 'mv', fromFile, toFile])
def remoteUpdateTimestap(inputFile):
print "Updating timestamp of: {}".format(inputFile)['ssh', ssh_host, 'touch', inputFile])
def rsync(backupLink, backupSource, backupDestination):
print "Rsyncing..."['rsync', '-va', '--exclude-from=' + backup_exclude, '--delete', '--link-dest=' + backupLink, backupSource, ssh_host + ':' + backupDestination])
def backupPath(index):
return '{0}/{1}.{2:03d}'.format(backup_dest, backup_name, index)
start = time.time()
print 'Checking if remote exists...'
if remoteExists(backupPath(backup_count)):
print "deleting max backup at {}".format(backupPath(backup_count))
print 'Cycling backup folders...'
for i in reversed(range(1, backup_count+1)):
print "Checking backup {}".format(i)
if remoteExists(backupPath(i-1)):
print 'moving backup {} to {}'.format(backupPath(i - 1), backupPath(i))
remoteMove(backupPath(i - 1), backupPath(i))
rsync(backupPath(1), backup_source, backupPath(0))
print "Backup took: {} seconds".format(time.time() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.