handlers/sys: start to use new lib/vserver functionality
[matthijs/upstream/backupninja.git] / handlers / sys
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 enabled and are saved in /var/backups.
5 #
6 # (1) a list of all the packages installed and removed.
7 #     this file can be used to restore the state of installed packages
8 #     by running "dpkg --set-selections < dpkg-selections.txt
9
10 # (2) the partition table of all disks. 
11 #     this partition table can be used to format another disk of
12 #     the same size. this can be handy if using software raid and 
13 #     you have a disk go bad. just replace the disk and partition it
14 #     by running "sfdisk /dev/sdb < partitions.sdb.txt"
15 #     (MAKE SURE YOU PARTITION THE CORRECT DISK!!!)
16 #
17 # (3) hardware information. 
18 #     write to a text file the important things which hwinfo can gleen.
19 #
20
21 getconf packages yes
22 getconf packagesfile /var/backups/dpkg-selections.txt
23
24 getconf partitions yes
25 getconf partitionsfile /var/backups/partitions.__star__.txt
26
27 getconf hardware yes
28 getconf hardwarefile /var/backups/hardware.txt
29
30 getconf SFDISK `which sfdisk`
31 getconf HWINFO `which hwinfo`
32 getconf sfdisk_options ""
33 getconf hwinfo_options ""
34
35 # See if vservers are configured
36 local usevserver=no
37 if [ $vservers_are_available = yes ]
38 then
39    info "vserver method enabled"
40    usevserver=yes
41 fi
42
43 if [ "$packages" == "yes" ]; then
44         if [ $usevserver = yes ]
45         then
46                 nodpkg="lost+found|ARCHIVES"
47                 info "vserver root directory set to: $VROOTDIR"
48                 for vserver in $found_vservers
49                 do
50                         info "examining vserver: $vserver"
51                         running=`$VSERVERINFO $vserver RUNNING`
52                         if [ "$running" = "1" ]; then
53                             if [ ! -x "$VROOTDIR/$vserver`$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 [ "$packages" == "yes" ]; then
96    if [ $usevserver = yes ]
97    then
98       for vserver in `ls $VROOTDIR | grep -E -v $nodpkg`
99       do
100          debug "$VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile"
101          $VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile
102       done
103    fi
104    
105 # We want to perform this on the host as well
106    if [ "$packages" == "yes" ]; then
107       debug "dpkg --get-selections > $packagesfile"
108       dpkg --get-selections > $packagesfile
109    fi
110 fi
111
112 ## PARTITIONS #############################
113
114 # here we use sfdisk to dump a listing of all the partitions. 
115 # these files can be used to directly partition a disk of the same size.
116
117 if [ "$partitions" == "yes" ]; then
118         devices=`$HWINFO --disk | grep "Device File" | cut -d\  -f5`
119         for dev in $devices; do
120                 [ -b $dev ] || continue
121                 label=${dev#/dev/}
122                 label=${label//\//-}
123                 outputfile=${partitionsfile//__star__/$label}
124                 debug "$SFDISK $sfdisk_options -d $dev > $outputfile"
125                 $SFDISK $sfdisk_options -d $dev > $outputfile
126         done
127 fi
128
129 ## HARDWARE #############################
130
131 #
132 # here we use hwinfo to dump a table listing all the
133 # information we can find on the hardware of this machine
134
135
136 if [ "$hardware" == "yes" ]; then
137         if [ -f $hardwarefile ]; then
138                 rm $hardwarefile
139         fi
140         touch $hardwarefile
141         echo -e "\n\n====================== summary ======================\n" >>  $hardwarefile
142         debug "$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile"
143         $HWINFO --short --cpu --network --disk --pci  >> $hardwarefile
144         for flag in cpu network disk bios pci; do
145                 echo -e "\n\n====================== $flag ======================\n" >>  $hardwarefile
146                 $HWINFO --$flag >> $hardwarefile
147         done
148 fi