工作中用到的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` 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