In CentOS Linux, MySQL databases are automatically backed up every day.

Source: Internet
Author: User

In CentOS Linux, MySQL databases are automatically backed up every day.

In CentOS Linux, MySQL databases are automatically backed up every day.

1. Create the following three folders on the server: mkdir/bak, mkdir/bak/bakmysql, mkdir/bak/bakmysqlold. Create a shell script in the/bak/bakmysql Folder: touch/bak/bakmysql/backup. sh.

Authorize the file: chmod 755/bak/bakmysql/backup. sh.

2. Edit the shell script vi/bak/bakmysql/backup. sh.
#! /Bin/sh
Cd/bak/bakmysql
Echo "You are in bakmysql directory"
Mv bakmysql */bak/bakmysqlold
Echo "Old databases are moved to bakmysqlold folder"
Now = $ (date + "% d-% m-% Y ")
File = bakmysql-$ Now. SQL
Mysqldump-uroot-p 'Password' db_bbs> $ File
Echo "Your database backup successfully completed"
SevenDays = $ (date-d-7day + "% d-% m-% Y ")
If [-f/bak/bakmysqlold/bakmysql-$ SevenDays. SQL]
Then
Rm-rf/bak/bakmysqlold/bakmysql-$ SevenDays. SQL
Echo "You have delete 7 days ago bak file"
Else
Echo "7 days ago bak file not exist"
Fi

3. shell script: Replace the red font in mysqldump-uroot-p 'Password' db_bbs> $ File with your own mysql database user, password, and database name. The script first moves the backup file that bakmysql starts with the file name in the/bak/bakmysql folder to/bak/bakmysqlold, and then generates the latest backup file, finally, check whether the files in the/bak/bakmysqlold folder existed seven days ago. If the files exist, delete them and end them. In this way, the database scripts for the last seven days can be automatically backed up.

This is the master server:

4. set the daily automatic shell script: vi/etc/crontab to add the following line at the end (For details, refer to the notes in/etc/crontab ): 0 0 *** root/bak/bakmysql/backup. sh. Restart crontab:/sbin/service crond restart. If crontab is not set as auto-start, you can set it as follows: chkconfig-level 35 crond on

This article permanently updates the link address:

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.