Scheduled backup files under Linux

Source: Internet
Author: User

First, write the script

Write a script file that enables the script to execute the backup command.
For example, the file directory/home/backups/balalala is backed up to the/home directory and compressed.

1. Create a script

Command format: Touch path/file name. sh
For example:
Input command: touch/home/backup.sh

2. Write commands

First go to script: VI command
vi/home/backup.sh
Write inside the script:

#! /bin/shmkdir/home/beifen# Create a temporary file (the path to save the backup) cp-r/home/backups/balalala  /home/beifen# data exists in backups directory, Back up to the Beifen directory, so first copy the data over tar-zcpvf/home/backup$ (date +%y%m%d). Tar.gz/home/beifen #将数据所在文件夹beifen打包rm-rf/home/beifen /#删除临时文件内容find./-mtime +30-name "*.tar.gz"-exec rm-rf {} \; #删除改文件夹下超过30天的文件

3. Execute the script file

Manually execute the script file directly.
Command format: SH path/file name. sh
Example: sh/home/backup.sh

Second, set a timed execution of this script.

For example, the setup script executes every 7 days.

1. Installing Crond

If the Crond service is not installed, the service is loaded first.
Check Services Status: Service Crond status

2. Modify Crontab Content

Input command: crontab–e
Continue entering command after entering: 0 0 /7 */home/backup.sh
Set to execute script file every 7 days
Then, save the exit: Wq

The first five fields of the SH script represent the contents of the script in minutes (0-59), Hours (0-23), Days (1-31), months (0-12), Day of the Week (0-6), respectively.

3. Restart the Crond service

Input command: Service Crond restart

This configuration is complete!

Scheduled backup files 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.