Automatically backs up MySQL database every day under Linux (CentOS)

Source: Internet
Author: User

1. Under the server, create the following 3 folders: Mkdir/bak,mkdir/bak/bakmysql,mkdir/bak/bakmysqlold. Build a shell script under the/bak/bakmysql folder: touch/bak/bakmysql/backup.sh. Authorization to File: chmod 755/bak/bakmysql/backup.sh.

2. Edit Shell script: vi/bak/bakmysql/backup.sh

#!/bin/shcd /bak/bakmysqlecho  "You are in  Bakmysql directory "mv bakmysql* /bak/bakmysqloldecho " old databases are  Moved to bakmysqlold folder "now=$ (date +"%d-%m-%y ") file=bakmysql-$Now. sqlmysqldump - Uroot -p ' password '  db_bbs >  $Fileecho   "your database backup  Successfully completed "sevendays=$ (date -d -7day  +"%d-%m-%y ") if [ -f  /bak/bakmysqlold/bakmysql-$SevenDays. sql ]thenrm -rf /bak/bakmysqlold/bakmysql-$ sevendays.sqlecho  "you have delete 7days ago bak file " elseecho  " 7days ago bak file not exist  "Fi 

3.shell script: mysqldump-uroot-p 'password' Db_bbs> $File The red font in this sentence, please change to your own MySQL database password and name. The script first moves the backup file under the/bak/bakmysql folder Bakmysql to the beginning of the file name to/bak/bakmysqlold, generates the most recent backup file, and then judges/bak/ Bakmysqlold folder seven days ago whether the file exists, the existence is deleted, and finally ended.


This is the blogger's server: 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M01/7A/3E/wKiom1al7aywsIeBAACYSJ9aC6U422.jpg " Title= "Qq20160125173941.jpg" alt= "Wkiom1al7aywsiebaacysj9ac6u422.jpg"/>

4. Set the shell script to execute automatically every day: Vi/etc/crontab add such a line at the end (refer to the note in/etc/crontab for your own reference): 0 0 * * * root/bak/bakmysql/backup.sh. Restart Crontab:/sbin/service crond restart. If you do not set the crontab to boot, you can set it to boot from: Chkconfig–level crond on

This article is from the "Zhanglunc" blog, make sure to keep this source http://zhanglunc.blog.51cto.com/11144997/1738402

Automatically backs up MySQL database every day under Linux (CentOS)

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.