Linux Scheduled backup files __linux

Source: Internet
Author: User

Today, someone asked me to write a script that would allow Linux to regularly back up file data and delete backups over a certain period of time. It sounds interesting, but after some groping, it didn't work out. (because it is for the people who do not know Linux very well, so more detailed) look at the following: first, write a script

Write a script file that enables the script to perform a 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/filename. sh
For example:
Enter command: touch/home/backup.sh
2. Write Command

First into the script: VI command
vi/home/backup.sh
To write within a script:

#。 /bin/sh
mkdir/home/beifen
#创建一个临时文件 (path to save the backup)
cp-r/home/backups/balalala  /home/beifen
# The data exists in the backups directory, backed up to the Beifen directory, so copy the data first
tar-zcpvf/home/backup$ (date +%y%m%d). Tar.gz/home/beifen
 # Package the data in the folder Beifen
rm-rf/home/beifen/
 #删除临时文件内容 find

./-mtime +30-name "*.tar.gz"-exec rm-rf {} \;
  
    #删除改文件夹下超过30天的文件
  

3. Execute script file

Manually execute the script file directly.
Command format: SH path/filename. sh
For example: sh/home/backup.sh II, set to execute this script regularly.

For example, the setup script executes once every 7 days. 1. Install Crond

If the Crond service is not installed, install the service first.
Check Service Status: Services Crond status 2. Modify Crontab content

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

SH Script The previous five fields for minutes (0-59), Hours (0-23), Days (1-31), month (0-12), Day (0-6), after the script is the directory

3. Restart the Crond service

Enter command: Service crond Restart this configuration is complete.

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.