sys: new luksheaders option to backup the Luks header of every Luks device
[matthijs/upstream/backupninja.git] / handlers / sys.in
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # this handler will save various reports of vital system information.
4 # by default, all the reports are saved in /var/backups.
5 #
6 # (1) a capture of the debconf package selection states. This file
7 #     can be used to restore the answers to debconf questions for
8 #     packages that you will be installing through (2) below. To
9 #     do this, run: "debconf-set-selections < debconfsel.txt"
10 #
11 # (2) a list of all the packages installed and removed.
12 #     this file can be used to restore the state of installed packages
13 #     by running "dpkg --set-selections < dpkg-selections.txt and
14 #     then run "apt-get -u dselect-upgrade". If you have the 
15 #     debconf-set-selections file from (1), you should restore those first.
16
17 # (3) the partition table of all disks. 
18 #     this partition table can be used to format another disk of
19 #     the same size. this can be handy if using software raid and 
20 #     you have a disk go bad. just replace the disk and partition it
21 #     by running "sfdisk /dev/sdb < partitions.sdb.txt"
22 #     (MAKE SURE YOU PARTITION THE CORRECT DISK!!!)
23 #
24 # (4) hardware information. 
25 #     write to a text file the important things which hwinfo can gleen.
26 #
27 # (5) the Luks header of every Luks block device, if option luksheaders
28 #     is enabled.
29 #     in case you (have to) scramble such a Luks header (for some time),
30 #     and restore it later by running "dd if=luksheader.sda2.bin of=/dev/sda2"
31 #     (MAKE SURE YOU PASS THE CORRECT DEVICE AS of= !!!)
32 #
33
34 if [ -f /etc/debian_version ]
35 then
36    os=debian
37    debug "Debian detected"
38    osversion="/etc/debian_version"
39 elif [ -f /etc/redhat-release ]
40 then
41    os=redhat
42    debug "Redhat detected"
43    osversion="/etc/redhat-release"
44 else
45    warning "Unknown OS detected!"
46 fi
47
48 getconf parentdir /var/backups
49 getconf packages yes
50 getconf dosfdisk yes
51 getconf dohwinfo yes
52
53 if [ ! -d $parentdir ]; then
54    mkdir -p $parentdir
55 fi
56    
57 if [ $os = "debian" ]
58 then
59    getconf packagesfile $parentdir/dpkg-selections.txt
60    getconf packagemgr   `which dpkg`
61    getconf packagemgroptions ' --get-selections *'
62    getconf selectionsfile $parentdir/debconfsel.txt
63    getconf debconfgetselections `which debconf-get-selections`
64 elif [ $os = "redhat" ]
65 then
66    getconf packagesfile  $parentdir/rpmpackages.txt 
67    getconf packagemgr   `which rpm`
68    getconf packagemgroptions    ' -qa '
69
70    getconf SYSREPORT `which sysreport`
71    getconf sysreport_options ' -norpm '
72 else
73    getconf packagesfile $parentdir/unknownOS.txt
74 fi
75 packagemgroptions="${packagemgroptions//__star__/*}"
76
77 getconf partitions yes
78 getconf partitionsfile $parentdir/partitions.__star__.txt
79
80 getconf hardware yes
81 getconf hardwarefile $parentdir/hardware.txt
82
83 getconf sysreport yes
84 getconf sysreportfile $parentdir/sysreport.txt
85
86 getconf SFDISK `which sfdisk`
87 getconf HWINFO `which hwinfo`
88 getconf sfdisk_options ""
89 getconf hwinfo_options ""
90
91 getconf CRYPTSETUP `which cryptsetup`
92 getconf DD `which dd`
93 getconf luksheaders no
94 getconf luksheadersfile $parentdir/luksheader.__star__.bin
95
96 getconf vsnames all
97
98 # If vservers are configured, check that the ones listed in $vsnames are running.
99 local usevserver=no
100 if [ $vservers_are_available = yes ]; then
101    if [ "$vsnames" = all ]; then
102       vsnames="$found_vservers"
103    fi
104    if ! vservers_running "$vsnames" ; then
105       fatal "At least one of the vservers listed in vsnames ($vsnames) is not running."
106    fi
107    info "Using vservers '$vsnames'"
108    usevserver=yes
109 fi
110
111 ## SANITY CHECKS #########################
112
113 if [ "$luksheaders" == "yes" ]; then
114    if [ ! -x "$DD" ]; then
115       warning "can't find dd, skipping backup of Luks headers."
116       luksheaders="no"
117    fi
118    if [ ! -x "$CRYPTSETUP" ]; then
119       warning "can't find cryptsetup, skipping backup of Luks headers."
120       luksheaders="no"
121    fi
122 fi
123
124 ## PACKAGES ##############################
125
126 #
127 # here we grab a list of the packages installed and removed.
128 #
129
130 if [ "$packages" == "yes" ]; then
131
132    if [ $usevserver = yes ]; then
133       info "vserver root directory set to: $VROOTDIR"
134       for vserver in $vsnames; do
135          info "examining vserver: $vserver"
136          # is it running ?
137          vservers_running $vserver
138          if [ $? -ne 0 ]; then
139             warning "The vserver $vserver is not running."
140             continue
141          fi
142          # is $packagemgr available inside $vserver ?
143          if [ ! -x "${VROOTDIR}/${vserver}${packagemgr}" ]; then
144             warning "can't find $packagemgr in vserver $vserver, skipping installed packages report."
145          else
146             # don't expand * since it can be used in $packagemgroptions
147             set -o noglob
148             debug "$VSERVER $vserver exec $packagemgr $packagemgroptions > $VROOTDIR/$vserver$packagesfile"
149             $VSERVER $vserver exec $packagemgr $packagemgroptions > $VROOTDIR/$vserver$packagesfile || fatal "can not save $packagemgr info to $packagesfile"
150             set +o noglob
151          fi
152          # is $debconfgetselections available inside $vserver ?
153          found=no
154          # case #1: it is available on the host, is it available inside $vserver ?
155          if [ -n "$debconfgetselections" ]; then
156             [ -x "${VROOTDIR}/${vserver}${debconfgetselections}" ] && found=yes
157          # case #2: it is not available on the host, is it available inside $vserver ?
158          else
159             [ -n "`$VSERVER $vserver exec which debconf-get-selections`" ] && found=yes
160          fi
161          if [ "$found" != yes ]; then
162             warning "can't find debconf-get-selections in vserver $vserver, skipping package selection states. You may want to install the debconf-utils package."
163          else
164             debug "$VSERVER $vserver exec $debconfgetselections > $VROOTDIR/$vserver$selectionsfile"
165             $VSERVER $vserver exec $debconfgetselections > $VROOTDIR/$vserver$selectionsfile || fatal "can not save debconf-get-selections info to $selectionsfile"
166          fi
167          unset found
168       done
169    fi
170    
171    # We want to perform this on the host as well
172    if [ -z "$packagemgr" -o ! -x "$packagemgr" ]; then 
173       warning "can't find ${packagemgr}, skipping installed packages report."
174    else
175       # don't expand * since it can be used in $packagemgroptions
176       set -o noglob
177       debug "$packagemgr $packagemgroptions > $packagesfile"
178       $packagemgr $packagemgroptions > $packagesfile || fatal "can not save $packagemgr info to $packagesfile"
179       set +o noglob
180    fi
181    if [ -z "$debconfgetselections" ]; then
182       warning "can't find debconf-get-selections, skipping package selection states. You might want to install the debconf-utils package."
183    else
184       debug "$debconfgetselections > $selectionsfile"
185       $debconfgetselections > $selectionsfile || fatal "can not save $debconfgetselections info to $selectionsfile"
186    fi
187 fi
188
189 ## System report ##############################
190
191 #
192 # here we grab a bunch of system stuff for a report
193 #
194
195 export STATUS
196
197 HASHES="#################################################################"
198 DASHES="-----------------------------------------------------------------"
199
200 cat /dev/null > $sysreportfile || fatal "can not write to $sysreportfile"
201
202
203 catiffile () {
204    echo $HASHES >> $sysreportfile
205    echo "# $STATUS" >> $sysreportfile
206    echo $HASHES >> $sysreportfile
207    if [ -f $1 ]; then
208       echo "file: $1" >> $sysreportfile
209       echo $DASHES >> $sysreportfile
210       cat $1 >> $sysreportfile 2>&1 || info "reading of $1 failed"
211    fi
212    if [ -d $1 ]; then
213       echo "directory: $1" >> $sysreportfile
214       echo $DASHES >> $sysreportfile
215       for file in `find $1 -maxdepth 3 -noleaf -type f`
216       do
217        catiffile $file
218       done
219    fi
220    echo $DASHES >> $sysreportfile
221
222
223 catifexec () {
224    if [ -x $1 ]; then
225       echo $HASHES >> $sysreportfile
226       echo "# $STATUS" >> $sysreportfile
227       echo $HASHES >> $sysreportfile
228       $*  >> $sysreportfile 2>&1 || info "executing of $1 failed"
229    fi
230 }
231    
232
233 STATUS="Determining $os version:"
234 catiffile $osversion
235
236 STATUS="Determinding your current hostname: " 
237 catifexec "/bin/hostname"
238
239 STATUS="Getting the date:"
240 catifexec "/bin/date"
241
242 STATUS="Checking your systems current uptime and load average:"
243 catifexec "/usr/bin/uptime"
244
245 STATUS="Checking available memory:"
246 catifexec "/usr/bin/free"
247
248 STATUS="Checking free disk space:"
249 catifexec "/bin/df" "-al"
250
251 STATUS="Collecting what services run at what run level:"
252 if [ $os = "redhat" ]; then
253    catifexec "/sbin/chkconfig" "--list"
254    STATUS="Collecting information about /etc/rc.d:"
255    catiffile "/bin/ls /etc/rc.d/rc*.d/"
256
257 elif [ $os = "debian" ]; then
258     for level in 0 1 2 3 4 5 6 S; do
259        echo "Level: $level" >> $sysreportfile
260        for f in /etc/rc${level}.d/*; do
261         # Remove /etc/Knn or Snn from beginning
262           ff=$(echo $f | @SED@ 's_/etc/rc..d/[KS][0-9][0-9]__')
263           if [ $f != $ff ]; then
264              echo $ff >> $sysreportfile
265           fi
266        done
267        echo "" >> $sysreportfile
268     done
269 fi
270
271 STATUS="Getting bootloader information:"
272 catifexec "/bin/ls" "-alR /boot"
273
274 # This covers sparc, alpha, and intel (respectively)
275 # updated for grub -mpg
276 if [ -f /etc/silo.conf ]; then
277   STATUS="Collecting information about the boot process (silo):"
278   catiffile "/etc/silo.conf"
279 fi
280 if [ -f /etc/milo.conf ]; then
281   STATUS="Collecting information about the boot process (milo):"
282   catiffile "/etc/milo.conf"
283 fi
284 if [ -f /etc/lilo.conf ]; then
285   STATUS="Collecting information about the boot process (lilo):"
286   catiffile "/etc/lilo.conf"
287   catifexec "/sbin/lilo" "-q"
288 fi
289 if [ -d /boot/grub -a -f /boot/grub/grub.conf -a -f /boot/grub/device.map ]; then
290   STATUS="Collecting information about the boot process (grub.conf):"
291   catiffile "/boot/grub/grub.conf"
292   STATUS="Collecting information about the boot process (grub.map):"
293   catiffile "/boot/grub/device.map"
294 fi
295 if [ -f /etc/cluster.conf -o -f /etc/cluster.xml ] ; then
296   STATUS="Gathering information on cluster setup"
297   # 2.1 AS
298   if [ -f /etc/cluster.conf ] ; then
299     catiffile "/etc/cluster.conf"
300   fi
301   # Taroon
302   if [ -f /etc/cluster.xml ] ; then
303     catiffile "/etc/cluster.xml"
304   fi
305 fi
306
307 STATUS="Gathering sysctl information (sysctl -a):"
308 catiffile "sysctl -a 2>/dev/null"
309 STATUS="Gathering sysctl information (/etc/sysctl.conf):"
310 catiffile "/etc/sysctl.conf"
311
312 STATUS="Gathering IP information (/sbin/ifconfig):"
313 catifexec "/sbin/ifconfig" "-a"
314
315 STATUS="Gathering additional IP information (/bin/ip addr list):"
316 catifexec "/bin/ip" "addr list"
317
318 STATUS="Checking network routes:"
319 catifexec "/sbin/route" "-n"
320
321 STATUS="Collecting Name Service Switch config information:"
322 catiffile "/etc/nsswitch.conf"
323
324 STATUS="Collecting information about system authentication (pam):"
325 catiffile "/etc/pam.conf"
326 catiffile "/etc/pam.d"
327
328 echo
329 echo "Getting information about the kernel."
330 echo
331 STATUS="Getting kernel version:"
332 catifexec "/bin/uname" "-a"
333 STATUS="Checking module information:"
334 catifexec "/sbin/lsmod"
335 for x  in $(/sbin/lsmod | /bin/cut -f1 -d" " 2>/dev/null | /bin/grep -v Module 2>/dev/null 
336 ) ; do
337   STATUS="Checking module information $x:"
338   catifexec "/sbin/modinfo" "$x"
339 done
340
341 STATUS="Gathering information about your filesystems:"
342 catiffile "/proc/filesystems"
343
344 STATUS="Gathering information about your system stat:"
345 catiffile "/proc/stat"
346
347 STATUS="Gathering information about your partitions:"
348 catiffile "/proc/partitions"
349
350 STATUS="Gathering information about your ksyms:"
351 catiffile "/proc/kallsyms"
352
353 STATUS="Gathering information about slabinfo:"
354 catiffile "/proc/slabinfo"
355
356 # Added support to cover for the new modules.conf layout in Red Hat 7
357 STATUS="Collecting information regarding kernel modules"
358 VER=`uname -r`
359 catiffile "/lib/modules/$VER/modules.dep"
360 if [ -f /etc/conf.modules ]; then
361   STATUS="Collecting information regarding kernel modules (conf.modules)"
362   catiffile "/etc/conf.modules"
363 fi
364 if [ -f /etc/modules.conf ]; then
365   STATUS="Collecting information regarding kernel modules (modules.conf)"
366   catiffile "/etc/modules.conf"
367 fi
368 if [ -f /etc/modprobe.conf ]; then
369   STATUS="Collecting information regarding kernel modules (modeprobe.conf)"
370   catiffile "/etc/modprobe.conf"
371 fi
372
373 # dkms status
374 if [ -x /usr/sbin/dkms ] ; then
375    STATUS="Gathering current status of modules, versions and kernels (dkms):"
376   catifexec "/usr/sbin/dkms" "status"
377 fi
378
379 if [ -f /etc/sysconfig/isdncard ] ; then
380   STATUS="Gathering information about ISDN:"
381   catiffile "/etc/sysconfig/isdncard"
382 fi
383
384 STATUS="Collecting information from the proc directory:"
385 catiffile "/proc/pci"
386
387 STATUS="Getting kernel command line"
388 catiffile "/proc/cmdline"
389
390 STATUS="Gathering information about your CPU:"
391 catiffile "/proc/cpuinfo"
392
393 STATUS="Gathering information about your Ram:"
394 catiffile "/proc/meminfo"
395
396 STATUS="Gathering information about your ioports:"
397 catiffile "/proc/ioports"
398
399 STATUS="Gathering information about your interrupts:"
400 catiffile "/proc/interrupts"
401
402 STATUS="Gathering information about your scsi devices:"
403 catiffile "/proc/scsi"
404
405 STATUS="Gathering information about your dma:"
406 catiffile "/proc/dma"
407
408 STATUS="Gathering information about your devices (/proc/devices):"
409 catiffile "/proc/devices"
410
411 STATUS="Gathering information about your rtc:"
412 catiffile "/proc/rtc"
413
414 STATUS="Gathering information about your ide drivers:"
415 catiffile "/proc/ide"
416
417 STATUS="Gathering information about your bus:"
418 catifexec "/usr/bin/lspci"
419 catiffile "/proc/bus"
420
421 echo
422 echo "Getting disk and filesystem information."
423 echo
424
425 STATUS="Collecting information from /etc/fstab:"
426 catiffile "/etc/fstab"
427
428 STATUS="Collecting disk partition information:"
429 catifexec "/sbin/fdisk" "-l"
430
431 STATUS="Checking mounted file systems (mount) "
432 catifexec "/bin/mount"
433
434 STATUS="Checking mounted file systems (/proc/mounts)"
435 catiffile "/proc/mounts"
436
437 STATUS="Collecting Software RAID information (/proc/mdstat)"
438 catiffile "/proc/mdstat"
439
440 STATUS="Collecting Software RAID information (/etc/raidtab)"
441 catiffile "/etc/raidtab"
442
443 STATUS="Collecting Software RAID information (/etc/mdadm.conf)"
444 catiffile "/etc/mdadm.conf"
445
446 STATUS="Collecting Software RAID information (/sbin/mdadm -Q)"
447 catifexec "/sbin/mdadm" "-Q" "--detail" '/dev/md?*'
448
449 STATUS="Collecting Automount information (auto.master)"
450 catiffile "/etc/auto.master"
451
452 STATUS="Collecting Automount information (auto.misc)"
453 catiffile "/etc/auto.misc"
454
455 STATUS="Collecting Automount information (auto.net)"
456 catiffile "/etc/auto.net"
457
458 STATUS="Collecting LVM information:"
459 if [ $os = "redhat" ]; then
460    catifexec "/usr/sbin/vgdisplay" "-vv"
461 elif [ $os = "debian" ]; then
462    catifexec "/sbin/vgdisplay" "-vv"
463 fi
464    
465 STATUS="Collecting SCSI Tape information (/etc/stinit.def)"
466 catiffile "/etc/stinit.def"
467
468 if [ -x /sbin/lsusb ] ; then
469   STATUS="Collecting USB devices list (lsusb):"
470   catifexec "/sbin/lsusb"
471 fi
472
473 if [ -x /usr/bin/lshal ] ; then
474   STATUS="Collecting global devices list (lshal):"
475   catifexec "/usr/bin/lshal"
476 fi
477
478
479 STATUS="Gathering information on SELinux setup"
480 catifexec "/usr/bin/selinuxconfig"
481 catifexec "/usr/sbin/sestatus"
482 if [ $os = "redhat" ]; then
483    catifexec "rpm" "-q -V selinux-policy-targeted"
484    catifexec "rpm" "-q -V selinux-policy-strict"
485 fi
486
487 if [ $usevserver = yes ]; then
488    STATUS="Gathering vserver information"
489    catiffile "/proc/virtual"
490 fi
491
492 if [ "$partitions" == "yes" ]; then
493    if [ "$dosfdisk" == "yes" ]; then
494         if [ ! -x "$SFDISK" ]; then
495                 warning "can't find sfdisk, skipping sfdisk report."
496                 partitions="no"
497         fi
498    fi
499    if [ "$dohwinfo" == "yes" ]; then
500         if [ ! -x "$HWINFO" ]; then
501                 warning "can't find hwinfo, skipping partition report."
502                 partitions="no"
503         fi
504    fi
505 fi
506
507 if [ "$hardware" == "yes" ]; then
508         if [ ! -x "$HWINFO" ]; then
509                 warning "can't find hwinfo, skipping hardware report."
510                 hardware="no"
511         fi
512 fi
513
514 ## HARDWARE #############################
515
516 #
517 # here we use hwinfo to dump a table listing all the
518 # information we can find on the hardware of this machine
519
520
521 if [ "$hardware" == "yes" ]; then
522    if [ "dohwinfo" == "yes" ]; then
523       if [ -f $hardwarefile ]; then
524          rm $hardwarefile
525       fi
526       touch $hardwarefile
527       echo -e "\n\n====================== summary ======================\n" >>  $hardwarefile
528       debug "$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile"
529       $HWINFO --short --cpu --network --disk --pci  >> $hardwarefile
530       for flag in cpu network bios pci; do
531          echo -e "\n\n====================== $flag ======================\n" >>  $hardwarefile
532          $HWINFO --$flag >> $hardwarefile
533       done
534    fi
535 fi
536
537
538 ## PARTITIONS #############################
539
540 # here we use sfdisk to dump a listing of all the partitions. 
541 # these files can be used to directly partition a disk of the same size.
542
543 if [ "$partitions" == "yes" ]; then
544    if [ "$dosfdisk" == "yes" ]; then
545       devices=`LC_ALL=C $SFDISK -l 2>/dev/null | grep "^Disk /dev" | @AWK@ '{print $2}' | cut -d: -f1`
546         if [ "$devices" == "" ]; then 
547            warning "No harddisks found" 
548         fi
549         for dev in $devices; do
550                 debug "$SFDISK will try to backup partition tables for device $dev"
551                 [ -b $dev ] || continue
552                 label=${dev#/dev/}
553                 label=${label//\//-}
554                 outputfile=${partitionsfile//__star__/$label}
555                 debug "$SFDISK $sfdisk_options -d $dev > $outputfile 2>/dev/null"
556                 $SFDISK $sfdisk_options -d $dev > $outputfile 2>/dev/null
557                 if [ $? -ne 0 ]; then
558                    warning "The partition table for $dev could not be saved."
559                 fi
560         done
561    fi
562    if [ "$dohwinfo" == "yes" ]; then
563       debug "Using $HWINFO to get all available disk information"
564       echo -e "\n\n====================== $disk ======================\n" >>  $hardwarefile
565       $HWINFO --disk >> $hardwarefile
566    fi
567 fi
568
569 if [ "$luksheaders" == "yes" ]; then
570    devices=`LC_ALL=C $SFDISK -l 2>/dev/null | grep "^Disk /dev" | @AWK@ '{print $2}' | cut -d: -f1`
571    [ -n "$devices" ] || warning "No block device found"
572    targetdevices=""
573    for dev in $devices; do
574       [ -b $dev ] || continue
575       debug "$CRYPTSETUP isLuks $dev"
576       $CRYPTSETUP isLuks $dev
577       [ $? -eq 0 ] && targetdevices="$targetdevices $dev"
578    done
579    for dev in $targetdevices; do
580       label=${dev#/dev/}
581       label=${label//\//-}
582       outputfile=${luksheadersfile//__star__/$label}
583       # the following sizes are expressed in terms of 512-byte sectors
584       debug "Let us find out the Luks header size for $dev"
585       debug "$CRYPTSETUP luksDump \"$dev\" | grep '^Payload offset:' | @AWK@ '{print $3}'"
586       headersize=`$CRYPTSETUP luksDump "$dev" | grep '^Payload offset:' | @AWK@ '{print $3}'`
587       if [ $? -ne 0 ]; then
588          warning "Could not compute the size of Luks header, skipping device $dev"
589          continue
590       elif [ -z "$headersize" -o -n "`echo \"$headersize\" | sed 's/[0-9]*//g'`" ]; then
591          warning "The computed size of Luks header is not an integer, skipping device $dev"
592          continue
593       fi
594       debug "Let us backup the Luks header of device $dev"
595       debug "$DD if=\"${dev}\" of=\"${outputfile}\" bs=512 count=\"${headersize}\""
596       output=`$DD if="${dev}" of="${outputfile}" bs=512 count="${headersize}" 2>&1`
597       exit_code=$?
598       if [ $exit_code -eq 0 ]; then
599          debug $output
600          info "The Luks header of $dev was saved to $outputfile."
601       else
602          debug $output
603          fatal "The Luks header of $dev could not be saved."
604       fi
605    done
606 fi