MySQL Enables automatic database backup

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.