Backup MYSQL database with linux modified version mysqldump
- 2020-05-06 11:47:56
- OfStack
Because MYSQLDUMP adds a global lock to the entire library.
If you simply use MYSQLDUMP for full library backup, the following three aspects will be affected.
1. Server CPU is severely blocked.
2, disk I/O line increase.
3. All queries become slow queries.
My current website database is about 5 G, and it is getting bigger every day.
The table structure is a mixture of MYISAM,INNODB and MEMORY.
So I'm afraid it's a little difficult to just use the HOTCOPY tool. So today I simply changed the script I wrote last time about backing up MYSQL with OUTFILE.
The three disadvantages mentioned above can be solved.
1. Backup script contents
[david_yeung@localhost ~]$ cat fast_backup
#!/bin/sh
#
# Created by david yeung.
#
# 20080707.
#
# Backup mysql's full data.
#
DBNAME=$1
BACKUPDIR=/home/david_yeung/backup_new
USERNAME=backup_user
PASSWD=123456
TARNAME="$BACKUPDIR"/backup"$1"`date '+%Y%m%d'`
# Add your own database name here.
case "$1" in
my_site);;
*) exit;;
esac
# Get all the tables' name.
NUM=`/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -s -vv -e "show tables" -D $DBNAME|wc -l`
HEADNUM=`expr ${NUM} - 3`
TAILNUM=`expr ${NUM} - 7`
ARR1=`/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -s -vv -e "show tables" -D $DBNAME| head -n"$HEADNUM" | tail -n "$TAILNUM"`
ARR2=($ARR1)
i=0
while [ "$i" -lt "${#ARR2[@]}" ]
do
tmpFileName=${ARR2[$i]}
# The real dump process.
/usr/local/mysql/bin/mysqldump -u$USERNAME -p"$PASSWD" "$DBNAME" "$tmpFileName" > > "$TARNAME"
let "i++"
done
2. Because we always use stored procedures, we have to back them up separately.
[david_yeung@localhost ~]$ cat fast_sp
#!/bin/sh
# Created by david yeung 20080122.
#
# Backup site's routine.
TARNAME=/home/david_yeung/backup_new/spBackup"$1"`date '+%Y%m%d'`
/usr/local/mysql/bin/mysqldump -ubackup_user -p123456 -n -t -d -R my_site > "$TARNAME"
3. Drop it in the scheduled task and leave it there.
[root@localhost backup_new]# crontab -l
0 01 * * * /home/david_yeung/fast_backup my_site
0 0 * * 5 /home/david_yeung/fast_sp
Backup data at 1 a.m. every day and stored procedures every Friday morning.
If you simply use MYSQLDUMP for full library backup, the following three aspects will be affected.
1. Server CPU is severely blocked.
2, disk I/O line increase.
3. All queries become slow queries.
My current website database is about 5 G, and it is getting bigger every day.
The table structure is a mixture of MYISAM,INNODB and MEMORY.
So I'm afraid it's a little difficult to just use the HOTCOPY tool. So today I simply changed the script I wrote last time about backing up MYSQL with OUTFILE.
The three disadvantages mentioned above can be solved.
1. Backup script contents
[david_yeung@localhost ~]$ cat fast_backup
#!/bin/sh
#
# Created by david yeung.
#
# 20080707.
#
# Backup mysql's full data.
#
DBNAME=$1
BACKUPDIR=/home/david_yeung/backup_new
USERNAME=backup_user
PASSWD=123456
TARNAME="$BACKUPDIR"/backup"$1"`date '+%Y%m%d'`
# Add your own database name here.
case "$1" in
my_site);;
*) exit;;
esac
# Get all the tables' name.
NUM=`/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -s -vv -e "show tables" -D $DBNAME|wc -l`
HEADNUM=`expr ${NUM} - 3`
TAILNUM=`expr ${NUM} - 7`
ARR1=`/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -s -vv -e "show tables" -D $DBNAME| head -n"$HEADNUM" | tail -n "$TAILNUM"`
ARR2=($ARR1)
i=0
while [ "$i" -lt "${#ARR2[@]}" ]
do
tmpFileName=${ARR2[$i]}
# The real dump process.
/usr/local/mysql/bin/mysqldump -u$USERNAME -p"$PASSWD" "$DBNAME" "$tmpFileName" > > "$TARNAME"
let "i++"
done
2. Because we always use stored procedures, we have to back them up separately.
[david_yeung@localhost ~]$ cat fast_sp
#!/bin/sh
# Created by david yeung 20080122.
#
# Backup site's routine.
TARNAME=/home/david_yeung/backup_new/spBackup"$1"`date '+%Y%m%d'`
/usr/local/mysql/bin/mysqldump -ubackup_user -p123456 -n -t -d -R my_site > "$TARNAME"
3. Drop it in the scheduled task and leave it there.
[root@localhost backup_new]# crontab -l
0 01 * * * /home/david_yeung/fast_backup my_site
0 0 * * 5 /home/david_yeung/fast_sp
Backup data at 1 a.m. every day and stored procedures every Friday morning.