Shell script Backup Database (docu)

Source: Internet
Author: User
Tags bz2

#!/bin/bash
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
Export PATH
#数据库用户名
Dbuser= ' Root '
#数据库用密码
dbpasswd= ' Password '
#需要备份的数据库, multiple databases are separated by spaces
Dbname= (' dbname1 ' dbname2 ' dbname3 ' dbname4 ' dbname5 ')
#备份时间
Backtime= ' Date +%y%m%d%h%m%s '
#mysql安装路径
Mysqlpath= '/data/mysql5.6/bin '
#日志备份路径
Logpath= '/store/bank/data '
#数据备份路径
Datapath= '/store/bank/data '
#日志记录头部
Echo ' "Backup Time is ${backtime}, back Up database table ${dbname} start" >> ${logpath}/log.log
#正式备份数据库 take the above variable array
For db_name in ${dbname[@]}; Do
#mysqldump-U ${dbuser}-p${dbpasswd} ${db_name}> ${logpath}/${backtime}.sql 2>> ${logpath}/mysqllog.log
Source= ' ${mysqlpath}/mysqldump-u${dbuser}-p${dbpasswd} ${dbname}> ${logpath}/${db_name}${backtime}.sql ' 2> > ${logpath}/mysqllog.log;
#备份成功以下操作
If ["$?" = = 0];then
CD $datapath
#为节约硬盘空间, compress the database
Tar jcvf ${db_name}${backtime}.tar.bz2 ${table}${backtime}.sql >/dev/null
#删除原始文件, leave only compressed files
Rm-f ${datapath}/${db_name}${backtime}.sql
#删除七天前备份, which is to save only 7 days of backup
Find $datapath-name "*.tar.bz2"-type f-mtime +7-exec rm-rf {} \; >/dev/null 2>&1
echo "database table ${dbname} backup succeeded!!" >> ${logpath}/mysqllog.log
Else
#备份失败则进行以下操作
echo "database table ${dbname} backup failed!" >> ${logpath}/mysqllog.log
Fi
Done

Shell script Backup Database (docu)

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.