Fixed broken vserver dpkg parsing in sys handler
[matthijs/upstream/backupninja.git] / handlers / sys
1 #
2 # this handler will save various reports of vital system information.
3 # by default, all the reports are enabled and are saved in /var/backups.
4 #
5 # (1) a list of all the packages installed and removed.
6 #     this file can be used to restore the state of installed packages
7 #     by running "dpkg --set-selections < dpkg-selections.txt
8
9 # (2) the partition table of all disks. 
10 #     this partition table can be used to format another disk of
11 #     the same size. this can be handy if using software raid and 
12 #     you have a disk go bad. just replace the disk and partition it
13 #     by running "sfdisk /dev/sdb < partitions.sdb.txt"
14 #     (MAKE SURE YOU PARTITION THE CORRECT DISK!!!)
15 #
16 # (3) hardware information. 
17 #     write to a text file the important things which hwinfo can gleen.
18 #
19
20 getconf packages yes
21 getconf packagesfile /var/backups/dpkg-selections.txt
22
23 getconf partitions yes
24 getconf partitionsfile /var/backups/partitions.__star__.txt
25
26 getconf hardware yes
27 getconf hardwarefile /var/backups/hardware.txt
28
29 getconf SFDISK `which sfdisk`
30 getconf HWINFO `which hwinfo`
31 getconf sfdisk_options ""
32 getconf hwinfo_options ""
33
34 # See if vservers are configured
35 if [ "$vservers" = "yes" ]
36 then
37         if [ ! -d $VROOTDIR ]
38         then
39                 fatal "vservers enabled, but $VROOTDIR does not exist!"
40         else
41                 info "vserver method enabled"
42                 usevserver=1
43         fi
44 fi
45
46 if [ "$packages" == "yes" ]; then
47         if [ $usevserver ]
48         then
49                 nodpkg="lost+found"
50                 info "vserver root directory set to: $VROOTDIR"
51                 for vserver in `ls $VROOTDIR |grep -v $nodpkg`
52                 do
53                         info "examining vserver: $vserver"
54                         running=`vserver-info $vserver RUNNING`
55                         if [ $running = 1 ]; then
56                             if [ ! -x "`$VSERVER $vserver exec which dpkg`" ]; then
57                                 warning "can't find dpkg in vserver $vserver, skipping installed packages report."
58                                 nodpkg="$nodpkg $vserver"
59                             fi
60                         else
61                             warning "vserver $vserver is not running, skipping installed packages report."
62                             nodpkg="$nodpkg $vserver"
63                         fi
64
65                 done
66         else
67                 if [ ! -x "`which dpkg`" ]; then
68                         warning "can't find dpkg, skipping installed packages report."
69                         packages="no"
70                 fi
71         fi
72 fi
73
74 if [ "$partitions" == "yes" ]; then
75         if [ ! -x "$SFDISK" ]; then
76                 warning "can't find sfdisk, skipping partition report."
77                 partitions="no"
78         fi
79         if [ ! -x "$HWINFO" ]; then
80                 warning "can't find hwinfo, skipping partition report."
81                 partitions="no"
82         fi
83 fi
84
85 if [ "$hardware" == "yes" ]; then
86         if [ ! -x "$HWINFO" ]; then
87                 warning "can't find hwinfo, skipping hardware report."
88                 hardware="no"
89         fi
90 fi
91
92 ## PACKAGES ##############################
93
94 #
95 # here we grab a list of the packages installed and removed.
96 #
97
98 if [ $usevserver ]
99 then
100         for vserver in `ls $VROOTDIR | grep -v $nodpkg | grep -v lost+found`
101         do
102                 debug "$VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile"
103                 $VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile
104         done
105 fi
106
107 # We want to perform this on the host as well
108 if [ "$packages" == "yes" ]; then
109         debug "dpkg --get-selections > $packagesfile"
110         dpkg --get-selections > $packagesfile
111 fi
112
113
114 ## PARTITIONS #############################
115
116 # here we use sfdisk to dump a listing of all the partitions. 
117 # these files can be used to directly partition a disk of the same size.
118
119 if [ "$partitions" == "yes" ]; then
120         devices=`$HWINFO --disk | grep "Device File" | cut -d\  -f5`
121         for dev in $devices; do
122                 [ -b $dev ] || continue
123                 label=${dev#/dev/}
124                 label=${label//\//-}
125                 outputfile=${partitionsfile//__star__/$label}
126                 debug "$SFDISK $sfdisk_options -d $dev > $outputfile"
127                 $SFDISK $sfdisk_options -d $dev > $outputfile
128         done
129 fi
130
131 ## HARDWARE #############################
132
133 #
134 # here we use hwinfo to dump a table listing all the
135 # information we can find on the hardware of this machine
136
137
138 if [ "$hardware" == "yes" ]; then
139         if [ -f $hardwarefile ]; then
140                 rm $hardwarefile
141         fi
142         touch $hardwarefile
143         echo -e "\n\n====================== summary ======================\n" >>  $hardwarefile
144         debug "$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile"
145         $HWINFO --short --cpu --network --disk --pci  >> $hardwarefile
146         for flag in cpu network disk bios pci; do
147                 echo -e "\n\n====================== $flag ======================\n" >>  $hardwarefile
148                 $HWINFO --$flag >> $hardwarefile
149         done
150 fi