阅读更多
一周循环完全备份
datestr=`date '+%Y%m%d'`;
weekday=`date '+%w'`;
folder='/home/mysql_backup';
printf "$datestr --> $weekday.sql\r\n" >> "$folder/backup.log"
mysqldump --opt --password=yourpass --all-databases > "$folder/$weekday.sql";
增量备份
转:http://blog.chinaunix.net/u/29134/showart_1194259.html
#!/bin/sh
#
# Created by david yeung at 2008-09-16
#
# File name:backup_increment
#
# Backup mysql's increment data after daily backup.
#
# Usage:
# If you database name is t_girl and you want to backup from 2008-09-16 08:13:26.
# ./backup_increment t_girl 20080916081326
#
# You can simple put this file into crontab in order to work automatically.
#
# Put your own database name here.
DBNAME=$1
# You own specific datatime.
# For example,2008-09-16 08:13:26 should be replaced with 20080916081326 here.
#
USERDATETIME=$2
# You own backup directory.
BACKUPDIR=/home/david_yeung/backup_db
# You own backup user.
# He must have lock tables,select,super privileges.
# Also replication client privileges if your mysqld was set up as Master/Slave.
USERNAME=backup_user
PASSWD=123456
# Socket path.
SOCKET=/tmp/mysql_3306.sock
# All the binary logs.
LOG=/usr/local/mysql/data/ytt-bin.[0-9]*
# Backup file name.
TARNAME="$BACKUPDIR"/incrementbackup"$1""$2"
# Copy all the binary logs here to prevent failed backup.
cp -rf `echo $LOG` /home/david_yeung/logs
# Change the current directory.
# If you have aready put mysql into your environment variable named path,you can comment this line and use simple mysql instead of /bin/mysql.
#
cd /usr/local/mysql
bin/mysql -u$USERNAME -p"$PASSWD" -S$SOCKET -e "purge master logs before ${USERDATETIME}"
bin/mysqlbinlog -u$USERNAME -p"$PASSWD" -S$SOCKET -d$DBNAME `echo $LOG` --start-datetime="$USERDATETIME"> "$TARNAME"
指定数据库完全备份
假设有三个库
m_site,m_bbs,m_cms.
#!/bin/sh
#
# MySQL Backup Scripts.
# Created by david.
#
# Created time:2008-01-09 11:10:49
#
USERNAME=backup_user
PASSWORD=123456
case "$1" in
m_site) BACKUP_FILE=/home/david_yeung/backup/backup_m_site`date '+%Y%m%d_%H%M%S'`.sql ;;
m_bbs) BACKUP_FILE=/home/david_yeung/backup/backup_m_bbs`date '+%Y%m%d_%H%M%S'`.sql ;;
m_cms) BACKUP_FILE=/home/david_yeung/backup/backup_m_cms`date '+%Y%m%d_%H%M%S'`.sql ;;
*) ;;
esac
# Start backup.
/usr/local/mysql/bin/mysqldump -u$USERNAME -p$PASSWORD -B $1 --add-drop-table -R > $BACKUP_FILE
# Compress backup file.
gzip $BACKUP_FILE
加入到CRONTAB里面去。
[david_yeung]# crontab -l
0 0 * * * /home/david_yeung/mysqlbackup_david m_site
0 0 * * * /home/david_yeung/mysqlbackup_david m_bbs
0 0 * * * /home/david_yeung/mysqlbackup_david m_cms
这个脚本来配合原来的备份脚本做善后工作,只保留最新三天内的备份。
文件名称示例:
[root@mysql131-3 backup]# ls -sihl
total 1.2G
11075627 4.0M -rw-r--r-- 1 root root 4.0M Jan 27 01:00 backup_bbs20080127_010001.sql.gz
14843905 4.0M -rw-r--r-- 1 root root 4.0M Jan 28 01:00 backup_bbs20080128_010001.sql.gz
11075601 4.0M -rw-r--r-- 1 root root 4.0M Jan 29 01:00 backup_bbs20080129_010001.sql.gz
11075629 44K -rw-r--r-- 1 root root 39K Jan 27 01:00 backup_cms20080127_010001.sql.gz
14843908 44K -rw-r--r-- 1 root root 39K Jan 28 01:00 backup_cms20080128_010001.sql.gz
11075608 44K -rw-r--r-- 1 root root 39K Jan 29 01:00 backup_cms20080129_010001.sql.gz
11075628 403M -rw-r--r-- 1 root root 403M Jan 27 01:00 backup_site20080127_010001.sql.gz
14843906 403M -rw-r--r-- 1 root root 403M Jan 28 01:00 backup_site20080128_010001.sql.gz
11075602 403M -rw-r--r-- 1 root root 403M Jan 29 01:00 backup_site20080129_010001.sql.gz
脚本内 容:
#!/bin/sh
#
# Created by david yeung 20080129.
#
# Delete overdue mysql backup file.
#
# Directory to list.
DIRNAME=/home/david_yeung/backup/
# Get the date part.
ARR1=`ls $DIRNAME | cut -d '_' -f3`
# Get three day ago.
CUR_DATE=`date +'%Y%m%d' -d '-3 day'`
# Delete the overdue file.
for CUR_FILENAME in $ARR1
do
RESULT=`echo $CUR_FILENAME | tr -d "[a-z]"`
if [ "$RESULT" -le "$CUR_DATE" ]
then
rm -rf `echo "${DIRNAME}backup_${CUR_FILENAME}_010001.sql.gz"`
fi
done
转于:http://itlife365.com/?post=44