2 # dumpdisklayout: system backup including disk layout and contents
3 # backup/restore disk layout (msdos-disklabel + lvm-layout) to a text file
4 # Copyright 2007 Francois Dupoux
5 # Distributed under the terms of the GNU General Public License v2
7 sys.path.append(os.path.abspath(os.curdir))
8 sys.path.append('/usr/lib/dumpdisklayout/modules/')
10 LOGFILE='/var/log/dumpdisklayout.log'
12 import re,commands,sys,datetime
15 import backup, restore
17 # -------------------------- utilities functions --------------------------
18 # write a message in the logfile
19 def log_message(line):
20 flog=open(LOGFILE,'a+')
24 # -------------------------- main: checks ----------------------------
26 REQPROGS=('sfdisk', 'uname', 'pvdisplay', 'vgdisplay', 'lvdisplay', 'pvcreate', 'vgcreate', 'lvcreate', 'lvm', 'dumpe2fs')
27 REQFILES=('/proc/partitions', '/sys/block')
29 (status, msg)=mod_checks.check_linux26()
34 (status, msg)=mod_checks.check_requirements(REQPROGS, REQFILES)
39 (status, msg)=mod_checks.check_lvmprogs()
44 # -------------------------- main: arguments -------------------------
46 print "usage: %s <command> <datafile>" % sys.argv[0]
47 print " commands: 'backup' || 'restore'"
48 print " datafile: backup file or '-' for stdin/stdout"
53 if len(sys.argv) != 3: main_usage()
58 if not command in ('backup', 'restore'):
59 print "%s is not a valid command. expected either 'backup' or 'restore'"%command
64 if command=='backup': datfile=sys.stdout
65 else: datfile=sys.stdin
67 if command=='backup': datmode='wb'
70 datfile=open(datpath, datmode)
72 print 'cannot open %s'%datpath
74 return (command, datfile)
76 def main_backup(datfile):
77 actions=((backup.dump_sysinfo, 'SI', 'System Information'),
78 (backup.dump_disks, 'HD', 'Physical Hard-disks'),
79 (backup.dump_sfdisk, 'SF', 'Partitions layout made by sfdisk'),
80 (backup.dump_pv, 'PV', 'LVM Physical Volumes'),
81 (backup.dump_vg, 'VG', 'LVM Volume Groups'),
82 (backup.dump_lv, 'LV', 'LVM Logical Volumes'),
83 (backup.dump_fs, 'FS', 'File Systems'))
85 # check that lvm is started
86 if mod_checks.check_lvm_status('available')!=0:
87 print 'please start the lvm initscript first'
91 (fct, prefix, desc)=work
92 datfile.write('# %s\n' % desc);
94 datfile.write(prefix+'!'+line+'\n');
97 def main_restore(datfile):
98 actions=((restore.check_filefmt, 'SI'),
99 (restore.check_physhdd, 'HD'),
100 (restore.rest_sfdisk, 'SF'),
101 (restore.rest_pv, 'PV'),
102 (restore.rest_vg, 'VG'),
103 (restore.rest_lv, 'LV'),
104 (restore.rest_fs, 'FS'))
108 # check that lvm is stopped
109 if mod_checks.check_lvm_status('not available')!=0:
110 print 'please stop the lvm initscript first'
113 contents=datfile.readlines()
114 for (fct, prefix) in actions:
117 for line in contents:
118 if (re.match('^%s'%prefix, line)):
119 inlines.append(line.strip("\r\n")[len(prefix+'!'):])
122 print 'ERROR: function %s returned %s' % (fct, res)
125 print 'function %s succeeded'%fct
126 print '\n--------------------------------------------------------\n'
128 # -------------------------- backup ------------------------------
130 (command, datfile)=parse_argv()
132 if command=='backup':
135 elif command=='restore':
136 main_restore(datfile)