Linux shell script backup MySQL Database

Source: Internet
Author: User
#!/bin/sh# Backup Database # Mysql user name Password mysql_user=rootmysql_pass=rootbackup_dir=/data/backup/mysqldata_dir=/data/backup/ dbdata# queries all database names in MySQL sql_string= "select Schema_name as db from INFORMATION_SCHEMA. Schemata WHERE schema_name not in (' MySQL ', ' information_schema ', ' Performance_schema '); dbs=$ (echo $SQL _string | mysql-u$mysql_user-p$mysql_pass-bs) # current date date=$ (date-d ' +0 days ' +%y%m%d%h%m) # package name Zip_name= " Mysql_ "$DATE". gz "# Delete database backup one months ago Find $BACKUP _dir-name" mysql_*.gz "-type f-mtime +30-exec rm {} \;# loop back up database for DBNAME I n $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 package all SQL files TAR-CZVPF $BACKUP _dir/$ Zip_name $DATA _dir# After the package succeeds, delete the SQL file if [$ = 0]; Then    rm-r $DATA _dirfi

  

Linux shell script backup MySQL Database

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.