MySQL Database backup script

Source: Internet
Author: User
#!/bin/sh# 备份数据库 mkdir -p /data/backup/mysql mkdir -p /data/backup/dbdata# Mysql 用户名密码MYSQL_USER=rootMYSQL_PASS=vrv123456.BACKUP_DIR=/data/backup/mysqlDATA_DIR=/data/backup/dbdata# 查询mysql中所有数据库名字SQL_STRING="SELECT SCHEMA_NAME AS db FROM information_schema.SCHEMATA WHERE SCHEMA_NAME NOT IN (‘information_schema‘, ‘performance_schema‘);"DBS=$(echo $SQL_STRING | mysql -u$MYSQL_USER -p$MYSQL_PASS -Bs)# 当前日期DATE=$(date -d ‘+0 days‘ +%Y%m%d%H%M)# 打包名称ZIP_NAME="mysql_"$DATE".gz"# 删除一个月之前的数据库备份find $BACKUP_DIR -name "mysql_*.gz" -type f -mtime +30 -exec rm {} \;# 循环备份数据库for DBNAME in $DBSdo if [ ! -d ${DATA_DIR} ]; then mkdir -p ${DATA_DIR} fi SQL_FILE=$DBNAME-$DATE".sql" /usr/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS $DBNAME > $DATA_DIR/$SQL_FILEdone# tar打包所有sql文件tar -czvPf $BACKUP_DIR/$ZIP_NAME $DATA_DIR# 打包成功后删除sql文件if [ $? = 0 ]; then rm -r $DATA_DIRfi

MySQL Database 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.