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