MySQL full script, this script can back up multiple databases, separate folders

Source: Internet
Author: User
Tags mysql backup

#!/bin/bash
#mysql全备脚本, this script can back up multiple databases,
#为每个数据建立单独的文件夹进行备份


#author: Yxy
#website: Www.sql8.net
#mail: [Email protected]
#created: 2014-8-14


#Mysql dabase Information
#主机
db_host= "localhost"
#数据库帐号
Db_user= "Root"
#数据库密码
Db_passwd= "12356789"
#mysql安装目录
Mysql= "/opt/lampp/bin/mysql"
#mysql命令目录
mysqldump= "/opt/lampp/bin/mysqldump"




#Path Information
#存放备份文件的路径
backup_db= "/opt/backup/database"
logfile= $BACKUP _db "/bak.log"


#Time Information
#时间
Time= ' date + '%y-%m-%d-%h-%m-%s '
day= ' date + '%d '
Month= ' date + '%y-%m '
weekday= ' date + '%u '






#Path Enable Write
if [!-W "$BACKUP _db"]; Then
Chmod-r $BACKUP _db
Fi


#echo "**********************************host info****************************************"
#echo "Db_host:" $db _host
#echo "Db_user:" $db _user
#echo "DB_PASSWD:" $db _passwd
#echo "MYSQL:" $MYSQL
#echo "MYSQLDUMP:" $MYSQLDUMP
#echo "DatabaseName:" $databaseName
#echo "backup_db:" $BACKUP _db
#echo "**********************************************************************************"




#Mysql Backup
Date= ' Date +%y%m%d '
begin= ' date + '%y-%m-%d%h:%m:%s '
#echo "Start Backup database:" $databaseName "" $Begin


#databases
#需要备份的数据库, if there are multiple databases, use, separate
Databaselist= "DD1,DD2"
For databaseName in ' Echo ' $databaseList | Sed ' s/,/\n/g '
Do
Echo $databaseName


#fileName
#生成备份文件名
Filename= $databaseName "-" $time ". sql"
#生成备份文件完整路径
Backup_dbpath= $BACKUP _db "/" $databaseName




#echo "***********************************database info***********************************************"
#echo "Backup_dbpath:" $BACKUP _dbpath
#echo "FileName:" $fileName
#echo "**********************************************************************************"





if [!-d "$BACKUP _dbpath"]; Then
mkdir "$BACKUP _dbpath"
Fi
#备份mysql
$MYSQLDUMP-u $db _user-p$db_passwd-h $db _host $databaseName > $BACKUP _dbpath/$fileName
#打包. sql file
CD $BACKUP _dbpath && tar-czf $fileName. tar.gz $fileName && rm-rf $fileName && chmod go-rwx $file Name.tar.gz


last= ' date + '%y-%m-%d%h:%m:%s '
#echo "End Backup Database:" $databaseName "$Last
#输出日志
echo Start: $Begin end: $Last $fileName succ >> $LogFile
#关于备份文件, this script is not processed, if necessary, can contact landlord
Done


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.