1. New. sh file, content is
# encoding:utf-8#配置参数 USER=Root #数据库用户名 PASSWORD=123456#数据库用户密码 DATABASE=CNM #数据库名称 WEBMASTER="[email protected]"#管理员邮箱地址 to send a backup failure message reminder Backup_dir=/home/huilife_backup#备份文件存储路径LOGFILE=/home/huilife_backup/backup.log #日志文件路径 DATE=`Date+"%y%m%d-%h%m"' #日期格式 (as filename) filename=$DATABASE $date.gz #压缩文件名OPTIONS="-u$user-p$password CNM | gzip"#mysqldump parameter details see Help mysqldump-help #判断备份文件存储目录是否存在, otherwise create this directoryif[!-d $BACKUP _dir]; Then mkdir-P"$BACKUP _dir"fi#开始备份之前, writes the backup information header to the journal fileEcho "——————————————— –">>$LOGFILEEcho "BACKUP DATE:"$(Date+"%y-%m-%d%h:%m:%s") >>$LOGFILE #切换至备份目录 cd $BACKUP _dir #使用mysqldump command Backup to make a database and name the backup file with a formatted timestamp mysqldump-u$user-p$password $DATABASE |gzip> $BACKUP _dir/$FILENAME #判断数据库备份是否成功if[[ $? ==0]]; Then#输入备份成功的消息到日记文件Echo "[$BACKUP _dir/$DATABASE $filename] BACKUP successful!">>$LOGFILEElse Echo "Database Backup fail!">>$LOGFILE #备份失败后向网站管理者发送邮件提醒, you need the support #如果需要发邮件 of mailutils or similar terminal to send mail tool, and mail is configured, remove the following commentEcho "database backup error, please check in time! Time:"`Date+"%y%m Month%d day%h point%m min"` | Mail-s"Database Database Daily Backup Fail"$WEBMASTERfi#输出备份过程结束的提醒消息Echo "Backup Process Done"
The core content is:
Filename= 'date +%y%m%d 'gzip >/root/huilife_backup/cnm$filename.gz
(1) Email alert function dependent SendMail
2. Auxiliary execution permission and test
chmod +x autobackup. /autobackup
3. Implement automatic Backup
Require cron service installation (scheduled Task Management Server)
VI /etc/crontab
Last added: (Daily 18:45)
$ * * * * Root /root/autobackup
done!
MySQL Enables automatic database backup