-
Notifications
You must be signed in to change notification settings - Fork 0
creating cronjob
In this example, you will create a cronjob that makes a backup of /Desktop/important
, /Desktop/work
and /Pictures/Holiday-12-03-2016
.
After this example cronjob you should be able to utilize cronjob on your own.
For this example you need following script backup-personal.sh
. You'll also need the ability to execute this script. This can be done with the following command:
sudo chmod +x scriptname.sh
The contents of the script are hold very simple, it checks if the contents are still there, if not it tries to find them. If it found the contents it will begin to create a backup in form of a tar-ball. The following Code can be found down below:
#!/bin/bash
# Define the directories to back up
directories=(
"/Desktop/important"
"/Desktop/work"
"/Pictures/Holiday-12-03-2016"
)
# Iterate over the directories
for directory_path in "${directories[@]}"; do
# Check if the directory exists
if [ -d "$directory_path" ]; then
echo "Contents exist in $directory_path"
else
echo "Contents not found in $directory_path. Trying to find them..."
# Code to find the contents here
# Check if the directory is found
if [ -d "$directory_path" ]; then
echo "Contents found in $directory_path"
else
echo "Unable to find contents in $directory_path."
continue
fi
fi
# Extract the directory name from the path
directory_name=$(basename "$directory_path")
# Create a backup in the form of a tarball
backup_filename="${directory_name}_backup_$(date +%Y%m%d%H%M%S).tar.gz"
tar -czf "$backup_filename" -C "$directory_path" .
echo "Backup created for $directory_path: $backup_filename"
done
Now after you have the script and it can be executed you can proceed to the next step.
Creating a cronjob is relatively simple. You can initialize your cronjob by using the following command:
crontab -e
This will open a prompt. In this prompt please pick your desired Text-editor. After you selected your desired Text-editor you will see the following:
GNU nano 6.2 /tmp/crontab.pbb321/crontab
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
After seeing this, enter the following at the bottom of the file:
* * */20 * * backup-personal.sh
you now may wonder why the *
well this is to indicate that the script should execute every 3 minutes:
This is how the they function:
The first * indicates the minute (0-59)
The second * indicates the hours (0-23)
The third * indicates day of month (1-31)
The fourth * indicates the month (1-12 or names, but check you manpage for this)
The fifth * indicates the day of week (0-7 7 is sun, or use names)
Now just add the previously mention snippet at the end of the file and et voilà, there you have your first cronjob.