mysql: No longer prepend vroot to the backupdir.
[matthijs/upstream/backupninja.git] / handlers / sys.in
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 capture of the debconf package selection states. This file
7 #     can be used to restore the answers to debconf questions for
8 #     packages that you will be installing through (2) below. To
9 #     do this, run: "debconf-set-selections < debconfsel.txt"
10 #
11 # (2) a list of all the packages installed and removed.
12 #     this file can be used to restore the state of installed packages
13 #     by running "dpkg --set-selections < dpkg-selections.txt and
14 #     then run "apt-get -u dselect-upgrade". If you have the 
15 #     debconf-set-selections file from (1), you should restore those first.
16
17 # (3) the partition table of all disks. 
18 #     this partition table can be used to format another disk of
19 #     the same size. this can be handy if using software raid and 
20 #     you have a disk go bad. just replace the disk and partition it
21 #     by running "sfdisk /dev/sdb < partitions.sdb.txt"
22 #     (MAKE SURE YOU PARTITION THE CORRECT DISK!!!)
23 #
24 # (4) hardware information. 
25 #     write to a text file the important things which hwinfo can gleen.
26 #
27
28 if [ -f /etc/debian_version ]
29 then
30    os=debian
31    debug "Debian detected"
32    osversion="/etc/debian_version"
33 elif [ -f /etc/redhat-release ]
34 then
35    os=redhat
36    debug "Redhat detected"
37    osversion="/etc/redhat-release"
38 else
39    warning "Unknown OS detected!"
40 fi
41
42 getconf parentdir /var/backups
43 getconf packages yes
44 getconf dosfdisk yes
45 getconf dohwinfo yes
46
47 if [ ! -d $parentdir ]; then
48    mkdir -p $parentdir
49 fi
50    
51 if [ $os = "debian" ]
52 then
53    getconf packagesfile $parentdir/dpkg-selections.txt
54    getconf packagemgr   `which dpkg`
55    getconf packagemgroptions ' --get-selections *'
56    getconf selectionsfile $parentdir/debconfsel.txt
57    getconf debconfgetselections `which debconf-get-selections`
58 elif [ $os = "redhat" ]
59 then
60    getconf packagesfile  $parentdir/rpmpackages.txt 
61    getconf packagemgr   `which rpm`
62    getconf packagemgroptions    ' -qa '
63
64    getconf SYSREPORT `which sysreport`
65    getconf sysreport_options ' -norpm '
66 else
67    getconf packagesfile $parentdir/unknownOS.txt
68 fi
69 packagemgroptions="${packagemgroptions//__star__/*}"
70
71 getconf partitions yes
72 getconf partitionsfile $parentdir/partitions.__star__.txt
73
74 getconf hardware yes
75 getconf hardwarefile $parentdir/hardware.txt
76
77 getconf sysreport yes
78 getconf sysreportfile $parentdir/sysreport.txt
79
80 getconf SFDISK `which sfdisk`
81 getconf HWINFO `which hwinfo`
82 getconf sfdisk_options ""
83 getconf hwinfo_options ""
84
85 getconf vsnames all
86
87 # If vservers are configured, check that the ones listed in $vsnames are running.
88 local usevserver=no
89 if [ $vservers_are_available = yes ]; then
90    if [ "$vsnames" = all ]; then
91       vsnames="$found_vservers"
92    fi
93    if ! vservers_running "$vsnames" ; then
94       fatal "At least one of the vservers listed in vsnames ($vsnames) is not running."
95    fi
96    info "Using vservers '$vsnames'"
97    usevserver=yes
98 else
99    # Get the config value again, but now without a default value
100    getconf vsnames
101    [ -z "$vsnames" ] || warning 'vservers support disabled in backupninja.conf, vsnames configuration line will be ignored'
102
103 fi
104
105 ## PACKAGES ##############################
106
107 #
108 # here we grab a list of the packages installed and removed.
109 #
110
111 if [ "$packages" == "yes" ]; then
112
113    if [ $usevserver = yes ]; then
114       info "vserver root directory set to: $VROOTDIR"
115       for vserver in $vsnames; do
116          info "examining vserver: $vserver"
117          # is it running ?
118          vservers_running $vserver
119          if [ $? -ne 0 ]; then
120             warning "The vserver $vserver is not running."
121             continue
122          fi
123          # is $packagemgr available inside $vserver ?
124          if [ ! -x "${VROOTDIR}/${vserver}${packagemgr}" ]; then
125             warning "can't find $packagemgr in vserver $vserver, skipping installed packages report."
126          else
127             # don't expand * since it can be used in $packagemgroptions
128             set -o noglob
129             debug "$VSERVER $vserver exec $packagemgr $packagemgroptions > $VROOTDIR/$vserver$packagesfile"
130             $VSERVER $vserver exec $packagemgr $packagemgroptions > $VROOTDIR/$vserver$packagesfile || fatal "can not save $packagemgr info to $packagesfile"
131             set +o noglob
132          fi
133          # is $debconfgetselections available inside $vserver ?
134          found=no
135          # case #1: it is available on the host, is it available inside $vserver ?
136          if [ -n "$debconfgetselections" ]; then
137             [ -x "${VROOTDIR}/${vserver}${debconfgetselections}" ] && found=yes
138          # case #2: it is not available on the host, is it available inside $vserver ?
139          else
140             [ -n "`$VSERVER $vserver exec which debconf-get-selections`" ] && found=yes
141          fi
142          if [ "$found" != yes ]; then
143             warning "can't find debconf-get-selections in vserver $vserver, skipping package selection states. You may want to install the debconf-utils package."
144          else
145             debug "$VSERVER $vserver exec $debconfgetselections > $VROOTDIR/$vserver$selectionsfile"
146             $VSERVER $vserver exec $debconfgetselections > $VROOTDIR/$vserver$selectionsfile || fatal "can not save debconf-get-selections info to $selectionsfile"
147          fi
148          unset found
149       done
150    fi
151    
152    # We want to perform this on the host as well
153    if [ -z "$packagemgr" -o ! -x "$packagemgr" ]; then 
154       warning "can't find ${packagemgr}, skipping installed packages report."
155    else
156       # don't expand * since it can be used in $packagemgroptions
157       set -o noglob
158       debug "$packagemgr $packagemgroptions > $packagesfile"
159       $packagemgr $packagemgroptions > $packagesfile || fatal "can not save $packagemgr info to $packagesfile"
160       set +o noglob
161    fi
162    if [ -z "$debconfgetselections" ]; then
163       warning "can't find debconf-get-selections, skipping package selection states. You might want to install the debconf-utils package."
164    else
165       debug "$debconfgetselections > $selectionsfile"
166       $debconfgetselections > $selectionsfile || fatal "can not save $debconfgetselections info to $selectionsfile"
167    fi
168 fi
169
170 ## System report ##############################
171
172 #
173 # here we grab a bunch of system stuff for a report
174 #
175
176 export STATUS
177
178 HASHES="#################################################################"
179 DASHES="-----------------------------------------------------------------"
180
181 cat /dev/null > $sysreportfile || fatal "can not write to $sysreportfile"
182
183
184 catiffile () {
185    echo $HASHES >> $sysreportfile
186    echo "# $STATUS" >> $sysreportfile
187    echo $HASHES >> $sysreportfile
188    if [ -f $1 ]; then
189       echo "file: $1" >> $sysreportfile
190       echo $DASHES >> $sysreportfile
191       cat $1 >> $sysreportfile 2>&1 || info "reading of $1 failed"
192    fi
193    if [ -d $1 ]; then
194       echo "directory: $1" >> $sysreportfile
195       echo $DASHES >> $sysreportfile
196       for file in `find $1 -maxdepth 3 -noleaf -type f`
197       do
198        catiffile $file
199       done
200    fi
201    echo $DASHES >> $sysreportfile
202
203
204 catifexec () {
205    if [ -x $1 ]; then
206       echo $HASHES >> $sysreportfile
207       echo "# $STATUS" >> $sysreportfile
208       echo $HASHES >> $sysreportfile
209       $*  >> $sysreportfile 2>&1 || info "executing of $1 failed"
210    fi
211 }
212    
213
214 STATUS="Determining $os version:"
215 catiffile $osversion
216
217 STATUS="Determinding your current hostname: " 
218 catifexec "/bin/hostname"
219
220 STATUS="Getting the date:"
221 catifexec "/bin/date"
222
223 STATUS="Checking your systems current uptime and load average:"
224 catifexec "/usr/bin/uptime"
225
226 STATUS="Checking available memory:"
227 catifexec "/usr/bin/free"
228
229 STATUS="Checking free disk space:"
230 catifexec "/bin/df" "-al"
231
232 STATUS="Collecting what services run at what run level:"
233 if [ $os = "redhat" ]; then
234    catifexec "/sbin/chkconfig" "--list"
235    STATUS="Collecting information about /etc/rc.d:"
236    catiffile "/bin/ls /etc/rc.d/rc*.d/"
237
238 elif [ $os = "debian" ]; then
239     for level in 0 1 2 3 4 5 6 S; do
240        echo "Level: $level" >> $sysreportfile
241        for f in /etc/rc${level}.d/*; do
242         # Remove /etc/Knn or Snn from beginning
243           ff=$(echo $f | @SED@ 's_/etc/rc..d/[KS][0-9][0-9]__')
244           if [ $f != $ff ]; then
245              echo $ff >> $sysreportfile
246           fi
247        done
248        echo "" >> $sysreportfile
249     done
250 fi
251
252 STATUS="Getting bootloader information:"
253 catifexec "/bin/ls" "-alR /boot"
254
255 # This covers sparc, alpha, and intel (respectively)
256 # updated for grub -mpg
257 if [ -f /etc/silo.conf ]; then
258   STATUS="Collecting information about the boot process (silo):"
259   catiffile "/etc/silo.conf"
260 fi
261 if [ -f /etc/milo.conf ]; then
262   STATUS="Collecting information about the boot process (milo):"
263   catiffile "/etc/milo.conf"
264 fi
265 if [ -f /etc/lilo.conf ]; then
266   STATUS="Collecting information about the boot process (lilo):"
267   catiffile "/etc/lilo.conf"
268   catifexec "/sbin/lilo" "-q"
269 fi
270 if [ -d /boot/grub -a -f /boot/grub/grub.conf -a -f /boot/grub/device.map ]; then
271   STATUS="Collecting information about the boot process (grub.conf):"
272   catiffile "/boot/grub/grub.conf"
273   STATUS="Collecting information about the boot process (grub.map):"
274   catiffile "/boot/grub/device.map"
275 fi
276 if [ -f /etc/cluster.conf -o -f /etc/cluster.xml ] ; then
277   STATUS="Gathering information on cluster setup"
278   # 2.1 AS
279   if [ -f /etc/cluster.conf ] ; then
280     catiffile "/etc/cluster.conf"
281   fi
282   # Taroon
283   if [ -f /etc/cluster.xml ] ; then
284     catiffile "/etc/cluster.xml"
285   fi
286 fi
287
288 STATUS="Gathering sysctl information (sysctl -a):"
289 catiffile "sysctl -a 2>/dev/null"
290 STATUS="Gathering sysctl information (/etc/sysctl.conf):"
291 catiffile "/etc/sysctl.conf"
292
293 STATUS="Gathering IP information (/sbin/ifconfig):"
294 catifexec "/sbin/ifconfig" "-a"
295
296 STATUS="Gathering additional IP information (/bin/ip addr list):"
297 catifexec "/bin/ip" "addr list"
298
299 STATUS="Checking network routes:"
300 catifexec "/sbin/route" "-n"
301
302 STATUS="Collecting Name Service Switch config information:"
303 catiffile "/etc/nsswitch.conf"
304
305 STATUS="Collecting information about system authentication (pam):"
306 catiffile "/etc/pam.conf"
307 catiffile "/etc/pam.d"
308
309 echo
310 echo "Getting information about the kernel."
311 echo
312 STATUS="Getting kernel version:"
313 catifexec "/bin/uname" "-a"
314 STATUS="Checking module information:"
315 catifexec "/sbin/lsmod"
316 for x  in $(/sbin/lsmod | /bin/cut -f1 -d" " 2>/dev/null | /bin/grep -v Module 2>/dev/null 
317 ) ; do
318   STATUS="Checking module information $x:"
319   catifexec "/sbin/modinfo" "$x"
320 done
321
322 STATUS="Gathering information about your filesystems:"
323 catiffile "/proc/filesystems"
324
325 STATUS="Gathering information about your system stat:"
326 catiffile "/proc/stat"
327
328 STATUS="Gathering information about your partitions:"
329 catiffile "/proc/partitions"
330
331 STATUS="Gathering information about your ksyms:"
332 catiffile "/proc/kallsyms"
333
334 STATUS="Gathering information about slabinfo:"
335 catiffile "/proc/slabinfo"
336
337 # Added support to cover for the new modules.conf layout in Red Hat 7
338 STATUS="Collecting information regarding kernel modules"
339 VER=`uname -r`
340 catiffile "/lib/modules/$VER/modules.dep"
341 if [ -f /etc/conf.modules ]; then
342   STATUS="Collecting information regarding kernel modules (conf.modules)"
343   catiffile "/etc/conf.modules"
344 fi
345 if [ -f /etc/modules.conf ]; then
346   STATUS="Collecting information regarding kernel modules (modules.conf)"
347   catiffile "/etc/modules.conf"
348 fi
349 if [ -f /etc/modprobe.conf ]; then
350   STATUS="Collecting information regarding kernel modules (modeprobe.conf)"
351   catiffile "/etc/modprobe.conf"
352 fi
353
354 # dkms status
355 if [ -x /usr/sbin/dkms ] ; then
356    STATUS="Gathering current status of modules, versions and kernels (dkms):"
357   catifexec "/usr/sbin/dkms" "status"
358 fi
359
360 if [ -f /etc/sysconfig/isdncard ] ; then
361   STATUS="Gathering information about ISDN:"
362   catiffile "/etc/sysconfig/isdncard"
363 fi
364
365 STATUS="Collecting information from the proc directory:"
366 catiffile "/proc/pci"
367
368 STATUS="Getting kernel command line"
369 catiffile "/proc/cmdline"
370
371 STATUS="Gathering information about your CPU:"
372 catiffile "/proc/cpuinfo"
373
374 STATUS="Gathering information about your Ram:"
375 catiffile "/proc/meminfo"
376
377 STATUS="Gathering information about your ioports:"
378 catiffile "/proc/ioports"
379
380 STATUS="Gathering information about your interrupts:"
381 catiffile "/proc/interrupts"
382
383 STATUS="Gathering information about your scsi devices:"
384 catiffile "/proc/scsi"
385
386 STATUS="Gathering information about your dma:"
387 catiffile "/proc/dma"
388
389 STATUS="Gathering information about your devices (/proc/devices):"
390 catiffile "/proc/devices"
391
392 STATUS="Gathering information about your rtc:"
393 catiffile "/proc/rtc"
394
395 STATUS="Gathering information about your ide drivers:"
396 catiffile "/proc/ide"
397
398 STATUS="Gathering information about your bus:"
399 catifexec "/usr/bin/lspci"
400 catiffile "/proc/bus"
401
402 echo
403 echo "Getting disk and filesystem information."
404 echo
405
406 STATUS="Collecting information from /etc/fstab:"
407 catiffile "/etc/fstab"
408
409 STATUS="Collecting disk partition information:"
410 catifexec "/sbin/fdisk" "-l"
411
412 STATUS="Checking mounted file systems (mount) "
413 catifexec "/bin/mount"
414
415 STATUS="Checking mounted file systems (/proc/mounts)"
416 catiffile "/proc/mounts"
417
418 STATUS="Collecting Software RAID information (/proc/mdstat)"
419 catiffile "/proc/mdstat"
420
421 STATUS="Collecting Software RAID information (/etc/raidtab)"
422 catiffile "/etc/raidtab"
423
424 STATUS="Collecting Software RAID information (/etc/mdadm.conf)"
425 catiffile "/etc/mdadm.conf"
426
427 STATUS="Collecting Software RAID information (/sbin/mdadm -Q)"
428 catifexec "/sbin/mdadm" "-Q" "--detail" '/dev/md?*'
429
430 STATUS="Collecting Automount information (auto.master)"
431 catiffile "/etc/auto.master"
432
433 STATUS="Collecting Automount information (auto.misc)"
434 catiffile "/etc/auto.misc"
435
436 STATUS="Collecting Automount information (auto.net)"
437 catiffile "/etc/auto.net"
438
439 STATUS="Collecting LVM information:"
440 if [ $os = "redhat" ]; then
441    catifexec "/usr/sbin/vgdisplay" "-vv"
442 elif [ $os = "debian" ]; then
443    catifexec "/sbin/vgdisplay" "-vv"
444 fi
445    
446 STATUS="Collecting SCSI Tape information (/etc/stinit.def)"
447 catiffile "/etc/stinit.def"
448
449 if [ -x /sbin/lsusb ] ; then
450   STATUS="Collecting USB devices list (lsusb):"
451   catifexec "/sbin/lsusb"
452 fi
453
454 if [ -x /usr/bin/lshal ] ; then
455   STATUS="Collecting global devices list (lshal):"
456   catifexec "/usr/bin/lshal"
457 fi
458
459
460 STATUS="Gathering information on SELinux setup"
461 catifexec "/usr/bin/selinuxconfig"
462 catifexec "/usr/sbin/sestatus"
463 if [ $os = "redhat" ]; then
464    catifexec "rpm" "-q -V selinux-policy-targeted"
465    catifexec "rpm" "-q -V selinux-policy-strict"
466 fi
467
468 if [ $usevserver = yes ]; then
469    STATUS="Gathering vserver information"
470    catiffile "/proc/virtual"
471 fi
472
473 if [ "$partitions" == "yes" ]; then
474    if [ "$dosfdisk" == "yes" ]; then
475         if [ ! -x "$SFDISK" ]; then
476                 warning "can't find sfdisk, skipping sfdisk report."
477                 partitions="no"
478         fi
479    fi
480    if [ "$dohwinfo" == "yes" ]; then
481         if [ ! -x "$HWINFO" ]; then
482                 warning "can't find hwinfo, skipping partition report."
483                 partitions="no"
484         fi
485    fi
486 fi
487
488 if [ "$hardware" == "yes" ]; then
489         if [ ! -x "$HWINFO" ]; then
490                 warning "can't find hwinfo, skipping hardware report."
491                 hardware="no"
492         fi
493 fi
494
495 ## HARDWARE #############################
496
497 #
498 # here we use hwinfo to dump a table listing all the
499 # information we can find on the hardware of this machine
500
501
502 if [ "$hardware" == "yes" ]; then
503    if [ "dohwinfo" == "yes" ]; then
504       if [ -f $hardwarefile ]; then
505          rm $hardwarefile
506       fi
507       touch $hardwarefile
508       echo -e "\n\n====================== summary ======================\n" >>  $hardwarefile
509       debug "$HWINFO --short --cpu --network --disk --pci  >> $hardwarefile"
510       $HWINFO --short --cpu --network --disk --pci  >> $hardwarefile
511       for flag in cpu network bios pci; do
512          echo -e "\n\n====================== $flag ======================\n" >>  $hardwarefile
513          $HWINFO --$flag >> $hardwarefile
514       done
515    fi
516 fi
517
518
519 ## PARTITIONS #############################
520
521 # here we use sfdisk to dump a listing of all the partitions. 
522 # these files can be used to directly partition a disk of the same size.
523
524 if [ "$partitions" == "yes" ]; then
525    if [ "$dosfdisk" == "yes" ]; then
526       devices=`LC_ALL=C $SFDISK -l 2>/dev/null | grep "^Disk /dev" | @AWK@ '{print $2}' | cut -d: -f1`
527         if [ "$devices" == "" ]; then 
528            warning "No harddisks found" 
529         fi
530         for dev in $devices; do
531                 debug "$SFDISK will try to backup partition tables for device $dev"
532                 [ -b $dev ] || continue
533                 label=${dev#/dev/}
534                 label=${label//\//-}
535                 outputfile=${partitionsfile//__star__/$label}
536                 debug "$SFDISK $sfdisk_options -d $dev > $outputfile 2>/dev/null"
537                 $SFDISK $sfdisk_options -d $dev > $outputfile 2>/dev/null
538                 if [ $? -ne 0 ]; then
539                    warning "The partition table for $dev could not be saved."
540                 fi
541         done
542    fi
543    if [ "$dohwinfo" == "yes" ]; then
544       debug "Using $HWINFO to get all available disk information"
545       echo -e "\n\n====================== $disk ======================\n" >>  $hardwarefile
546       $HWINFO --disk >> $hardwarefile
547    fi
548 fi