MySQL Backup shell Footsteps

Source: Internet
Author: User
Tags mysql backup

#!/bin/bash
#Shell Command for Backup MySQL Database everyday automatically by Crontab

User=root
password= "Admin"
Database= "Nansha_car_manager"
Hostname= "localhost"


backup_dir=/home/mysql_back/#备份文件存储路径
Logfile=/home/mysql_back/data_backup.log #日记文件路径
Date= ' Date ' +%y%m%d-%h%m ' #日期格式 (as file name)
dumpfile= $DATE. SQL #备份文件名
Archive= $DATE. Sql.tgz #压缩文件名
options= "-h$hostname-u$user-p$password $DATABASE"
#mysqldump-help

#判断备份文件存储目录是否存在, otherwise create the directory
if [!-D $BACKUP _dir];
Then
Mkdir-p "$BACKUP _dir"
Fi

#开始备份之前, writes the backup information header to the journal file
echo "" >> $LOGFILE
echo "" >> $LOGFILE
echo "——————————————— –" >> $LOGFILE
echo "BACKUP Date:" $ (date + "%y-%m-%d%h:%m:%s") >> $LOGFILE
echo "——————————————— –" >> $LOGFILE

#切换至备份目录
CD $BACKUP _dir
#使用mysqldump command Backup to make a database and name the backup file with a formatted timestamp
Mysqldump $OPTIONS > $DUMPFILE
#判断数据库备份是否成功
if [[$ = = 0]]; Then
#创建备份文件的压缩包
Tar czvf $ARCHIVE $DUMPFILE >> $LOGFILE 2>&1
#输入备份成功的消息到日记文件
echo "[$ARCHIVE] Backup successful! >> $LOGFILE
#删除原始备份文件, just keep the archive of your database backup files
Rm-f $DUMPFILE
The #-ctime represents the creation time, which indicates how many days before the deletion was created, that is, the data is retained for only the day of the result.
Find $BACKUP _dir-ctime +7-type f-name "*.tgz"-print > Deleted.txt
Echo-e "Delete files:\n" >> $LogFile
#循环删除匹配到的文件
Cat Deleted.txt | While Read line
Do
RM-RF $LINE
echo $LINE >> $LogFile
Done
Else
echo "Database Backup fail!" >> $LOGFILE
Fi
#输出备份过程结束的提醒消息
echo "Backup Process Done"

# Write day to Cron job
# CRONTAB-E
# */1 * * * * */home/auto_log.sh >/dev/null 2>& #shell脚本目录

MySQL Backup shell Footsteps

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.