Linux VPS Backup Database script (delete 10 before backup)

Source: Internet
Author: User
Tags vps

Join the Crontab timed task to perform daily. The effect is: Back up the database to the specified directory every day, delete the files backed up 10 days ago.

Here's what the script reads:

The code is as follows Copy Code


#!/bin/bash
Mysql_host= "127.0.0.1"
Mysql_user= "Root"
Mysql_passwd= "********"

Back_dir= "/www/backup/database/"

ignore_db= "Information_schema|mysql|performance_schema"

Db_arr= ' Mysql-u${mysql_user}-p${mysql_passwd}-h${mysql_host}-bse "show databases;" | Grep-ve "(${ignore_db})" '

datetime=$ (Date +%y-%m-%d-%h_%m_%s)

For dbname in ${db_arr}
Todo
Mysqldump-u${mysql_user}-p${mysql_passwd}-h${mysql_host} ${dbname} | gzip > ${back_dir}${dbname}-${datetime}.sql.gz
Done

Find ${back_dir}-mtime +10-name "*.*"-exec rm-rf {};

The backup file cannot be generated correctly on the Aliyun server crontab the automatic backup database.

Check it out and see that all the settings are correct. Ah, it's strange that this script runs on my American VPS.

And, I directly execute the. sh file run script, all normal OH. Did crontab not perform at all?

So I set the crontab to execute in a minute and then add:>/web/test.log to the back 2>&1

A minute later, I looked at the generated log file and found the problem:

The code is as follows Copy Code

Mysql-u${mysql_user}-p${mysql_passwd}-h${mysql_host}

Mysqldump-u${mysql_user}-p${mysql_passwd}-h${mysql_host} ${dbname}

The above two statements fragment, MySQL and mysqldump are not recognized, and then changed to:

/usr/local/mysql/bin/mysql and/usr/local/mysql/bin/mysqldump (my MySQL installation directory is/usr/local/mysql)

Problem solved, this should be the problem of environmental variables.

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.