Shell implementation automatically backs up the entire database, one library backs up a file

Source: Internet
Author: User

Automatic backup of the entire database
    • Implement a library to back up a file
    • Implement exclude libraries that do not require backup
    • Implementing Backup to Compressed files
    • Implementing a backup file that defines how many days to retain
Core code
1#!/bin/Bash2#set-x3 #########################4 # function: Automatically backs up the entire MySQL database5 # Author: Time Blog6# Release Time:2018/ ./Geneva7 # Last modified:8# version: v1.09 ########################Ten ########################### #参数定义 ######################## One #用户名 AUser="Root" - #密码 -pass="Root" the# to set up a backup directory, you mustEnd -back_dir=/data/backup/ - #设置保存多少天 -day= the + #定义文件名 -File_name= 'Date+%y%m%d%h%M ' + #定义排除的数据库名称 AExclude= ("Database" "Performance_schema" "Information_schema" "MySQL") at ############################ #代码逻辑 ######################## - #定义命令 -Mysql= 'whichMySQL ' -Mysqldump= 'whichmysqldump ' - #创建一个临时文件, loading the database -tmp_file=/tmp/Databases_$random in Touch$tmp _file - #登陆mysql, get all the database names to$MYSQL-u$user-p$pass <<eof >$tmp _file + show databases; - EOF the #定义数据库名称数组 *database_list=() $ #定义自增变量Panax Notoginsenglen=0 - #获取真实有用的数据库名称 the  whileRead Line +  Do A         if[["${exclude[@]}"! = * $line * ]] the          Then +Let"len++" -database_list[$len]=$line $         fi $  Done<$tmp _file - #输出结果, export the database - Echo "Database total: ${#database_list [*]}" the #开始备份 - Echo "Start Backup ..."Wuyird=$RANDOM the  forNameinch${database_list[@]} -  Do WuFile_path="${back_dir} ' date +%y%m%d '"/ -         if[ ! -e $file _path] About          Then $                 mkdir-P $file _path -         fi -         file="${file_path}${name}-$file _name-$rd. GZ" -$MYSQLDUMP--opt $name-u $user-p${pass} |gzip> $file A  Done + #保留多少天的数据 the Find$basedir-mtime + $day-name"*.gz"-execRM-RF {} \; - #删除临时文件 $ if[ -e $tmp _file] the  Then the         RM-RF $tmp _file the fi the Echo "end of backup."

Turn From Time Blog

Shell implementation automatically backs up the entire database, one library backs up a file

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.