Script automation: back up a file every 2 minutes
Requirements: back up the file directory / home/backups/balalala to the / home directory every 2 minutes
Prepare for
1. Create the source directory
# mkdir / home/backups/balalala
Create the original file
# touch / home/backups/balalala/first.sh
2. Create a backup directory
# mkdir / home
First, write scripts with backup function
Create script
# vi / home/backup.sh write command
#! / bin/sh
Cp-r / home/backups/balalala/* / home/beifen
# backup the files in the / home/backups/balalala directory to the / home/beifen directory to execute the script file
For example: sh / home/backup.sh
Second, set the timing to execute this script.
The setup script is executed every 2 minutes.
Install crond
If the crond service is not installed, install the service first.
Check service status: service crond status modifies crontab content
Enter the command: crontab-e
Enter and continue to enter the command:
/ 2 * / home/backup.sh
# set to execute the script file every 2 minutes
Then, save exit: wq
# the first five fields of the sh script represent minutes (0-59), hours (0-23), day (1-31), month (0-12), and day of the week (0-6). Restart the crond service for the directory where the script resides
Enter command: service crond restart
III. Verification
1. Switch to / home/beifen
# cd / home/beifen
2. List files
# ls
You can see that the file first.sh is currently available.
3. Add new files to the / home/backups/balalala/ directory
# touch / home/backups/balalala/second.sh
4. Check again after 2 minutes for second.sh in the / home/beifen directory
Second.sh verification is successful!