Click to See Complete Forum and Search --> : Backup script need advice


VoiDeR
01-13-2005, 06:27 PM
I have been searching for a good backup shell script and found alot. Ive been reading alot of articles about how to write shell scripts so i figured id try and write my own. Heres what have come up with


#!/bin/bash
date="$(date --date= +%F)"

if [ -f /mnt/backup/$date.tar.bz2 ]; then
echo You already backed up today.
else
echo Ok lets start backing up.
echo What would you like to back up.
read path
echo Where is your list of files not to be tared are.
read notar
touch ~/backupinfo
echo `date` >> ~/backupinfo
echo "Backing up $path" >> ~/backupinfo
echo `uptime` >> ~/backupinfo
tar -X $notar -c -v -j -f /mnt/backup/$date.tar.bz2 -C /mnt/backup $path
echo `uptime` >> ~/backupinfo


fi


It looks really sloppy to me. This is my first shell script that was more than 2 lines. It seems to work just fine. I just need some advice if you think this script would work well enough to give to a family member that is computer illiterate (sp) and not have any "problems". I have no coding skills except for what i read in the articles. So feel free to bash any and all of my coding mistakes

Thanks VoiDeR

davisfactor
01-13-2005, 07:03 PM
FIrst off, not bad. That looks better than my first shell script.

The date variable can be shortened a little by changing it to just date +%F

Also, what if the person wants to backup more than one directory? Or what if he wants to automate it so it does it automatically every night/morning?

There are lots of backup scripts around that each use a different technique such as rsync or tar. My backup script does a full backup every night so I don't have to worry about differential backups and it's very maintainable.

My backup script is coded to backup the following:
/boot
/home
/etc
/var/www
/var/spool/davis
/usr/src
and /var/lib/mysql after I stop the mysql daemon
then I restart the daemon on the next line.

A trick that you might have seen is using the 2> to redirect error messages to a file.

For example:

TODAY=$(date | awk '{print $2"_"$3}')
BACKUP_DIR=/hde/backup/$TODAY
BACKUP_ERRORS=/home/davis/.backup_errors.txt

tar zcpvf $BACKUP_DIR/boot.tgz /boot 2>> $BACKUP_ERRORS

At the end of my script I email myself the $BACKUP_ERRORS file so I can review it for errors .

That's pretty much my entire script, it's not very complicated at all.


Hope some of this helps you!

VoiDeR
01-17-2005, 01:23 PM
Thanks for the input. Im going to start rewriting my it so it does alot of what you posted. How would i go about haveing it email me any specific information. Would i need to set up mail server?

Thanks
VoiDeR

davisfactor
01-18-2005, 03:08 PM
instead of setting up a mail server, it's easier to setup nullmailer. It's a relay agent that forwards mail to another mail server. I have mine setup to forward mail to my ISP's SMTP server.

apeekaboo
01-19-2005, 07:35 PM
I would suggest putting double-quotes (") around the echo stuff, for better readability.
Also for a backup-script it's essential to have working error checking for everything.
For instance;
* what happens when tar fails for some reason?
* how will you handle an interrupted backup?
* what if the mail doesn't get sent or doesn't reach you? (when you implement this feature)

I would advice you to use a logfile and a lockfile, but this all depends of the complexity of the script and how valuable your data is.

The logfile logs everything your script succeeds or fails to do, as well as what files are being backuped.
This will serve you well when backups don't work or when you need a particular file from your backup and you only have to grep your logfiles to see where the file is located.

The lockfile makes sure that no more than one instance is running of the backup-script at any time.
The script should delete the lockfile as the very last thing.
This let's you implement a feature which looks for the lockfile at the beginning of the script, and if it's found, you know the previous backup failed or is still running.

It's also a good thing to check the integrity of the backup once it's done.
Checking the archive with tar -t and watching exit statis could probably suffice.

I don't mean to put you down, you have made a good start for a backup-script and you use a quite clean coding style for an inexperienced user.
I just think that backups are a thing to be taken seriously and there is only one thing worse than not having made backups, and that's having backups you can't trust! Trust me I'm speaking from my own experience here... :mad:

As for mailing:
You have local mail working if you can receive this:echo test |mail -s "test" $(whoami)
No need to send the mail through your ISP if the final destination is the same box doing the backups.