Implementation method of Crontab automatic backup Web site in CentOS

Source: Internet
Author: User
Tags mysql backup

Crontab Introduction

The crontab command is common in UNIX and unix-based operating systems, and is used to set periodically executed instructions. This command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution.  The word derives from the Greek chronos (χρνο), which is intended to be time. Typically, crontab stored instructions are activated by the daemon, Crond often run in the background, checking every minute for scheduled jobs to be performed. This type of assignment is commonly referred to as cron jobs.

Crontab usage

The crontab format is as follows:

The code is as follows Copy Code

F1 F2 F3 f4 f5 program

Where F1 is the minute, F2 represents the Hour, F3 represents the day of the month, the F4 represents the month, and the F5 represents the day of the one week. The program represents the path to execute.

When F1 is *, the PROGRAM,F2 is executed every minute for *, which means that the program is executed hourly, and the rest of the analogy
When F1 is a-b to be executed from the time of the first a minute to the B minute, the F2 is a-b to execute from a to B hours, and the rest by analogy
When F1 is */n, it is executed once every n minutes, F2 is performed once per N-hour intervals for */n, and the rest by analogy
When F1 is a, B, C,... The first A, B, C,... Minutes to execute, F2 for a, B, C,... The first is a, B, c ... An hour to execute, the rest of the analogy
Admin login ssh, enter command crontab-e edit crontab file, enter and save according to the format above.

Here's a common backup method:

1. File backup

Compress files for backup using the TAR command:

The code is as follows Copy Code
#tar-ZCVF backup.tar.gz Dir1

Dir1 for the directory or file to be backed up
Backup RESTORE command:

The code is as follows Copy Code
#tar-ZXVF backup.tar.gz

2. Database backup

Most database systems have commands to support backup and recovery of databases. For example, MySQL, the database backup command is:

The code is as follows Copy Code
#mysqldump-host=dbhost-user=username-password=password dbname > Db.dump
The restore command for the MySQL backup is:
#mysql-host=dbhost-user=username-password=password dbname < Db.dump

3. Automatic Task Scheduled backup

Use the above command to write a backup script (such as/home/backup/backup.sh), and then use the CRONTAB-E command to edit the timed task, the following is the day 01:30 automatic backup example

The code is as follows Copy Code

* * * */home/backup/backup.sh

4. Name of backup file

You can automatically add a backup time to a daily backup file name, and here is a sample script:

  code is as follows copy code

#!/bin/ Bash
curr= ' date +%y%m%d%h%m%s '
tar czvf backup_$curr.tar.gz dir1 dir2
Mysqldump-host=dbhost-user=username- Password=password dbname > Db_$curr.dump

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.