When you are a system administrator, you will be required to regularly backup files & directories about your website, blogs & company. In fact, it is a good practice to regularly backup your data. In this article, we will look at how to create a shell script to backup files in directory.
Shell Script to Backup Files in Directory
We will use tar command, which is generally the tool used for backing up files & directories.
1. Create Shell Script
Open terminal and create an empty shell script using the following command.
$ sudo vi data_backup.sh
2. Add shell script to backup files
Here is the tar command to backup files in a directory /home/data to backup.tar.gz. You can replace the directory path and backup file name below as per your requirement.
$ sudo tar -cvpzf /home/backup.tar.gz /home/data
In the above command,
c – compression
v – verbose
p – retain file permissions
z – create gzip file
f – regular file
Now add the following to your shell script.
#!/bin/sh timestamp="$(date +'%b-%d-%y')" sudo tar -cvpzf /home/backup-${timestamp}.tar.gz /home/data
Save and close the file. In the above code, the first line sets the execution environment, the second line saves timestamp value and the third line does the backup by creating .tar.gz file. We use the timestamp variable to create a new filename every time the script is run so that the backups remain separate and there is no overwriting.
3. Make Script Executable
Run the following command to make your script executable.
$ sudo chmod +x data_backup.sh
4. Verify the script
Run the script with following command.
$ sudo /home/data_backup.sh
5. Automate Backup
It is advisable to create a cron job to run the above script regularly in an automatic manner. For this, open crontab with the following command.
$ sudo crontab -e
Add the following line to run the above shell script regularly every day at 10 a.m
0 10 * * * sudo /home/data_backup.sh >/dev/null 2>&1
Save & close the file. In the above command, “0 10 * * *” ensures that the our shell script runs every day at 10 am. Also, we mute the execution result by sending the output to /dev/null.
If you wish to make your script dynamic to be able to accept folder name dynamically then update the last line of your shell script as following
sudo tar -cvpzf /home/backup-$timestamp.tar.gz $1
In this case, you can call the shell script to backup any folder by passing it as command line argument.
$ sudo /home/data_backup.sh /home/data
Update the cronjob command as following to pass the folder name dynamically as you wish.
0 10 * * * sudo /home/data_backup.sh /home/data >/dev/null 2>&1
In this article, we have learnt how to automatically backup files & directory using shell script & cronjob.
Also read:
How to Disable Apache Configuration File
How to Read Command Line Arguments in NodeJS
How to Generate MD5 Hash of File in Linux
How to Install NVM on Mac with Brew
Shell Script to Start & Restart Tomcat Automatically
Hello,
Thank you so much for this step by step tutorial, i found it useful.
I tried to implement it, it works though timestamp seems not to work as the backup overwrites the other.
I am using ubuntu canonical server 20 LTS
Thank you so much!
Thank you for pointing it out. We have updated the shell script so that timestamp works properly. Here are updated the shell commands to store timestamp and use it backup file name.
#!/bin/sh
timestamp=”$(date +’%b-%d-%y’)”
sudo tar -cvpzf /home/backup-${timestamp}.tar.gz /home/data