Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
blob: 5b881235a1c13c7a835ebec76e23827d79f54895 (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
#!/bin/sh
# $Id$
DIRS="/etc /srv /home/shtrom"
BACKUPROOT="/srv/rdiff-backup"
BACKUPDIR="${BACKUPROOT}/`hostname -s`"
BACKUPSERVER=backup.narf.ssji.net
#REMOTEBACKUPDIR="`hostname -s`@${BACKUPSERVER}::${BACKUPDIR}"
REMOTEBACKUPDIR="`hostname -s`@${BACKUPSERVER}::/share/homes/`hostname -s`"
EXCLUDEALWAYS="${BACKUPROOT} /srv/backup/ /srv/www/sites/offlines.fr/"
MYSQLDUMPDIR="/srv/backup/"
MYSQLUSER="dumper"
TMPSUBDIR=/tmp

# 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()
{
	mysql -B -N $@ -e "SELECT DISTINCT CONCAT(
		'SHOW GRANTS FOR \'', user, '\'@\'', host, '\';'
		) AS query FROM mysql.user" | \
		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.$(date +%Y-%m-%d_%H:%M).dump.sql"
	echo -n " mysql (${MYSQLDUMP})"
	# GRANT SELECT, SHOW VIEW, TRIGGER, LOCK TABLES ON *.* TO 'dumper'@'localhost';
	mysqldump -u ${MYSQLUSER} -A -E > ${MYSQLDUMP}
	mysqldump-grants -u ${MYSQLUSER} >> ${MYSQLDUMP}
	if [ "$1" = "remote" ]; then
		scp ${MYSQLDUMP} ${REMOTEBACKUPDIR}
	fi
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 "."