blob: 8a22ee5214fa96cc49da2e07f0ce884d2a104e4c (
plain) (
blame)
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
|
#!/bin/bash
# this script will backup all mysql databases from the server to a ftp
# server
# mysql server informations
MYSQL_SERVER="localhost"
MYSQL_USER="backup"
MYSQL_PASSWORD="password"
# databases to exclude, seperated by space
# TODO
#MYSQL_EXCLUDE_DB=""
# ftp server data
FTP_SERVER="backup"
FTP_USER="ftp"
FTP_PASSWORD="password"
# target directory on the ftp (if no / at the beginning, relativ to
# the first directory after login)
FTP_TARGET="mysql"
# temporary directory
TMP_DIRECTORY="/tmp"
# backup name
BACKUP_PREFIX="$(hostname -f)_${MYSQL_SERVER}_$(date +%F)"
# logging
LOGGING="0"
## end of configuration
log() {
if [ $LOGGING -eq 0 ]; then
if [ -n "$1" ]; then
echo "$@"
else
cat
fi
fi
}
# check for tools
for prog in mysqlshow mysqldump ncftpput
do
if ! hash $prog 2>/dev/null ; then
echo "Error: This script needs: $prog. Exiting..." >&2
exit 1
fi
done
echo "Started backup of mysql databases ($(date +%c)):" | log
echo | log
# create temporary dir
mkdir -p ${TMP_DIRECTORY}
TMP_DIR=$(mktemp -d --tmpdir=${TMP_DIRECTORY})
pushd ${TMP_DIR} >/dev/null 2>&1
# enumerate all mysql databases
MYSQL_DATABASES=$(mysqlshow -h${MYSQL_SERVER} -u${MYSQL_USER} \
-p${MYSQL_PASSWORD} | awk '{ print $2 }' | grep -v "^$" | sed \
'1d')
# backup all databases to file
for db in ${MYSQL_DATABASES}
do
mysqldump -h${MYSQL_SERVER} -u${MYSQL_USER} -p${MYSQL_PASSWORD} \
${db} | bzip2 -zc > "${BACKUP_PREFIX}_${db}.sql.bz2"
done
# echo created log
du -sch * | log
# move backups to ftp server
for file in *
do
ncftpput -u${FTP_USER} -p${FTP_PASSWORD} -V -DD ${FTP_SERVER} \
${FTP_TARGET} ${file} >/dev/null
done
echo | log
echo "Finished backup of mysql databases ($(date +%c))" | log
# restore old cwd
popd >/dev/null 2>&1
rmdir ${TMP_DIR}
|