Skip to content

Instantly share code, notes, and snippets.

@motleytech
Last active December 5, 2018 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motleytech/131861b23683ca6d79ee104ebb724040 to your computer and use it in GitHub Desktop.
Save motleytech/131861b23683ca6d79ee104ebb724040 to your computer and use it in GitHub Desktop.
Rsync like functionality via python and cp - much faster for certain purposes
# script to smartly backup files using cp rather than rsync
import sys
import os
import exceptions
from pprint import pprint as pp
def removeFile(fp):
os.remove(fp)
def removeDir(dp):
if dp != '/':
os.system('rm -rf \'%s\'' % dp)
else:
print('Bad path : %s' % dp)
def copy(sf, df):
print ('Copying \'%s\' -> \'%s\'' % (sf, df))
os.system("cp -p '%s' '%s'" % (sf, df))
def mkdir(dr):
print ('Creating dir %s' % dr)
os.mkdir(dr)
def shouldCopyFile(sf, df):
# if dest does not exist
if not os.path.exists(df):
print('Should copy : %s does not exist' % df)
return True
# if dest size is different
if os.path.getsize(sf) != os.path.getsize(df):
print('Should copy : %s size different' % df)
return True
# if dest modification time is different
if abs(int(os.path.getmtime(sf)) - int(os.path.getmtime(df))) > 1:
print('Should copy : %s mod time different' % df)
print('ST : %s, DT : %s' % (os.path.getmtime(sf), os.path.getmtime(df)))
return True
print('DONT copy : %s' % sf)
return False
def smartCopy(src, dest):
if not os.path.exists(src):
raise Exception(exceptions.LookupError, src)
if not os.path.exists(dest):
raise Exception(exceptions.LookupError, dest)
for fd in os.listdir(src):
sf, df = os.path.join(src, fd), os.path.join(dest, fd)
if os.path.isdir(sf):
if not os.path.exists(df):
mkdir(df)
elif not os.path.isdir(df):
removeFile(df)
mkdir(df)
smartCopy(sf, df)
else:
if os.path.isdir(df):
removeDir(df)
if shouldCopyFile(sf, df):
copy(sf, df)
def checkSrcDest(src, dest):
if not os.path.exists(src):
print('Src path does not exist.\n%s' % src)
return False
if not os.path.isdir(src):
print('Src must be a directory.')
return False
if not os.path.exists(os.path.split(dest)[0]):
print('Parent folder for Dest %s does not exist. Stop' % dest)
return False
else:
if not os.path.exists(dest):
mkdir(dest)
return True
def main():
src, dest = sys.argv[1:]
src, dest = map(os.path.abspath, (src, dest))
print('Sync %s -> %s' % (src, dest))
if not checkSrcDest(src, dest):
return
try:
smartCopy(src, dest)
except LookupError as e:
print ('Path not found')
print('Done')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment