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