While I was working on a related project to use python to write to cloud object storage and the logic around purging; I jotted down some quick and dirty code here for my reference to build on. Normally I would recommend using the excellent restic program but in this case I am forced to use native API's.
This serves as a reminder for it is only a very elementary tar plus gzip daily backup and subsequent purging of old backups. Just a test.
import optparse, os, glob, sys, re, datetime |
optdesc = 'This script is used to manage tar backups of files' |
parser = optparse.OptionParser(description = optdesc,version = os.path.basename(__file__) + ' ' + __version__) |
parser.formatter.max_help_position = 50 |
parser.add_option( '-t' , '--target' , help = 'Specify Target' , dest = 'target' , action = 'append' ) |
parser.add_option( '-f' , '--folders' , help = 'Specify Folders' , dest = 'folders' , action = 'append' ) |
parser.add_option( '-c' , '--create' , help = 'Create a new backup' , dest = 'create' , action = 'store_true' ,default = False ) |
parser.add_option( '-p' , '--purge' , help = 'Purge older backups per policy' , dest = 'purge' , action = 'store_true' ,default = False ) |
parser.add_option( '-g' , '--group' , help = 'Policy group' , dest = 'group' , action = 'append' ) |
parser.add_option( '-l' , '--list' , help = 'List backups' , dest = 'listall' , action = 'store_true' ,default = False ) |
opts, args = parser.parse_args() |
def make_tarfile(output_filename, source_dirs): |
with tarfile. open (output_filename, "w:gz" ) as tar: |
for source_dir in source_dirs: |
tar.add(source_dir, arcname = os.path.basename(source_dir)) |
def getBackupType(backup_time_created): |
utc,mt = str (backup_time_created).split( '.' ) |
d = datetime.datetime.strptime(utc, '%Y-%m-%d %H:%M:%S' ).date() |
dt = d.strftime( '%a %d %B %Y' ) |
elif ( (d.day = = 1 ) and (d.mon = = 1 ) ): |
print ( "Listing backup files.." ) |
files = glob.glob(target + "*DAILY*" ) |
files.sort(key = os.path.getmtime, reverse = True ) |
def purgeBackups(target, group): |
print ( "Purging backup files..this needs testing and more logic for SILVER and BRONZE policies?" ) |
files = glob.glob(target + "*.tgz*" ) |
files.sort(key = os.path.getmtime, reverse = True ) |
if ( ( "DAILY" in file ) or ( "WEEKLY" in file ) or ( "MONTHLY" in file ) or ( "YEARLY" in file ) ): |
sub = re.search( 'files-(.+?)-2019' , file ) |
comment = comment + " this one is more than 7 deleting" |
comment = comment + " this one is more than 4 deleting" |
comment = comment + " this one is more than 12 deleting" |
comment = comment + " this one is more than 5 deleting" |
comment = " manual snapshot not purging" |
if "this one " in comment: |
print ( 'DELETE: {:25}: {:25}' . format ( file , comment) ) |
def createBackup(target, folders, group): |
print ( "creating backup of " + str (folders)) |
hostname = socket.gethostname() |
creationDate = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S.0" ) |
t,ds = getBackupType(creationDate) |
BackupName = target + "/" + hostname + '-files-' + t + "-" + datetime.datetime.now().strftime( "%Y%m%d-%H%MCST" ) + '.tgz' |
proceed = "SNAPSHOT NOT NEEDED AT THIS TIME PER THE POLICY" |
if ( group = = "BRONZE" ) and ( (t = = "MONTHLY" ) or (t = = "YEARLY" ) ): |
proceed = "CLEAR TO SNAP" |
elif ( group = = "SILVER" and (t = = "WEEKLY" ) or (t = = "MONTHLY" ) or (t = = "YEARLY" ) ): |
proceed = "CLEAR TO SNAP" |
proceed = "CLEAR TO SNAP" |
make_tarfile(BackupName, folders) |
print ( "\n\n must specify target folder" ) |
folders = opts.folders[ 0 ].split( ',' ) |
print ( "\n\n must specify folders" ) |
createBackup(target, folders, opts.group[ 0 ]) |
purgeBackups(target, opts.group[ 0 ]) |
if __name__ = = '__main__' : |
And running it like this:
$ python tarBak.py -t /tmp/MyBackups/ -f '/home/rrosso,/var/log/syslog' -g GOLD -c |
creating backup of [ '/home/rrosso' , '/var/log/syslog' ] |
$ python tarBak.py -t /tmp/MyBackups/ -p -g GOLD |
Purging backup files..this needs testing and more logic for SILVER and BRONZE policies? |
DELETE: /tmp/MyBackups/xubuntu32-files-DAILY-20190313-1420CST.tgz: DAILY this one is more than 7 deleting |
0 5 * * * cd /Src/tarBak/ ; python tarBak.py -t /MyBackups/ -f '/home/rrosso,/var/spool/syslog' -c -p -g GOLD 2>&1 |