Last active
October 10, 2020 00:15
-
-
Save johncarl81/5448e3702a186dbc67064f946aeb49e0 to your computer and use it in GitHub Desktop.
rotating backups in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/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 subprocess.call(['ssh', ssh_host, 'test', '-e', inputfile]) == 0 | |
def remoteDelete(inputfile): | |
print "Deleting remote: {}".format(inputfile) | |
subprocess.call(['ssh', ssh_host, 'rm', '-rf', inputfile]) | |
def remoteMove(fromFile, toFile): | |
print "Moving remote from: {} to: ".format(fromFile, toFile) | |
subprocess.call(['ssh', ssh_host, 'mv', fromFile, toFile]) | |
def remoteUpdateTimestap(inputFile): | |
print "Updating timestamp of: {}".format(inputFile) | |
subprocess.call(['ssh', ssh_host, 'touch', inputFile]) | |
def rsync(backupLink, backupSource, backupDestination): | |
print "Rsyncing..." | |
subprocess.call(['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)) | |
remoteDelete(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)) | |
remoteUpdateTimestap(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