工作中用到的Mysql单库备份Shell脚本,压缩备份,并在Crontab中添加计划任务,最多保存60天的备份
#!/bin/bash . /etc/profile USERNAME=zabbix PASSWORD=xxxxx DBHOST=127.0.0.1 DATABASES=zabbix BACKUPDIR=/data/mysqlback/zabbix/fulldb DATE=`date +%Y-%m-%d_%H:%M:%S` rm -fv ${BACKUPDIR}/*$(date +%Y-%m-%d -d "60 days ago")* BACKUPFILE=zabbix_full_db_${DATE}.sql.gz /bin/mysqldump --single-transaction --user=${USERNAME} --password=${PASSWORD} --host=${DBHOST} --databases $DATABASES |gzip >${BACKUPDIR}/${BACKUPFILE} #增加 --single-transaction 选项防止备份时锁表
crontab中添加:
00 03 * * * /bin/bash /opt/zabbix_mysql_bak.sh
解压缩:
gzip -d xx.sql.gz
安装percona-xtrbackup:
yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm yum list|grep -i percona yum -y install percona-xtrabackup-24.x86_64
只备份指定数据库并自动创建名为时间的文件夹
innobackupex --user=root --password=123456 --databases=blog /data/backups/full/
全备: 也会自动创建时间文件夹
innobackupex --user=root --password=123456 /data/backups/full/