Rcjp's Weblog

September 5, 2006

Backup script

Filed under: python, unix, utils — rcjp @ 12:45 pm

Just a handy bit of code to backup a file.

#!/usr/bin/env python

import os, sys
from shutil import copyfile

def backup(filename):
        i = 1
        while os.path.exists('%s.old%d' % (filename,i)):
                i+=1
        backup = '%s.old%d' % (filename,i)
        print "Backing up %s to %s\n" % (filename, backup)
        copyfile(filename, backup)

if len(sys.argv) != 2:
        print "Usage: backup filename"
else:
        try:
                backup(sys.argv[1])
        except IOError, val:
                print "*** Backup Failed : %s ***\n" % val

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: