mysql自动备份脚本 发表于 2019-09-18 | 更新于 2019-09-22 | 分类于 linux mysql自动备份脚本 脚本编写123456789101112131415161718192021222324252627282930313233[root@localhost ~]# mkdir /home/bak_data[root@localhost ~]# mkdir /home/bak_data/log[root@localhost ~]# vi bak_mysql.sh#! /bin/bash# name fengkUSER=rootPASSWORD=P@ssW0rdDATABASE1=infoveriplatform_zhuozhouBACKUP_DIR=/home/bak_dataLOGFILE=/home/bak_data/log/bak_data.logDATE=`date +%Y%m%d-%H%M -d -3minute`DUMPFILE1=$DATE-${DATABASE1}.sqlARCHIVE1=$DUMPFILE1.gzKEEPTIME=1if [ ! -d $BACKUP_DIR ];thenmkdir -p "$BACKUP_DIR"fiecho -e "\n" >> $LOGFILEecho "-----------------------------------------------------" >> $LOGFILEecho "BACKUP DATE:$DATE" >> $LOGFILEecho "-----------------------------------------------------" >> $LOGFILEcd $BACKUP_DIR/home/mysql/bin/mysqldump --triggers --routines --events -h127.0.0.1 -P3306 -u$USER -p$PASSWORD --databases $DATABASE1 | gzip > $ARCHIVE1if [[ $? == 0 ]];thenDELFILE=`find $BACKUP_DIR -type f -mtime +${KEEPTIME} -exec rm -fr {} \;`echo "delete expire file " >> $LOGFILEecho "-----------------------------------------------------" >> $LOGFILEfi[root@localhost ~]# chmod +x bak_mysql.sh 设置定时任务123456789101112131415161718[root@localhost ~]# vi /etc/corntabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root# For details see man 4 crontabs# Example of job definition:# .---------------- minute (0 - 59)# | .------------- hour (0 - 23)# | | .---------- day of month (1 - 31)# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat# | | | | |# * * * * * user-name command to be executed 30 00 * * * root /root/bak_mysql.sh[root@localhost ~]# systemctl restart crond