本文共 2898 字,大约阅读时间需要 9 分钟。
#!/bin/sh## created by yejr,2007/06/03## 本脚本用于定期做全量备份,备份的对象是slave上的全部数据# 每次备份之前都先执行"STOP SLAVE; FLUSH TABLES;",然后将所有文件拷贝到备份目录下#echo "[backupmysql_fullly]"datetoday=`date +"%Y_%m_%d"`logdir=/logs/mysqldatadir=/data/mysqlbkdir=/backup/mysql/$today/full_backupexpdays=7if [ ! -d $bkdir/$today ] ; then mkdir -p $bkdirfi#停止同步,刷新日志,刷新表echo "stop slave; flush logs; flush tables;"mysqladmin -uroot stop-slavemysqladmin -uroot flush-logsmysqladmin -uroot flush-tablescd $logdir#备份日志文件echo "backup errro log"cp $logdir/error.log* $bkdir/echo "backup ib_logfile* master.info relay.info"cp ib_logfile* master.info relay.info $bkdir/#拷贝所有文件echo "backup my.cnf"cp /usr/local/mysql/my.cnf $bkdir/##如果是老版本的 FreeBSD 不支持快照功能,就拷贝全部数据库文件#cd $datadir#cp -rf * $bkdir/##如果是新版本的 FreeBSD,则采用其特有的 snapshot 功能,先制作镜像,而后挂载镜像,再拷贝数据文件echo "make snapshot of /backup"/sbin/mksnap_ffs /data/ /data/.snap/mysql_snap_$today#执行 slave-start;mysqladmin -uroot start-slave#挂载镜像文件,而后拷贝数据/sbin/mdconfig -a -t vnode -f /data/.snap/mysql_snap_$today -u 4/sbin/mount -r /dev/md4 /backup/.snapcp -rf /backup/.snap/mysql/data/* $bkdir//sbin/umount /backup/.snap/sbin/mdconfig -d -u 4#删除7天前的过期文件find /backup/mysql -type d -mtime +$expdays -maxdepth 1 | xargs rm -rffind /data/.snap -type f -mtime +$expdays -maxdepth 1 | xargs rm -f#修改备份文件的属主chown -R nobody:nobody $bkdirecho "[/backupmysql_fullly]"
0 0 * * * (/bin/sh /backup/mysql/backupmysql_fullly.sh >> /backup/mysql/backupmysql.log)
#!/bin/sh## created by yejr,2007/06/03## 本脚本用于定期做增量备份,备份的对象是binlog# 每次备份之前都先执行 flush-logs,然后将最近的binlog移动到备份目录下#echo "[backupmysql_hourly]"datelogdir=/logs/mysqlbkdir=/backup/mysqlnow=`date +"%Y_%m_%d_%H"`today=`date +"%Y_%m_%d"`if [ ! -d $bkdir/$today ] ; then mkdir -p $bkdir/$todayfi#执行 "flush logs"echo "flush logs; backup error log"mysqladmin -uroot flush-logs#去的 binlog 文件总数,去掉最后一个以及 binlog.indextotal=`ls $logdir/logbin.*|wc -l`total=`expr $total - 2`#循环移动所有的 binlog 文件echo "backup binary logs"for f in `ls $logdir/logbin.*|head -n $total`do bf=`basename $f` mv -if $f $bkdir/$today/$bfdonechown -R nobody:nobody $bkdirecho "[/backupmysql_hourly]"
0 */` * * * (/bin/sh /backup/mysql/backupmysql_hourly.sh >> /backup/mysql/backupmysql.log)