How to use crontab to regularly back up MySQL database under Linux:

Source: Internet
Author: User

How to use crontab to regularly back up MySQL database under Linux:

First step: Configure the backup directory code on the server:

Mkdir/var/lib/mysqlbackup

Cd/var/lib/mysqlbackup

Step Two: Write the backup script code:

VI dbbackup.sh

Paste the following code, and make sure to change the Username,password and dbname.

Code:

#!/bin/sh

Mysqldump-uuser-ppassword dbname | gzip >/var/lib/mysqlbackup/dbname ' Date +%y-%m-%d_%h%m%s '. sql.gz

Cd/var/lib/mysqlbackup

Rm-rf ' Find. -name ' *.sql.gz '-mtime ' #删除10天前的备份文件

Step Three: change backup script permissions

Code:

chmod +x dbbackup.sh

Fourth step: Execute the Backup script code with CRONTAB timing:

Crontab-e

If you are backing up at 21 o'clock 00 every night, add the following code

Code:

* * * * */var/lib/mysqlbackup/dbbackup.sh




Note:

1, the Task scheduling settings file
The CRONTAB-E command can be used to edit, edit the corresponding user's cron file under/var/spool/cron, or modify the/etc/crontab file directly.
The specific format is as follows:
Minute Hour Day Month Dayofweek command
Minutes hours days months days per week order
Each field represents the following meanings:
Minute the first few minutes of every hour to perform the task
Hour the first few hours of the day to perform this task
Day of the month to perform the task
Month months of the year to perform this task
DayOfWeek the day of the week to perform the task
Command Specifies the program to execute
In these fields, other fields are optional except that the Command is a field that must be specified each time.

field, depending on your needs. For a field that is not specified, "*" is used to fill its position.
Examples are as follows:
5 * * * ls Specifies the first 5 minutes of every hour to execute the LS command
5 * * * ls specifies the 5:30 execute LS command per day
7 8 * * ls specifies 7:30 minutes of 8th per month to execute the LS command
5 8 6 * ls specifies that the LS command be executed every June 8 5:30
6 * * 0 ls specifies 6:30 execute ls command per Sunday [Note: 0 for Sunday, 1 for Week 1,

And so on, can also be expressed in English, Sun said Sunday, Mon said Monday and so on. ]

3 10,20 * * ls 3:30 for each month 10th and 20th executes the LS command [note: "," used to connect multiple discontinuous periods]

8-11 * * * ls command is executed at 25 minutes per day at 8-11 Pips [Note: "-" used to connect a continuous period of time]

*/15 * * * * ls is executed every 15 minutes with the LS command [i.e. No. 0 15 30 45 60 minutes per hour to execute the LS command]

6 */10 * ls command is executed every 10 days each month (1, 11, 21, 31st). 6:30 executes the LS command once. ]

Every day 7:50 executes all executables in the/etc/cron.daily directory as root

     50   7       *              *     *     root      run-parts    /etc/cron.daily   [Note: The Run-parts parameter indicates that all executables in the following directory are executed. ]

2, the new scheduling task can be used in two ways:
1), at the command line input: CRONTAB-E and then add the corresponding task, Wq save the disk to exit.
2), directly edit the/etc/crontab file, that is, vi/etc/crontab, add the corresponding task.


This article is from the "Lao Jia Linux" blog, please be sure to keep this source http://jiarh.blog.51cto.com/3467177/1707092

How to use crontab to regularly back up MySQL database under Linux:

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.