Skip to content

Instantly share code, notes, and snippets.

@joelthelion
Last active December 15, 2015 07:59
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 joelthelion/5227417 to your computer and use it in GitHub Desktop.
Save joelthelion/5227417 to your computer and use it in GitHub Desktop.
Simple script for quick and dirty backups
#!/usr/bin/env python3
# coding: utf-8
# Copyright Joel Schaerer, 2013. Licensed under the GPL, version 3
from datetime import datetime
from sys import argv
import os, shutil
from itertools import count
""" Simple script for quick and dirty backups.
Save all files given on the command line to save_dir.
Will add _n to the filename if it already exists in
the destination dir so as never to overwrite a file.
"""
SAVE_DIR = os.path.expanduser("~/tmp")
def make_name(dirname, basename, extention, cnt=None):
""" Build the name for the backup """
return os.path.join(dirname, "".join((basename,
"_%d" % cnt if cnt else "", ".", extention)))
for f in argv[1:]:
dir, base = os.path.split(f)
# We don't use os.path.splitext because it doesn't handle .tar.gz
name, ext = base.split(".",1)
for n in count():
save_name = make_name(SAVE_DIR, name, ext, n)
if not os.path.exists(save_name):
break
shutil.copy(f, save_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment