CentOS automatically backs up MySQL database every day

Source: Internet
Author: User
Tags bz2

#!/bin/bashpath=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbinexport path# Database user name dbuser= ' root ' # Database with password dbpasswd= ' ******** ' need to back up the database, multiple databases with spaces separated dbname= ' Xiaohuai xiaohuai2 ' #备份时间backtime = ' Date +%y%m%d%h%m%s ' # Log backup path logpath= '/home/mysqlbak ' #数据备份路径datapath = '/home/mysqlbak ' #日志记录头部echo ' "Backup Time is ${backtime}, backup database table ${dbname} start" >> ${logpath}/log.log# official Backup database for table in $dbname; Dosource= ' Mysqldump-u ${dbuser}-p${dbpasswd} ${table}> ${logpath}/${backtime}.sql ' 2>> ${logpath}/ Mysqllog.log; #备份成功以下操作if ["$?" = = 0];thencd $datapath # to conserve hard disk space, compress the database into tar jcf ${table}${backtime}.tar.bz2 ${backtime}. SQL >/dev/null# Delete the original file, leaving only the compressed file rm-f ${datapath}/${backtime}.sql# deleted seven days ago backup, that is, save only 7 days backup find $datapath-name "*. tar.bz2 "-type f-mtime +7-exec rm-rf {} \; >/dev/null 2>&1echo "database table ${dbname} backup succeeded!!" >> ${logpath}/mysqllog.logelse# Backup fails the following Operation Echo database table ${ DBNAME} Backup Failed! ">> ${logpath}/mysqllog.logfidone

CentOS automatically backs up MySQL database every day

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.