#!/usr/bin/env python # Back up important files and directories in homedir. # Copyright 2007, 2011 by Akkana Peck; share and enjoy under GPL.v2 # or later. import sys, os, time # Python can't get the hostname without the socket lib! # And from bash, $HOST isn't defined like it is in tcsh. import socket backupto = "/home/YOURNAME/dotback/" # List of what to save, both files and directories. # This isn't my complete list, just a sample. You decide what you need. backupfiles = [ \ ".bashrc", ".bash_profile", ".emacs", ".Xdefaults", ".vim", ".emacs-lisp", ".config/openbox", ".gimp-2.6", # Skip logs and other big stuff from xchat: ".xchat2/colors.conf", ".xchat2/keybindings.conf", ".xchat2/servlist_.conf", ".xchat2/replace.conf", ".xchat2/sound.conf", ".xchat2/urlhandlers.conf", ".xchat2/usermenu.conf", ".xchat2/xchat.conf", # Skip cache and other autogenerated cruft from Mozilla: ".mozilla/firefox/YOURPROFILE/bookmarks.html", ".mozilla/firefox/YOURPROFILE/user.js", ".mozilla/firefox/YOURPROFILE/prefs.js", ".mozilla/firefox/YOURPROFILE/chrome/", ".mozilla/firefox/YOURPROFILE/cookies.txt", ".mozilla/firefox/YOURPROFILE/cookperm.txt", ".mozilla/firefox/YOURPROFILE/localstore.rdf", ".mozilla/firefox/YOURPROFILE/mimeTypes.rdf", ".mozilla/firefox/YOURPROFILE/training.dat", ".mozilla/firefox/YOURPROFILE/places.sqlite", ".mozilla/firefox/YOURPROFILE/signons3.txt", ".mozilla/firefox/YOURPROFILE/signons.sqlite", ".mozilla/firefox/YOURPROFILE/chrome", ] if os.access(backupto, os.W_OK) != 1 : os.mkdir(backupto, 0755) # Make sure it exists now and is a directory: try : os.chdir(backupto) except NameError, e : print "Can't make $backupto" sys.exit(1) # Now chdir to home, which is where we really want to be: os.chdir(os.getenv("HOME")) # Exclude files which aren't there on the current system. # This doesn't use a "for fil in backupfiles" loop because # changing the list from the middle of a for loop confuses Python # and ends up skipping some files that should be removed. i=0 while i < len(backupfiles) : fil = backupfiles[i] if os.access(fil, os.R_OK) != 1 : print "Excluding", fil backupfiles.remove(fil) else : i = i+1 if len(backupfiles) < 1 : print "None of the files exist!" sys.exit(1) # Make a tarball and save it locally host = socket.gethostname() ltime = time.localtime() filenam = "dotfiles-" + host \ + "-" + str(ltime[0]) \ + "-" + str(ltime[1]) + "-" + str(ltime[2]) \ + ".tgz" realcmd = "tar czvf " + backupto + "/" + filenam + ' '.join(backupfiles) print "Running:", realcmd os.system(realcmd) print "Made " + backupto + "/" + filenam