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