Centos script automatically backs up databases

Source: Internet
Author: User
Compile the script [plain] viewplaincopy [root @ iZ232s4zbqvZservice] # vimysql_dump.sh #! /Bin/sh

Write scripts

[Plain]View plaincopy
  1. [Root @ iZ232s4zbqvZ service] # vi mysql_dump.sh
  2. #! /Bin/sh
  3. Echo "mysqldump start ..."
  4. DB_NAME = "imms_core"
  5. DB_USER = "root"
  6. DB_PASS = "smithhuang"
  7. BIN_DIR = "/usr/bin"
  8. YEAR = 'date + % Y'
  9. BCK_DIR = "/data/mysql_dump/" $ YEAR/
  10. DATE = 'date + % Y % m % d _ % H % M % s'
  11. Mkdir-p $ BCK_DIR
  12. $ BIN_DIR/mysqldump-u $ DB_USER-p $ DB_PASS $ DB_NAME | gzip> $ BCK_DIR/mongodb_name.dump_mongodate. SQL .gz
  13. Sleep 1
  14. #####
  15. DB_NAME = "imms_app"
  16. $ BIN_DIR/mysqldump-u $ DB_USER-p $ DB_PASS $ DB_NAME | gzip> $ BCK_DIR/mongodb_name.dump_mongodate. SQL .gz
  17. Echo "mysqldump end at" $ DATE
Note:

 

Directory/data/mysql_dump/and directory/data/logs/mysql should have www user permissions, and it is best to change it to 777 permissions.

 

Add the script to the scheduled task (last line) (executed at every day)

Switch to the www user first

[Plain]View plaincopy
  1. [Www @ iZ232s4zbqvZ ~] $ Crontab-e
  2. # Imms mysqldump
  3. 01 01 ***/usr/local/service/mysql_dump.sh>/data/logs/mysql/mysqldump. log

Use gunzip to decompress the archive before restoration.

For example:

[Root @ iZ232s4zbqvZ 2015] # gunzip imms_core.dump_20150211_133301. SQL .gz

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.