shlib.sh: add mailref function
[girocco.git] / toolbox / backup-db.sh
blob93ba9f3f622d01796d5a00b96d80cd6231436c37
1 #!/bin/sh
3 # This script makes a rotating backup of the very
4 # important database files $chroot/etc/passwd and
5 # $chroot/etc/group and $chroot/etc/sshkeys.
6 # Backup files are rotated dropping .9.$ext and
7 # renaming the others to the next higher number
8 # and then finally making a copy of the original
9 # to a .1 (not compressed for passwd and group).
11 # Running this regularly from a cron entry is
12 # highly recommended. It may be run manually
13 # from the $basedir/toolbox directory any time
14 # after make install has been run to create an
15 # immediate backup.
17 # Backups retain the modification date of the file
18 # they are backing up at the time the backup was
19 # made. If restoration is required this can be
20 # used to determine the time period from which data
21 # would be lost if the backup were to be used.
23 set -e
25 . @basedir@/shlib.sh
27 # rotate_file basename suffix
28 rotate_file2_9() {
29 [ -f "$1.8.$2" ] && mv -f "$1.8.$2" "$1.9.$2"
30 [ -f "$1.7.$2" ] && mv -f "$1.7.$2" "$1.8.$2"
31 [ -f "$1.6.$2" ] && mv -f "$1.6.$2" "$1.7.$2"
32 [ -f "$1.5.$2" ] && mv -f "$1.5.$2" "$1.6.$2"
33 [ -f "$1.4.$2" ] && mv -f "$1.4.$2" "$1.5.$2"
34 [ -f "$1.3.$2" ] && mv -f "$1.3.$2" "$1.4.$2"
35 [ -f "$1.2.$2" ] && mv -f "$1.2.$2" "$1.3.$2"
36 return 0
39 # backup_file basename
40 backup_file() {
41 rotate_file2_9 "$1" gz
42 if [ -f "$1.1" ]; then
43 rm -f "$1.2.gz"
44 gzip -n9 < "$1.1" > "$1.2.gz" && \
45 touch -r "$1.1" "$1.2.gz" && \
46 chmod a-w "$1.2.gz"
48 if [ -f "$1" ]; then
49 cp -pf "$1" "$1.1" && \
50 chmod a-w "$1.1"
52 return 0
55 # badkup_dir basename
56 backup_dir() (
57 set -e
58 rotate_file2_9 "$1" tar.gz
59 [ -f "$1.1.tar.gz" ] && mv -f "$1.1.tar.gz" "$1.2.tar.gz"
60 if [ -d "$1" ]; then
61 cd "$(dirname "$1")"
62 base="$(basename "$1")"
63 rm -f "$base.1.tar.gz"
64 tar -c -f - $base | gzip -n9 > "$base.1.tar.gz"
65 chmod a-w "$base.1.tar.gz"
67 return 0
70 # Be paranoid
71 if [ -z "$cfg_chroot" ] || [ "$cfg_chroot" = "/" ]; then
72 echo 'Config.pm chroot setting is invalid' >&2
73 exit 1
76 backup_file "$cfg_chroot/etc/passwd"
77 backup_file "$cfg_chroot/etc/group"
78 backup_dir "$cfg_chroot/etc/sshkeys"
80 exit 0