Fixed sys handler bracket problem
[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                 for vserver in `ls $VROOTDIR |grep -v lost+found`
50                 do
51                         running=`vserver-info $vserver RUNNING`
52                         if [ $running = 1 ]; then
53                             if [ ! -x "`$VSERVER $vserver exec which dpkg`" ]; then
54                                 warning "can't find dpkg in vserver $vserver, skipping installed packages report."
55                                 nodpkg="$nodpkg $vserver"
56                             fi
57                         else
58                             warning "vserver $vserver is not running, skipping installed packages report."
59                             nodpkg="$nodpkg $vserver"
60                         fi
61
62                 done
63         else
64                 if [ ! -x "`which dpkg`" ]; then
65                         warning "can't find dpkg, skipping installed packages report."
66                         packages="no"
67                 fi
68         fi
69 fi
70
71 if [ "$partitions" == "yes" ]; then
72         if [ ! -x "$SFDISK" ]; then
73                 warning "can't find sfdisk, skipping partition report."
74                 partitions="no"
75         fi
76         if [ ! -x "$HWINFO" ]; then
77                 warning "can't find hwinfo, skipping partition report."
78                 partitions="no"
79         fi
80 fi
81
82 if [ "$hardware" == "yes" ]; then
83         if [ ! -x "$HWINFO" ]; then
84                 warning "can't find hwinfo, skipping hardware report."
85                 hardware="no"
86         fi
87 fi
88
89 ## PACKAGES ##############################
90
91 #
92 # here we grab a list of the packages installed and removed.
93 #
94
95 if [ $usevserver ]
96 then
97         for vserver in `ls $VROOTDIR | grep -v $nodpkg | grep -v lost+found`
98         do
99                 debug "$VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile"
100                 $VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile
101         done
102 fi
103
104 # We want to perform this on the host as well
105 if [ "$packages" == "yes" ]; then
106         debug "dpkg --get-selections > $packagesfile"
107         dpkg --get-selections > $packagesfile
108 fi
109
110
111 ## PARTITIONS #############################
112
113 # here we use sfdisk to dump a listing of all the partitions. 
114 # these files can be used to directly partition a disk of the same size.
115
116 if [ "$partitions" == "yes" ]; then
117         devices=`$HWINFO --disk | grep "Device File" | cut -d\  -f5`
118         for dev in $devices; do
119                 [ -b $dev ] || continue
120                 label=${dev#/dev/}
121                 label=${label//\//-}
122                 outputfile=${partitionsfile//__star__/$label}
123                 debug "$SFDISK $sfdisk_options -d $dev > $outputfile"
124                 $SFDISK $sfdisk_options -d $dev > $outputfile
125         done
126 fi
127
128 ## HARDWARE #############################
129
130 #
131 # here we use hwinfo to dump a table listing all the
132 # information we can find on the hardware of this machine
133
134
135 if [ "$hardware" == "yes" ]; then
136         if [ -f $hardwarefile ]; then
137                 rm $hardwarefile
138         fi
139         touch $hardwarefile
140         echo -e "\n\n====================== summary ======================\n" >>  $hardwarefile
141         debug "$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile"
142         $HWINFO --short --cpu --network --disk --pci  >> $hardwarefile
143         for flag in cpu network disk bios pci; do
144                 echo -e "\n\n====================== $flag ======================\n" >>  $hardwarefile
145                 $HWINFO --$flag >> $hardwarefile
146         done
147 fi