sysrescue
This script backs up important files to a folder in ~/Desktop of the root user. It appends the time of last access to the filename as well. To add more directories to sysrescue’s list, just put them in file_list in the impFileBakup function. Enjoy !
[sourcecode language=”python”]
#!/usr/bin/python
#Author: Shriphani Palakodety
#filename: sys-rescue.py
from subprocess import *
import shutil
import os
import time
home = os.getenv(’HOME’)
def dirExistsCheck():
”’Create a backup directory in ~/Desktop or do nothing if it already exists.”’
try:
os.mkdir(home + “/Desktop/” + “backup”)
except OSError:
pass
def impFileBakup(home):
”’Back up the important files with the timestamps appended to them”’
dirExistsCheck()
file_list = [ “/etc/fstab”, “/etc/X11/xorg.conf”, “/boot/grub/menu.lst”, “/etc/apt/sources.list” ]
for dir in file_list:
secs_since_epoch = os.stat(dir)[9]
dest = home + “/Desktop/” + “backup/” + dir.split(”/”)[-1] + “-” + str(time.localtime(secs_since_epoch)[0]) + “-” + str(time.localtime(secs_since_epoch)[1]) + “-” + str(time.localtime(secs_since_epoch)[2]) + “-” + str(time.localtime(secs_since_epoch)[3]) + “:” + str(time.localtime(secs_since_epoch)[4]) + “:” + str(time.localtime(secs_since_epoch)[5])
shutil.copyfile(dir, dest)
crap = Popen([”fdisk”, “-l”], stdout=PIPE)
output = crap.communicate()
partition_table = output[0]
reqd_partition_table = partition_table.split(’\n’)[6:]
def deviceName(reqd_partition_table):
”’Get the name of the hard-disk”’
for line in reqd_partition_table:
list_of_words = line.split()
try:
if list_of_words[1] == ‘*’:
device = list_of_words[0]
device_really_wanted = device[0:8]
return device_really_wanted
except IndexError:
continue
def mbrBakup(device):
”’Back-up the Master Boot Record”’
print device
a = Popen([”dd”, “if=”+device, “of=” + home + “/Desktop/backup/” + “mbr.bakup” + “-” + str(time.localtime()[0]) + “-” + str(time.localtime()[1]) + “-” + str(time.localtime()[2]) + “-” + str(time.localtime()[3]) + “:” + str(time.localtime()[4]) + “:” + str(time.localtime()[5]), “bs=446″, “count=1″], stderr=PIPE, stdout=PIPE)
output = a.communicate()
errors = output[0]
print errors
print “Done”
mbrBakup(deviceName(reqd_partition_table))
impFileBakup(home)
[/sourcecode]


