MySQL backup script

Source: Internet
Author: User
Tags mysql backup

#!/bin/bash


Dbname= User name #DB数据库

Backup_dir=/data/mysqlbackup/databackup #备份目录

dbrootpwd= Password #DB用户名root密码:

Expired_days=7 #BD备份存储7天


logfile= $backup _dir/db.log #日志文件

dumpfile= $backup _dir/db_lelaohui_$ (date +%y%m%d_%h). SQL #备份数据库文件

newfile= $backup _dir/db_lelaohui_$ (date +%y%m%d_%h). tgz #新增备份数据库文件

oldfile= $backup _dir/db_lelaohui_$ (date +%y%m%d--date= "$expired _days days Ago") *.tgz #旧的备份文件 "7 day ago" means 7 hours ago


[!-e "$backup _dir"] && mkdir-p $backup _dir #判断备份目录/data/mysqlbackup/databackup does not exist, then create a new backup directory


If [-n "' ls $OldFile 2>/dev/null '"];then #当串的长度大于0时为真 (string non-null), the 2>/DEV/NULL representative does not output an error message.

/bin/rm-f $OldFile

echo "[$OldFile] Delete old File Success" >> $LogFile

Else

echo "[$OldFile] Delete old Backup File" >> $LogFile

Fi


If [-E "$NewFile"];then #如果新文件存在, then the output log is then not backed up

echo "' Date +%y%m%d_%h ': [$NewFile] The backup File is exists, Can ' t Backup ' >> $LogFile

Else #否则创建新的备份文件, go to the backup path and compress the new backup database file. tgz, delete the backup. sql file.

/usr/bin/mysqldump-uroot-p$dbrootpwd-h10.66.126.90--opt--databases Dinner LLH llh_bomb > $DumpFile

CD $backup _dir

Tar czf $NewFile ${dumpfile##*/} >> $LogFile 2>&1 #${file##*/}: Delete the last/and left string: My.file.txt compress new Back up the database, and delete the. SQL database

echo "' Date +%y%m%d_%h ': [$NewFile] Backup Success" >> $LogFile #追加打印备份成功的日志

/bin/rm-f $DumpFile

Fi


MySQL backup script

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.