How to regularly back up a MYSQL database using crontab in linux: _ MySQL

Source: Internet
Author: User
In linux, you can use crontab to regularly back up the MYSQL database: follow the three steps below and everything is under your control:

Step 1: configure the backup directory code on the server:

--------------------------------------------------------------------------------

Mkdir/var/lib/mysqlbackup

Cd/var/lib/mysqlbackup

--------------------------------------------------------------------------------

Step 2: write the backup script code:

--------------------------------------------------------------------------------

Vi dbbackup. sh

--------------------------------------------------------------------------------

Paste the following code to change the username, password, and dbname.

Code:

--------------------------------------------------------------------------------

#! /Bin/sh

Mysqldump-uuser-ppassword dbname | gzip>/var/lib/mysqlbackup/dbname 'date encoding policy-m-1_d_1_h1_m1_s'. SQL .gz

Cd/var/lib/mysqlbackup

Rm-rf 'find.-name' *. SQL .gz '-mtime 10' # Delete the backup file 10 days ago.

--------------------------------------------------------------------------------

Step 3: Change the backup script permission

Code:

--------------------------------------------------------------------------------

Chmod + x dbbackup. sh

--------------------------------------------------------------------------------

Step 4: Use crontab to regularly execute the backup script code:

--------------------------------------------------------------------------------

Crontab-e

--------------------------------------------------------------------------------

If you back up data at every night, add the following code,

Code:

--------------------------------------------------------------------------------

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




Note:

1. writing of the task scheduling setting file
You can use the crontab-e command to edit the cron file of the corresponding user in/var/spool/cron. you can also directly modify the/etc/crontab file.
The specific format is as follows:
Minute Hour Day Month Dayofweek command
Minute Hour Day week Command
The meaning of each field is as follows:
Minute executes the task in minutes of each hour.
Hour executes the task the Hour of the day
Day of each month
Month: the Month of each year.
DayOfWeek executes this task the day of the week
Command specifies the program to be executed
In these fields, all other fields except "Command" must be specified each time are optional.

Field, depending on the needs. For unspecified fields, use "*" to fill their positions.
Example:
* Ls indicates that the ls command is executed every 5th minutes of an hour.
30 5 *** ls specifies to execute the ls command at every day
30 7 8 ** ls: specify to execute the ls command at on the 8 th of every month.
30 5 8 6 * ls specifies to execute the ls command at on January 1, June 8 each year.
30 6 ** 0 ls specify to execute the ls command at every Sunday [Note: 0 indicates Sunday, 1 indicates Monday,

Similarly, sun represents Sunday, mon represents Monday, and so on.]

30, 20 ** ls: execute the ls command at on the 10th and 20th of every month [note: "," is used to connect multiple discontinuous periods]

25 8-11 *** ls execute the ls command 25th minutes at every day [note: "-" is used to connect consecutive periods]

*/15 * ls: execute the ls command every 15 minutes [that is, execute the ls command every hour for 0th 15 30 45 60 minutes]

306 */10 ** ls: execute the ls command at every 10 days. [that is, execute the ls command at on May 1, 11, 21, and 31 every month. ]

Run all executable files in the/etc/cron. daily directory as root at every day.

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

2. two methods are available for adding a scheduling task:
1) enter crontab-e in the command line and add the corresponding task. The wq disk will exit.
2) directly edit the/etc/crontab file, that is, vi/etc/crontab, and add the corresponding task.

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.