forked from yhager/rsync_backup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_backup.sh
executable file
·58 lines (47 loc) · 1.88 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
55
56
57
58
#!/bin/bash
##########################################################################
#
# mysql_backups.sh: A shell script to back up all MySQL databases in
# one shot, nightly, and keep a rolling 3 weeks of
# backups hot, online in the backup archive.
#
# Written by: David A. Desrosiers
# Contact: [email protected]
# Last updated: Mon Feb 12 14:08:33 EST 2007
#
# Copyright 1998-2007. This may be modified and distributed on the same
# terms as the GPL itself. This copyright header
# must remain intact if you use this script.
#
##########################################################################
#####################################
### MySQL Configuration Variables ###
#####################################
. /etc/conf.d/backup
# Local directory for dump files
MYSQL_BACKUP_DIR=/var/backups/mysql/
#####################################
### Edit Below If Necessary #########
#####################################
cd $MYSQL_BACKUP_DIR
DATE=`eval date +%Y-%m-%d`
NOW=`date +'%Y-%m-%d.%H:%M:%S'`
# Optimize the table structure nightly
#mysqlcheck -u$DBUSER -p$DBPASSWD -h$DBHOST -oA
DBS=`mysql -u$DB_USER -p$DB_PASS -h$DB_HOST -e"show databases"`
for DATABASE in $DBS
do
if [ $DATABASE != "Database" ]; then
echo "Dumping $DATABASE now..."
#BASE=${DATE}.${DATABASE}
BASE=${DATABASE}
${MYSQLDUMP} -u$DB_USER -p$DB_PASS -h$DB_HOST --lock-tables --add-drop-table --skip-dump-date -e $DATABASE > ${BASE}.sql
${BZIP2} -f9 ${BASE}.sql
chmod 0400 ${BASE}.sql.bz2
#7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on ${BASE}.7z ${BASE}.sql && rm ${BASE}.sql
#chmod 0400 ${BASE}.7z
fi
done
# Delete files older than 21 days
for i in `find $MYSQL_BACKUP_DIR -mtime +21|sort`; do (rm $i); done;
exit 0