Linuxmysql regular backup and Compression

Source: Internet
Author: User
Tags mysql backup

Linuxmysql regular backup and Compression
1. Check whether the mysql BACKUP command works.

Create a backup directory under the var directory and a mysql directory under the backup Directory to store mysql backup files

Cd to the/var/backup Directory

 

 mysqldump -uroot -pwh5268925 zhaochao > mysql/zhaochao.sql

If yes, zhaochao. SQL backup files will be available under/var/backup/mysql.


 

2. Create a backup script

Create a file msyql-backup.sh under/var/backup, the file content is as follows, save the file

 

JIRA_FILE_NAME=zhaochao-`date +%Y%m%d%H%M%S`;cd /var/backup/mysql/mysqldump -uroot -pwh5268925 zhaochao > ${JIRA_FILE_NAME}.sqltar -zcvf ${JIRA_FILE_NAME}.sql.tar.gz ${JIRA_FILE_NAME}.sqlrm -rf ${JIRA_FILE_NAME}.sql

3. Run the script to check whether the script can be correctly executed.

Cd to the/var/backup Directory to run the./mysql-backup.sh script, if correct, the/var/backup/mysql directory will generate a backup file named with time and compressed

4. Set the timer and regularly execute the mysql-backup.sh script

Create the crontest. cron file under/var/backup as follows:

Execute the script every 15 minutes.

 

15,30,45,59 * * * *  /var/backup/mysql-backup.sh
Run the command at every morning.

 

 0 3 *  *  *  /var/backup/mysql-backup.sh

 

5. Add the timer file to the timer.

 

crontab crontest.cron
6. Check whether the scheduled task is added to crontab-l in the system.

 

[root@iZ23l2efmpqZ backup]# crontab -l15,30,45,59 * * * *  /var/backup/mysql-backup.sh

7. Cancel the scheduled task crontab-r

 

[root@iZ23l2efmpqZ backup]# crontab -l15,30,45,59 * * * *  /var/backup/mysql-backup.sh[root@iZ23l2efmpqZ backup]# crontab -r        [root@iZ23l2efmpqZ backup]# crontab -lno crontab for root

8. crontab command usage

 

Basic Format:
* *** Command
Hour, day, month, and week commands

The 1st column indicates minute 1 ~ 59. Each minute is represented by * or */1.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
6th columns of commands to run

Some examples of crontab files:

30 21 ***/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates restarting apache at every night.

45 4, 10, 22 **/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted at on the 1st, 10th, and 22th every month.

10 1 ** 6, 0/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted at every Saturday and Sunday.

0, 30 18-23 ***/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted every 30 minutes between and every day.

0 23 ** 6/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted at every Saturday.

**/1 ***/usr/local/etc/rc. d/lighttpd restart
Restart apache every hour

* 23-7/1 ***/usr/local/etc/rc. d/lighttpd restart
Restart apache every hour between PM and PM.

0 11 4 * mon-wed/usr/local/etc/rc. d/lighttpd restart
Restart apache on November 4 and every Monday to Wednesday.

0 4 1 jan */usr/local/etc/rc. d/lighttpd restart
Restart apache at on January 1, January 1

 

Name: crontab

Permission: All Users

Usage:

Crontab file [-u user]-replace the current crontab with the specified file.

Crontab-[-u user]-replace the current crontab with the standard input.

Crontab-1 [user]-list the current crontab of the user.

Crontab-e [user]-edit the current crontab of the user.

Crontab-d [user]-delete the current crontab of the user.

Crontab-c dir-specifies the crontab directory.

Crontab file format: m h d m d cmd.

 

M: minute (0-59 ).

H: hour (0-23 ).

D: Day (1-31 ).

M: Month (1-12 ).

D: days in a week (0 ~ 6, 0 is Sunday ).

The program to run in cmd is sent to sh for execution. This shell only contains three environment variables: USER, HOME, and SHELL.

Note:

Crontab is used to allow users to execute programs at a fixed time or interval. In other words, it is similar to the user's time table. -U user is used to specify

The premise is that you must have the permission (for example, root) to specify the time table of another user. If-u user is not used, it indicates

Set your own time table.

 

 

Parameters:

Crontab-e: run the text editor to set the time table. The preset text editor is VI. If you want to use another text editor, set the VISUAL environment variable first.

To specify the Text Editor (for example, setenv VISUAL joe)

Crontab-r: Delete the current time table

Crontab-l: to list the current time table

Crontab file [-u user]-replace the current crontab with the specified file.

The time table format is as follows:

F1 f2 f3 f4 f5 program

F1 indicates the minute, f2 indicates the hour, f3 indicates the day of the month, f4 indicates the month, and f5 indicates the day of the week. Program indicates to execute

.

When f1 is *, the program is executed every minute. When f2 is *, the program is executed every hour, and so on.

When f1 is a-B, it indicates that execution is performed from the minute a to the minute B. When f2 is a-B, it indicates that execution is performed from the hour a to the hour B, and so on

When f1 is */n, it indicates execution is performed every n minutes. If f2 is */n, it indicates execution is performed every n hours, and so on.

When f1 is a, B, c ,... a, B, c ,... execute in minutes. f2 is a, B, c ,... a, B, c... execution in hours, and so on

You can also store all settings in the file first, and use crontab file to set the time table.

Example:

# Run/bin/ls at every morning:

0 7 ***/bin/ls

During October 11, December, execute/usr/bin/backup every three hours from to every day:

0 6-12/3*12 */usr/bin/backup

From Monday to Friday, send a letter to alex@domain.name at pm:

0 17 ** 1-5 mail-s "hi" alex@domain.name </tmp/maildata

Execute echo "haha" at midnight, 00:20, and 02:20 every month"

20 0-23/2 *** echo "haha"

Note:

When the program is executed at the specified time, the system will send you a letter showing the program execution content. If you do not want to receive such a letter, please leave each row Blank

Add>/dev/null 2> & 1.

 

Example 2:

#06:10 every morning

10 6 **** date

# Every two hours

0 */2 * date

# Every two hours from PM to am, am

0 23-7/2, 8 **** date

# Am on the 4th day of each month and from Monday to Wednesday of each week

0 11 4 * mon-wed date

# A.m. of July

0 4 1 jan * date

Example

$ Crontab-l list the current crontab of a user.



 

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.