-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_mysql.sh
More file actions
52 lines (43 loc) · 1.48 KB
/
backup_mysql.sh
File metadata and controls
52 lines (43 loc) · 1.48 KB
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
#!/bin/bash -e
# Veriosn 1.0
###########################################################
# Back up MySQL databases
###########################################################
# This requires the .my.cnf in the home directory of the user the script runs as (root, when being run from rsnapshot).
# As it contains an important password, permissions on this file should be 600 (-rw-------).
#
# [client]
# user = root
# password = YOUR_MYSQL_ROOT_PASSWORD
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
PATH="$PATH:/usr/local/mysql/bin"
source /usr/local/bin/libC4Rsnapshot.sh
logThis $HOST $MOD "Starting Backup"
HOST=$1
EXIT_CODE=0
# Get list of all databases
if [ "$HOST" = "localhost" ]; then
REMOTECOMMAND=""
DB_LIST=`mysql -Bse "show databases"`
else
REMOTECOMMAND="ssh $HOST"
DB_LIST=`$REMOTECOMMAND mysql -Bse \"show databases\"`
fi
for db in $DB_LIST; do
if [ "$db" != "information_schema" ]; then
FILENAME=$db.sql.gz
#ORIGINAL: $REMOTECOMMAND mysqldump $db | gzip -9 > $FILENAME
if [ "$HOST" = "localhost" ]; then
mysqldump $db | gzip -9 > $FILENAME
else
$REMOTECOMMAND "mysqldump $db | gzip -9" > $FILENAME
EXIT_CODE="$?"
if [ "$EXIT_CODE" -gt "0" ]; then
echo "ERROR while backing MYSQL in $HOST"
fi
fi
chmod 600 $FILENAME
fi
done
logThis $HOST $MOD "Finnished"
exit $EXIT_CODE