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.