Cron job that creates backups of a directory in Linux
Social Share:
Wednesday, August 7, 2024 at 6:50 PM | 3 min read
Last modified on Thursday, August 8, 2024 at 10:43 AM
#chmod, #cron daemon, #crontab, #linux, #automation, #task automation

Photo by Mikhail Nilov on pexels.com
I just finished creating a cron job that backs up my Desktop directory daily in my Linux Mint OS. The name of the backup includes the date and time of the backup.
Table of Contents
- Creating the desktop-backup directory and the desktop_backup.sh shell script
- Creating the delete_backups.sh shell script
- Adding the shell scripts to the crontab file
- Related Resources
Creating the desktop-backup directory and the desktop_backup.sh shell script
First, I created a backup directory called desktop-backups in my home directory (/home/maria), and then I created a shell script called desktop_backup.sh located in /home/maria/Desktop/cron-job-scripts/. And then I added the following:
#!/bin/bash source_dir="/home/maria/Desktop" backup_dir="/home/maria/desktop-backup" timestamp=$(date +%Y%m%d%H%M%S) backup_file="backup_$timestamp.tar.gz" tar -czvf "$backup_dir/$backup_file" "$source_dir"
The first line in the script is very important. #! is referred to as shebang, and it tells the system which interpreter/command to use to execute the commands written inside the scripts. The interpreter here is bash, and the path to it is /bin/bash, which follows #!. #! is interpreted by the execve(2) system call (which executes programs).
Next, and most important, is to change file permissions for the desktop_backup.sh file. By default, a newly created file does not contain execute permissions, which is necessary for executing shell script files. So next, I have to run the following in Terminal to add execute permissions to desktop_backup.sh from inside the directory where it resides:
chmod +x desktop_backup.sh
chmod stands for change mode.
Then I check to make sure that execute permissions were added to desktop_backup.sh by running the ls -l command in Terminal, and the following is returned as a result:
-rwxrwxr-x 1 maria maria 208 Aug 7 13:10 desktop_backup.sh
When I create the delete_backups.sh file (coming up), I will have to change permissions in the same way as well.
If I run date +%Y%m%d%H%M%S in Terminal, something like the following is returned:
202400807161028
202400807 is the year, month and day. 161028 is the hour, minutes, and seconds in 24 hour format. After I run the shell script, something like the following appears in the desktop-backup directory:
backup_202400807161028.tar.gz
If I am inside the cron-job-scripts directory where desktop_backup.sh resides, I run the following in Terminal:
./desktop_backup.sh
Which is how a shell script is run in Terminal. If I am not inside the directory which contains the shell script, then I have to add the path to it. ./ means that it is in the current directory.
Creating the delete_backups.sh shell script
But after a while, a lot of backups will take up the desktop-backup directory, and I probably would want to remove older backups. I can create a separate script for that.
I could create another shell script to remove older backups:
#!/bin/bash find /home/maria/desktop-backup/* -type f -mtime +1 -delete
The find command looks for files in a directory hierarchy. In the script above, it is searching for the files located in /home/maria/desktop-backup/, and the wildcard * means anything (any file) inside /home/maria/desktop-backup/. -type f means of type file. -mtime is interpreted as the number of whole days in the age of a file. And -mtime +n (in this case +1) means greater than n (whole) days. -delete deletes what is inside the /home/maria/desktop-backup/ directory.
Adding the shell scripts to the crontab file
After I created these two shell scripts, I added them to my crontab file.
First, I ran crontab -e in 1. It took me into the crontab file, where I added my cron jobs. Then I added the following to the bottom of the file below all the commented lines:
00 06 * * * /home/maria/Desktop/cron-job-scripts/desktop_backup.sh 05 06 * * * /home/maria/Desktop/cron-job-scripts/delete_backups.sh
The meaning of the first line is that the cron job will take place at 6am every day. The meaning of the second cron job is that it will take place at 6:05am every day. The * in the day position which follows the hour position, means every day. The next * to the right is in the month position and means every month. And the last * (to the right) which represents the day of the week, means every day of the week.
Since I just set up this cron job today, I will probably not reap its benefits until the day after tomorrow. I will update this post with the results when the contents of the desktop-backup is deleted!
Related Resources
- The Cron daemon in Linux and how to create a cron job: mariadcampbell.com
- How to Create and Set Up a Cron Job in Linux: phoenixNap
- How To Format Date For Display or Use In a Shell Script: by Vivek Gite, cyberciti.biz