-
Notifications
You must be signed in to change notification settings - Fork 2
/
bkp-mysql.sh
executable file
·69 lines (58 loc) · 2.45 KB
/
bkp-mysql.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
#
# Version: V1.0 - 07-10-2014
# Author: Gustavo Etchudez - mail: [email protected]
# URL: https://github.com/getchudez
#
# I found this script at internet (I can't remember where) and I made some changes about how to get a list of databases for plesk,
# and create an specific directory to store backup.
# I've another script called clean-old-mysql-bkp.sh, this script should run every day and will delete old backup
#
### change the values below where needed.....
MYSQL_BIN=$(which mysql)
DBNAMES="$MYSQL_BIN `-N -uadmin -p\`cat /etc/psa/.psa.shadow\` -e "show databases;"`"
HOST="--host=localhost"
USER="--user=admin"
PASSWORD="--password=`cat cat /etc/psa/.psa.shadow`"
DAY=`/bin/date '+%y%m%d'`
BACKUP_DIR="/data/backups/mysql/"$DAY
mkdir $BACKUP_DIR
#### you can change these values but they are optional....
OPTIONS="--default-character-set=latin1 --complete-insert --no-create-info --compact -q"
RESTORESCRIPT="$BACKUP_DIR/__restoreData.sql"
DATE=`/bin/date '+%y%m%d_%H%M%S'`
#### make no changes after this....
#### start script ####
echo removing old temporary files if they exists...
rm -f ${BACKUP_DIR}/*.sql > /dev/null 2>&1
rm -f ${BACKUP_DIR}/*.tar > /dev/null 2>&1
rm -f ${BACKUP_DIR}/*.tar.gz > /dev/null 2>&1
cd ${BACKUP_DIR}
for DB in $DBNAMES
do
echo "=========================================="
echo ${DB}
echo "=========================================="
echo 'SET FOREIGN_KEY_CHECKS=0;' > $RESTORESCRIPT
mysqldump --no-data $HOST $USER $PASSWORD $DB > ${BACKUP_DIR}/__createTables.sql
echo 'source __createTables.sql;' >> $RESTORESCRIPT
for TABLE in `mysql $HOST $USER $PASSWORD $DB -e 'show tables' | egrep -v 'Tables_in_' `; do
TABLENAME=$(echo $TABLE|awk '{ printf "%s", $0 }')
FILENAME="${TABLENAME}.sql"
echo Dumping $TABLENAME
echo 'source' $FILENAME';' >> $RESTORESCRIPT
mysqldump $OPTIONS $HOST $USER $PASSWORD $DB $TABLENAME > ${BACKUP_DIR}/${FILENAME}
done
echo 'SET FOREIGN_KEY_CHECKS=1;' >> $RESTORESCRIPT
echo making tar...
tar -cf ${DB}_${DATE}.tar *.sql > /dev/null 2>&1
echo compressing...
gzip -9 ${DB}_${DATE}.tar > /dev/null 2>&1
echo removing temporary files...
rm -f ${BACKUP_DIR}/*.sql > /dev/null 2>&1
rm -f ${BACKUP_DIR}/*.tar > /dev/null 2>&1
echo "done with " $DB
done
echo "=========================================="
echo " done with all databases! "
echo "=========================================="