-
Notifications
You must be signed in to change notification settings - Fork 3
/
backup
executable file
·163 lines (141 loc) · 4.69 KB
/
backup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh
# $Id: backup,v 1.36 2006-06-30 10:09:56 turbo Exp $
#
# Backup script for host: aurora
DATE=`date +"%Y%m%d"`
BACKUPDIR=/var/.backups
HOSTNAME=`hostname`
BACKUPFILE1=$BACKUPDIR/System/$HOSTNAME-confs-$DATE.tar.bz2
if [ -f /etc/init.d/slapd.provider ]; then
BACKUPFILE2=$BACKUPDIR/OpenLDAP/slapd-$HOSTNAME-$DATE.ldif
fi
BACKUPFILE3=$BACKUPDIR/KerberosV/$HOSTNAME-krb5-$DATE
BACKUPFILE4=$BACKUPDIR/MySQL/mysql-$DATE.tar.bz2
BACKUPFILE5=/etc/dpkg/selections-$DATE.gz
BACKUPFILE6=$BACKUPDIR/OpenAFS/afsd-$DATE.gz
BACKUPFILE7=$BACKUPDIR/Logs/$HOSTNAME-oldlogs-$DATE.tar.bz2
BACKUPFILE8=$BACKUPDIR/Logs/bacula-oldlogs-$DATE.tar.bz2
BACKUPFILE9=$BACKUPDIR/PostgreSQL/postgresql-$DATE.sql.bz2
BACKUPFILES="$BACKUPFILE1 $BACKUPFILE2.bz2 $BACKUPFILE3.bz2 $BACKUPFILE4 $BACKUPFILE6 $BACKUPFILE7 $BACKUPFILE8 $BACKUPFILE9"
if [ ! -z "$1" ]; then
BACKUP_TYPE=$1
fi
cd /
renice +5 $$
# Make sure the directoris exists.
for file in $BACKUPFILES; do
dir=`dirname $file`
[ ! -x $dir ] && mkdir -p $dir
done
echo "Starting system backup ($BACKUP_TYPE) at" `date +"%Y%m%d %H:%M:%S"`
# Support function
check_error () {
local errcode=$1 ; shift
local msg="$*"
if [ "$errcode" = "0" ]; then
echo "Successfully backed up $msg."
else
echo "Failed to backup $msg!"
fi
}
# ----- L D A P -----
if [ -f /etc/init.d/slapd.provider ]; then
/etc/init.d/slapd.provider stop > /dev/null 2>&1
pushd /var/lib/ldap.provider/ > /dev/null 2>&1 && db4.2_recover && db4.2_archive -d
slapcat -m -b 'c=SE' -l $BACKUPFILE2 -f /etc/ldap/slapd.conf.provider #> /dev/null 2>&1
check_error "$?" "LDAP database"
popd > /dev/null 2>&1
/etc/init.d/slapd.provider start > /dev/null 2>&1
bzip2 -f9 $BACKUPFILE2
fi
# ----- K E R B E R O S -----
kdb5_util dump $BACKUPFILE3
check_error "$?" "KerberosV database"
if [ -f $BACKUPFILE3.dump_ok ]; then
bzip2 -f9 $BACKUPFILE3
rm $BACKUPFILE3.dump_ok
fi
# ----- A F S -----
DIR=etc/openafs
if [ -d "$DIR" ]; then
/usr/local/bin/list_afs_vols.sh -f > $DIR/volumes.txt
tar cjpf $BACKUPFILE6 \
$DIR/CellServDB $DIR/afs.conf* $DIR/ThisCell \
$DIR/server $DIR/cacheinfo $DIR/volumes.txt \
var/lib/openafs/db/*.DB0
check_error "$?" "AFS database"
fi
# ----- M y S Q L -----
USER=`cat /root/.my.cnf | grep ^user | sed 's@.*= @@'`
PASS=`cat /root/.my.cnf | grep ^pass | sed 's@.*= @@'`
TMPDIR=$BACKUPDIR/MySQL/tmp
if [ ! -d $TMPDIR ]; then
mkdir $TMPDIR
else
rm -R $TMPDIR
mkdir $TMPDIR
fi
mysqlhotcopy --quiet --user=$USER --password=$PASS --tmpdir=$BACKUPDIR/tmp \
--socket=/var/run/mysqld/mysqld.sock --regexp=.* $TMPDIR \
> /dev/null 2>&1
code=$? ; check_error "$?" "MySQL database(s)"
if [ "$code" = "0" ]; then
pushd $TMPDIR > /dev/null 2>&1
tar cjf $BACKUPFILE4 .
popd > /dev/null 2>&1
fi
rm -fR $TMPDIR
# ----- P O S T G R E S Q L -----
TMPFILE=`tempfile -d $BACKUPDIR/tmp -p pgsql.` ; chown postgres $TMPFILE ; chmod 600 $TMPFILE
su postgres -c "/usr/lib/postgresql/bin/pg_dumpall -c > $TMPFILE 2> /dev/null"
code=$? ; check_error "$code" "PostgreSQL database(s)"
if [ "$code" != "0" ]; then
# Error occured - test if PostgreSQL is alive.
psql template1 -q -c 'SHOW TimeZone;' -o /dev/null 2> /dev/null
code=$?
if [ "$code" -gt 0 ]; then
if [ "$code" = "2" ]; then
echo " -=> PostgreSQL does not accept connections. Is it started?"
else
echo " -=> Other PostgreSQL error ($code)"
fi
fi
else
cat $TMPFILE | bzip2 -f9 > $BACKUPFILE9
fi
rm -f $TMPFILE
# ----- I M P O R T A N T C O N F I G F I L E S -----
dpkg --get-selections \* | sort | gzip -f9 > $BACKUPFILE5
tar cjpsf $BACKUPFILE1 `find etc/ldap etc/krb5* \
var/lib/named/etc/bind /var/lib/named/var/cache/bind \
etc/hosts* etc/cron* etc/{pam.d,qmail,xinetd.d,apache,tcp.smtp} \
etc/{nsswitch,firewall.block}.conf \
etc/{profile,mailname,hostname,network/interfaces} \
etc/init.d/{qmail,slapd,slapd.bind9,slapd.demo,slapd.main,slapd.provider,slapd.recover} \
sbin/backup-aurora $BACKUPFILE5 -type f | egrep -v '~$' | \
sed -e 's@^/@@' -e 's@\.dpkg-dist@@' | \
sort` > /dev/null 2>&1
# -----------------
# Logfiles...
if [ "$FILES" != '' ]; then
pushd /var/log > /dev/null 2>&1
tar --remove-files -cjf $BACKUPFILE7 `find -name '*.gz' | sort`
popd > /dev/null 2>&1
fi
# -----------------
# Bacula logs etc
if [ -e /var/lib/bacula ]; then
pushd /var/lib/bacula > /dev/null 2>&1
tar --remove-files -cjf $BACKUPFILE8 `find -name '*.mail*' -o -name 'log*gz' | sort`
popd > /dev/null 2>&1
fi
# -----------------
for file in $BACKUPFILES; do
# if [ -f "$file" ]; then
chgrp backup $file
chmod 640 $file
# fi
done
rm -f $BACKUPFILE5
echo "Ending system backup at" `date +"%Y%m%d %H:%M:%S"`
echo