Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
blob: 83fa2c9d5d2256ebe603a0d322b62be8e7f964fc (plain)
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
#!/bin/sh
# The following variables can be overridden in /etc/backup.sh.conf
DIRS="/etc /srv"
TMPSUBDIR=/tmp

BACKUPROOT="/srv/rdiff-backup"
BACKUPDIR="${BACKUPROOT}/`hostname -s`"
BACKUPSERVER=backup.example.net
REMOTEBACKUPDIR="`hostname -s`@${BACKUPSERVER}::${BACKUPDIR}"
EXCLUDEALWAYS="${BACKUPROOT}"

# enable database backups by specifying non-null *DUMPDIR
MYSQLBINPATH=/usr/local/bin/
MYSQLDUMPDIR="/srv/dbdumps/"
MYSQLUSER=""
MYSQLPASS=""

LDAPBINPATH=/usr/local/bin/
LDAPDUMPDIR="/srv/dbdumps/"
LDAPUSER="cn=root,dc=example,dc=net"
LDAPPASS="secret"

test -e /etc/backup.sh.conf && . /etc/backup.sh.conf

# Dump privilege information  [0]
# [0] http://serverfault.com/questions/8860/how-can-i-export-the-privileges-from-mysql-and-then-import-to-a-new-server/13050#13050
mysqldump-grants()
{
	${MYSQLBINPATH}mysql -B -N $@ -e "SELECT DISTINCT CONCAT(
		'SHOW GRANTS FOR \'', user, '\'@\'', host, '\';'
		) AS query FROM mysql.user" | \
		${MYSQLBINPATH}mysql $@ | \
		sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}'
}

if [ "$1" = "remote" ]; then
	EFFECTIVEBACKUPDIR=${REMOTEBACKUPDIR}
	REMOTESCHEMA="ssh -qCT -i /etc/ssh/ssh_host_rsa_key %s"
else
	EFFECTIVEBACKUPDIR=${BACKUPDIR}
	REMOTESCHEMA=""
fi

echo -n "Dumping databases:"
if [ ! -z "$MYSQLDUMPDIR" ]; then
	MYSQLDUMP="${MYSQLDUMPDIR}mysql.dump.sql"
	echo -n " mysql (${MYSQLDUMP})"
	# GRANT SELECT, SHOW VIEW, TRIGGER, LOCK TABLES ON *.* TO 'dumper'@'localhost';
	${MYSQLBINPATH}mysqldump --comments --dump-date -u ${MYSQLUSER} > ${MYSQLDUMP} 
	mysqldump-grants -u ${MYSQLUSER} >> ${MYSQLDUMP}
fi

if [ ! -z "$LDAPDUMPDIR" ]; then
	LDAPPASSFILE=`mktemp` || exit 1
	cat <<EOF > ${LDAPPASSFILE}
${LDAPPASS}
EOF
	LDAPDUMP="${LDAPDUMPDIR}ldap.dump.ldiff"
	echo -n " ldap (${LDAPDUMP})"
	TERM=vt100 ${LDAPBINPATH}ldapvi --noninteractive --quiet --noquestions --out --host localhost -D "${LDAPUSER}" -y "${LDAPPASSFILE}" > ${LDAPDUMP}
	rm -f ${LDAPPASSFILE}
fi
echo "."

echo -n "Backing up to ${EFFECTIVEBACKUPDIR}:"
for DIR in ${DIRS}; do
	echo -n " ${DIR}"
	EXCLS=""
	for EXCL in ${EXCLUDEALWAYS}; do
		if [ "${EXCL#${DIR}}" != "${EXCL}" ]; then
			EXCLS="${EXCLS} --exclude ${EXCL}"
		fi
	done
	/usr/local/bin/rdiff-backup --create-full-path ${REMOTESCHEMA:+--remote-schema "${REMOTESCHEMA}"} ${TMPSUBDIR:+--tempdir ${BACKUPDIR}${TMPSUBDIR}} ${TMPSUBDIRxxxDISABLED:+--remote-tempdir /share/homes/`hostname -s`${TMPSUBDIR}} ${EXCLS} ${DIR} ${EFFECTIVEBACKUPDIR}${DIR}
done
echo "."