Skip to content

Instantly share code, notes, and snippets.

@Neo-Oli
Last active April 6, 2017 08:47
Show Gist options
  • Save Neo-Oli/817dcbb87e76deb5630ae808452d19a0 to your computer and use it in GitHub Desktop.
Save Neo-Oli/817dcbb87e76deb5630ae808452d19a0 to your computer and use it in GitHub Desktop.
email backup. Parts of my backup system
def mkdir(path):
if not os.path.isdir(path):
os.makedirs(path)
def ln(source,target):
target=target.replace(":","%%%%%")
# print("{} => {}".format(source,target))
if os.path.isfile(target):
os.remove(target)
os.symlink(source,target)
def createfolders(relpath,path,bp):
oldpath="{}/{}".format(path,relpath)
newpath="{}/{}".format(bp,relpath)
# print(oldpath)
# print(newpath)
mkdir(newpath)
contents = os.listdir(oldpath)# if f.is_dir() ]█
for f in contents:
newrelpath="{}/{}".format(relpath,f)
oldabspath="{}/{}".format(path,newrelpath)
# print(newrelpath)
# print(oldabspath)
if os.path.isdir(oldabspath):
createfolders(newrelpath,path,bp)
else:
source=oldabspath
target="{}/{}".format(bp,newrelpath)
ln(source,target)
def mailbackup(bp):
path="~"
path=os.path.expanduser(path)
relpath="mail"
createfolders(relpath, path,bp)
def run(command):
output=subprocess.call(['sh', '-c', command])
if os.path.isdir(os.path.expanduser("~/mail")):
comment("creating symlinked folder for rsync.")
#fix this shit if there is no more need to replace the : in filenames
bp="/data/data/com.termux/files/usr/tmp/mailbackuptemp"
mailbackup(bp)
comment("Copying mails to drive")
run("rsync {}/mail/ ~/drive/mail -aLv --delete --progress --size-only".format(bp))
comment("remove temporary directory")
shutil.rmtree(bp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment