From 4af68c3d9a6cd14f022ccbc2922bc68afbffac3f Mon Sep 17 00:00:00 2001 From: glmdev Date: Fri, 30 Nov 2018 17:03:41 -0600 Subject: [PATCH] add backup/restore functionality --- do_what.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/do_what.py b/do_what.py index c49a01a..f4ca9fd 100644 --- a/do_what.py +++ b/do_what.py @@ -4,6 +4,7 @@ import subprocess import configparser import magic import json +import datetime mime = magic.Magic(mime=True) @@ -213,6 +214,53 @@ elif ( active and len(sys.argv) > argiter+1 and sys.argv[argiter+1] == 'p' ): exit() +# File/Directory backup creator +# Capital B = Time versioned backup +elif ( active and len(sys.argv) > argiter+1 and sys.argv[argiter+1] == 'B' ): + if ( filearg == '-' ): + print("No file/directory specified to backup.") + elif ( os.path.isfile( path ) ): + bakname = ".bak-"+datetime.datetime.now().strftime("%Y-%B-%d_%I:%M%p") + print("cp "+path+" "+path+bakname) + print("echo Created file backup: "+path+bakname) + elif( os.path.isdir( path ) ): + bakname = ".bak-"+datetime.datetime.now().strftime("%Y-%B-%d_%I:%M%p") + print("cp -r "+path+" "+path+bakname) + print("echo Created directory backup: "+path+bakname) + exit() +# Lowercase b = un-versioned backup +elif ( active and len(sys.argv) > argiter+1 and sys.argv[argiter+1] == 'b' ): + if ( filearg == '-' ): + print("No file/directory specified to backup.") + elif ( os.path.isfile( path ) ): + bakname = ".bak" + print("cp "+path+" "+path+bakname) + print("echo Created file backup: "+path+bakname) + elif( os.path.isdir( path ) ): + bakname = ".bak" + print("cp -r "+path+" "+path+bakname) + print("echo Created directory backup: "+path+bakname) + exit() +# Lowercase r = restore the specified backup +elif ( active and len(sys.argv) > argiter+1 and sys.argv[argiter+1] == 'r' ): + if ( filearg == '-' ): + print("No file/directory specified to restore.") + elif ( os.path.isfile( path ) ): + if ( ".bak" not in path ): + print("echo "+path+" is not a valid backup file.") + else: + restorepath = path.split(".bak", 1)[0] + print("mv "+path+" "+restorepath) + print("echo Restored file: "+restorepath) + elif ( os.path.isdir( path ) ): + if ( ".bak" not in path ): + print("echo "+path+" is not a valid backup directory.") + else: + restorepath = path.split(".bak", 1)[0] + print("mv "+path+" "+restorepath) + print("echo Restored directory: "+restorepath) + exit() + # File Operations if ( (not active and len(sys.argv) == 1) or (active and len(sys.argv) == 2) ): # list the contents of the current directory