-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-backup.sh
executable file
·54 lines (44 loc) · 1.36 KB
/
mysql-backup.sh
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
#!/bin/bash
# Database credentials
USER=""
PASS=""
HOST=""
PORT="3306"
#* MySQL binaries *#
MYSQL=`which mysql`;
MYSQLDUMP=`which mysqldump`;
GZIP=`which gzip`;
# DO NOT BACKUP these databases
IGNOREDBS="information_schema mysql test"
# The directory to store the backup. Do not add a trailing slash.
BACKUPDIR="/var/backups/mysql"
DATE=$(date +"%d-%b-%Y")
# Set default file permissions
umask 177
# Check is backup dir exists.
if [ ! -d $BACKUPDIR ]; then
echo "Error: The directory $BACKUPDIR does not exist"
exit 1
fi
# get all database listing
DBS="$($MYSQL -u $USER -p$PASS -h $HOST -P $PORT -Bse 'show databases')"
for db in $DBS:
do
DUMP="yes";
if [ "$IGNOREDB" != "" ]; then
for i in $IGNOREDB # Store all value of $IGNOREDB ON i
do
if [ "$db" == "$i" ]; then
DUMP="NO";
#echo "$i database is being ignored!";
fi
done
fi
if [ "$DUMP" == "yes" ]; then
FILE="$BACKUPDIR/$DATE-$db.gz";
echo "BACKING UP $db";
$MYSQLDUMP --max_allowed_packet=500M --routing --triggers --add-drop-database --opt --lock-all-tables -u $USER -p$PASS -h $HOST -P $PORT $db > $FILE
fi
done
# Delete files older than 30 days
find $BACKUPDIR/* -mtime +30 -exec rm {} \;