A Linux Backup Script Using The BASH Shell
As mentioned in our previous article about our company backup plan, I’ve provided our simple shell script that backup our information on our server to our external hard drive.
First you need to connect your external hard drive and then mount it. Assuming you’ve done so already, here’s the backup script.
#! /bin/bash
echo Backup Started `date` >> ~/backuplog
mkdir /mnt/usbdrive/backups/`date +%Y%m%d`
tar -czf /mnt/usbdrive/backups/`date +%Y%m%d`/data.tar.gz /data
echo Backup Completed `date` >> ~/backuplog
The first line tells the script that is is using the BASH shell, which is located in /bin/bash on the file server. This is the standard location on nearly any UNIX machine.
The second line tells the script to write a line to a log file (located in our users home directory by specifying the tilde ~ ) with the days date. This log file can be referenced to ensure that the backup is completed each day.
The third line will create the folder on our mounted usbdrive, in the backups folder with the days date in the format YYYYMMDD (example: 20060915).
The fourth line actually creates the backup. On our file server we have created the folder /data where we store everything. We have multiple directories within the data folder for clients, internal documentation, resources, etc… and everything in that folder is compressed into an archive file using the tar command. The file is given the name data.tar.gz.
Finally, the fifth line writes a line to the backuplog telling us that the backup was completed and at what time.
NOTE: You must save this file somewhere on your computer, we recommend in the bin folder of your home directory, and you must give it execute permissions. If you want all users to be able to run the script, you need to use the chmod a+x command, if you want to keep it to yourself, then use the chmod u+x command. S, with it being saved in our home folder’s bin directory we type at the shell terminal:
chmod u+x /home/username/bin/backup
To run the script, simply type: backup from the terminal. Because we placed it in our bin directory (which is in our user’s execute path by default) the system looks there to execute the file. The entire process takes between one and two hours, and using a cronjob we have set this up to run automatically at noon everyday.
00 12 * * 1-5 /home/username/bin/backup
This CRON statement tells the operating system to run the backup command, which is located in the bin directory of the user’s home folder, at 12:00 noon, monday to friday. (We TRY to take weekends off so there isn’t a need for backups).
Now we have a simple script in place that will create a compressed archive of all our sensitive information on a daily basis and store it on our external hard drive.




June 25th, 2008 at 1:56 am
what files does it back up?
or how do i spesify that?
June 25th, 2008 at 8:25 am
This line:
tar -czf /mnt/usbdrive/backups/`date +%Y%m%d`/data.tar.gz /data
tells it to backup the /data directory into a tar file and then move it to /mnt/usbdrive/backups/20080625/data/data.tar.gz where the 20080625 is the current date. Just update /data to whatever folder you want to backup.
June 26th, 2008 at 6:49 am
Thanks for this script!
I’m trying to add some rotation to this code, so that old backups get erased on a regular basis. Do you by any chance know how that could be done?
June 26th, 2008 at 6:54 am
At the beginning of this script I’ve added a few lines that grab the dates of the last 7 days. Then it erases any folder named by those old dates and only keeps the 3 most recent backups. Here it is:
#GET DATE VARIABLES
Today=`date +%Y%m%d`
TwoDaysAgo=`date –date=’2 days ago’ +%Y%m%d`
ThreeDaysAgo=`date –date=’3 days ago’ +%Y%m%d`
FourDaysAgo=`date –date=’4 days ago’ +%Y%m%d`
FiveDaysAgo=`date –date=’5 days ago’ +%Y%m%d`
SixDaysAgo=`date –date=’6 days ago’ +%Y%m%d`
SevenDaysAgo=`date –date=’7 days ago’ +%Y%m%d`
#SETUP PATHS TO OLD BACKUPS
SevenDaysAgoTar=/mnt/usbdrive/backups/$SevenDaysAgo
SixDaysAgoTar=/mnt/usbdrive/backups/$SixDaysAgo
FiveDaysAgoTar=/mnt/usbdrive/backups/$FiveDaysAgo
FourDaysAgoTar=/mnt/usbdrive/backups/$FourDaysAgo
ThreeDaysAgoTar=/mnt/usbdrive/backups/$ThreeDaysAgo
TwoDaysAgoTar=/mnt/usbdrive/backups/$TwoDaysAgo
TodayTar=/mnt/usbdrive/backups/$Today
#DELETE ANY OLD BACKUPS IF THEY EXIST
rm -rf $SevenDaysAgoTar
rm -rf $SixDaysAgoTar
rm -rf $FiveDaysAgoTar
rm -rf $FourDaysAgoTar
You can add / remove the lines from the #DELETE ANY OLD BACKUPS IF THEY EXIST section to automatically delete the folders from those previous days. If they don’t exist then it skips them. Right now, I’m storing the last 3 days.
July 2nd, 2008 at 11:01 am
Thanks Jarrod!
I had the script running for about a week now, and triggered on a daily basis by cron.
But it seems like non of the backups where deleted, not even those older than three days. What may be wrong?
The backuplog returns only the following messages. Do the error messages get stored in another log file?
Backuplog:
Backup Completed Thu Jun 26 17:26:38 CEST 2008
Backup Started Thu Jun 26 17:35:07 CEST 2008
Backup Completed Thu Jun 26 17:35:41 CEST 2008
Backup Started Fri Jun 27 11:26:01 CEST 2008
Backup Completed Fri Jun 27 11:27:26 CEST 2008
Backup Started Sat Jun 28 11:26:01 CEST 2008
Backup Completed Sat Jun 28 11:27:22 CEST 2008
Backup Started Sun Jun 29 11:26:01 CEST 2008
Backup Completed Sun Jun 29 11:27:32 CEST 2008
Backup Started Mon Jun 30 11:26:02 CEST 2008
Backup Completed Mon Jun 30 11:27:59 CEST 2008
Backup Started Tue Jul 1 11:26:01 CEST 2008
Backup Completed Tue Jul 1 11:27:43 CEST 2008
Backup Started Wed Jul 2 11:26:01 CEST 2008
Backup Completed Wed Jul 2 11:27:28 CEST 2008
July 2nd, 2008 at 11:11 am
The only thing I can think of off the top of my head is either the paths have a typo in them so the command to delete them is not working, or a permissions issue.