initial commit: 1.5.0
[systemrescuecd.git] / portage-overlay / sys-apps / dumpdisklayout / files / dumpdisklayout
1 #!/usr/bin/python
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
6 import sys,os
7 sys.path.append(os.path.abspath(os.curdir))
8 sys.path.append('/usr/lib/dumpdisklayout/modules/')
9
10 LOGFILE='/var/log/dumpdisklayout.log'
11
12 import re,commands,sys,datetime
13 import mod_checks
14 import mod_filesys
15 import backup, restore
16
17 # -------------------------- utilities functions --------------------------
18 # write a message in the logfile
19 def log_message(line):
20         flog=open(LOGFILE,'a+')
21         flog.write(line)
22         flog.close()
23
24 # -------------------------- main: checks ----------------------------
25 def main_checks():
26         REQPROGS=('sfdisk', 'uname', 'pvdisplay', 'vgdisplay', 'lvdisplay', 'pvcreate', 'vgcreate', 'lvcreate', 'lvm', 'dumpe2fs')
27         REQFILES=('/proc/partitions', '/sys/block')
28         
29         (status, msg)=mod_checks.check_linux26()
30         if status != 0:
31                 print msg
32                 sys.exit(1)
33         
34         (status, msg)=mod_checks.check_requirements(REQPROGS, REQFILES)
35         if status != 0:
36                 print msg
37                 sys.exit(1)
38         
39         (status, msg)=mod_checks.check_lvmprogs()
40         if status != 0:
41                 print msg
42                 sys.exit(1)
43
44 # -------------------------- main: arguments -------------------------
45 def main_usage():
46         print "usage: %s <command> <datafile>" % sys.argv[0]
47         print "   commands: 'backup' || 'restore'"
48         print "   datafile: backup file or '-' for stdin/stdout"
49         sys.exit(1)
50
51 def parse_argv():
52         argc=len(sys.argv)
53         if len(sys.argv) != 3: main_usage()
54         command=sys.argv[1]
55         datpath=sys.argv[2]
56         
57         # process 'command'
58         if not command in  ('backup', 'restore'):
59                 print "%s is not a valid command. expected either 'backup' or 'restore'"%command
60                 sys.exit(1)
61                 
62         # process 'datafile'
63         if datpath=='-':
64                 if command=='backup': datfile=sys.stdout
65                 else: datfile=sys.stdin
66         else:
67                 if command=='backup': datmode='wb'
68                 else: datmode='rb'
69                 try:
70                         datfile=open(datpath, datmode)
71                 except:
72                         print 'cannot open %s'%datpath
73                         sys.exit(1)
74         return (command, datfile)
75
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'))
84         
85         # check that lvm is started
86         if mod_checks.check_lvm_status('available')!=0:
87                 print 'please start the lvm initscript first'
88                 sys.exit(1)
89         
90         for work in actions:
91                 (fct, prefix, desc)=work
92                 datfile.write('# %s\n' % desc);
93                 for line in fct():
94                         datfile.write(prefix+'!'+line+'\n');
95                 datfile.write('\n');
96
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'))
105         hddlist=[]
106         volgroup={}
107         
108         # check that lvm is stopped
109         if mod_checks.check_lvm_status('not available')!=0:
110                 print 'please stop the lvm initscript first'
111                 sys.exit(1)
112         
113         contents=datfile.readlines()
114         for (fct, prefix) in actions:
115                 #(fct, prefix)=work
116                 inlines=[]
117                 for line in contents:
118                         if (re.match('^%s'%prefix, line)):
119                                 inlines.append(line.strip("\r\n")[len(prefix+'!'):])
120                 res=fct(inlines)
121                 if res!=0:
122                         print 'ERROR: function %s returned %s' % (fct, res)
123                         sys.exit(1)
124                 else:
125                         print 'function %s succeeded'%fct
126                 print '\n--------------------------------------------------------\n'
127
128 # -------------------------- backup ------------------------------
129 main_checks()
130 (command, datfile)=parse_argv()
131
132 if command=='backup':
133         main_backup(datfile)
134         datfile.close()
135 elif command=='restore':
136         main_restore(datfile)
137         datfile.close()
138 else:
139         main_usage()
140
141 sys.exit(0)