How do I write a backup unix shell script?

The shell script needs to run nightly to backup the server with "tar", email me the backup results (if the backup was complete or if it failed)
Also the backup needs to be restart if it fails
no nameAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First you need to figure out the directories you need and those you don't want.
Also whether you want to stay within the same filesystem or not

The script would look something like this.

50 points :(

while :
tar pcvz --one-file-system -f bkup.tgz  \
--exclude=/proc --exclude=*lost+found* \
--exclude=/boot --exclude=/sys \
--exclude=/media --exclude=mnt \
--exclude=/tmp --exclude=/home \
--exclude=*BACKUPS* --exclude=*tgz* \
--exclude=/var/spool --exclude=/var/log \
--exclude=/services1 --exclude=tarballs* --exclude=f2e1.base.install.tgz \
--exclude=*tmp* --exclude=/services/faxes/* \
--exclude=/.autofsck --exclude=/.autorelabel --exclude=/usr/games \


if (( status == 0 ))
   echo "backup done" | mail "bkup_$(date)"
   exit 0

Open in new window

no nameAuthor Commented: should I write a cron shell script to run it?
Thank you for ...

Ok.  You have to analyse your system.  The above script may not run exactly on your system.  You should know which directories you should include and which ones you should exclude.

Yes, you can include in the Cron as per your wishes.  Suppose you want to run it every day at 7AM you can write something like

0 7 * * * /path/script
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

The above is though a good example as to what you might want to do.  It is pretty close.  First try it on a few folders and customize your tar command.

Run it manually to see you are happy with it.

Then put it on cron

crontab -e -u myuserID

0 7 * * * /path/script

Open in new window

Also if you want want to make a backup of only one partition at a time, then you would use the option --one-file-system, otherwise, you don't need it.  I was doing it because when I did, I was making a separate backup for each partition, as they were huge.
no nameAuthor Commented:
Okay this is how I did it, but is now giving me an endless loop
Can you tell me what part im doing wrong?

while [ true ]
echo "Backing up /dev/tape"
tar cvf /dev/tape . | wc - c

if [ status == 0 ]
    echo "backup done" | mail "bkup_$(date)" garcia
    exit 0
madunix (Fadi SODAH)Chief Information Security Officer Commented:
different ways to backup

PHP5 script  automate backups from  main hard drive, to a separate backup hard drive, run it overnight using a cron job.

-tar -cvzf /rescue/www-$(date +%u).tgz /var/www/*

-Backuppc is also a nice solution for backups for a nice how to(debian) but it should work on all linux as well check out

-rsync scripting

#  This script uses Rsync the backup files
#  to a remote server. To use this script
#  you first have to setup ssh to use keys.
#  This elimates the need for a password but
#  still provides security I have ssh on
#  a non standard port so I had to specify
#  the port (ssh -p 2998).
#  Simply change the variables list below
#  to match your specifications.
#  The Script also logs the transfer time.
#  For automated backup add script to crontab.
# Setup Variables
#The user you created with ssh_keys
STARTTIME=$(date +"%s")
# Verfiy the last rsync is not still running
if [ -f $LOCKFILE ]; then
ERRORTIME=$(date +"%d/%b/%Y:%H:%M:%S %Z")
echo "[$ERRORTIME] WARNING rsyncbackup.lock exists, aborting..." >> $LOGFILE
exit 1
# If it finished, create a new lock file
# The rsync command (for explanation type rsync --help)
rsync -aqrpogtu --delete --bwlimit=512 --rsh='ssh -p 22' $SOURCEPATH $DESTUSER@$DESTHOST:$DESTPATH 2>&1 >> $LOGFILE
# Log when the script was run
DONE=$(date +"%d/%b/%Y:%H:%M:%S %Z")
# Log how long it took the script to run
STOPTIME=$(date +"%s")
echo "[$DONE]finished: - Tranfer took:$(((SECONDS/60)/60)) hours $(((SECONDS/60)%60)) minutes $((SECONDS%60)) seconds" >> $LOGFILE
# Remove lock file

Open in new window

Hi sbanderag,

I've just been looking at your script.

I suggest you start by running your script manually from the command line, so you can see what's happening (error messages, etc), and debug any problems.

1. If you want to save space on tape, you might like to use tar's -z (for gzip) or -j (for bzip2 which is generally smaller than gzip) switch.  e.g.
    tar zcvf /dev/...
    tar jcvf /dev/...

2. I see you have a syntax error in your wc command.  If you want to use the -c switch, then use it like this:
    wc -c
    wc - c
I assume that's causing the loop, since the status will always be 1, and the "exit" will never be reached.

3. Also, I don't think this will work as you want it to:
    tar cvf /dev/tape . | wc - c
This will put the return code of the "wc" command into $status.  If tar fails, but wc succeeds, then $status will be 0, and you will get an email to say the backup worked.  I suggest:
    tar cvf /dev/tape .
but I'm not sure what you want the "wc" for, exactly.

4. If "bkup_$(date)" is meant to be your email subject, then I think you'll need to use the -s (subject) switch (see below).

5. And I see from your specifications, that you want an email whether the backup works or fails.  Maybe have something like this near the end:
    if [ status == 0 ]
        echo "backup done" | mail -s "bkup_$(date)" garcia
        exit 0
        echo "Backup FAILED with status $status" | mail -s "bkup_$(date)" garcia
Note that there is no exit on the "FAILED" code.
However, if I were you, I would strongly consider removing the loop, and let it fail and email you, because in most? cases, if it fails once, then it will keep failing, and will just go into an infinite loop, which is probably a worse problem than just failing and emailing you about it.  If it loops when you are away (e.g. weekends), then you'll get another backup running over the top of it, the next day, etc.  It really dependends on the cause of the failure, but that's for you to decide.  If you must retry, then limit your retries, e.g.
    while [ $RETRIES -gt 0 ]
        let RETRIES=RETRIES-1
        ...  # Put the rest of the code here

6. Also, when you've tested the script manually and it works fine, then I suggest you put the output into a file, so you can easily find and examine it if the script fails, e.g.:
    0 7 * * * cd /path; ./script >script.out 2>script.err
or if you want STDOUT & STDERR in the same file:
    0 7 * * * cd /path; ./script >script.out 2>&1
or possibly append the output & errors to a logfile:
    0 7 * * * cd /path; ./script >>script.log 2>&1

7. If you are editing your own user account's crontab, you don't need to do:
    crontab -e -u myuserID
you can simply do:
    crontab -e
but I assume both would work if you are root.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
no nameAuthor Commented:
Thank you guys!
Thanks for the points, sbanderag.

Depending on your situation, if I were you I would probably state whether the backup succeeded or failed in the subject of the email, e.g.:

    if [ status == 0 ]
            echo "Backup successful" | mail -s "bkup_$(date) successful" garcia
            exit 0
            echo "Backup FAILED with status $status" | mail -s "bkup_$(date) FAILED" garcia

That way you don't have to open the email to check if the backup was successful - you can just look at the subject.

Also, if your email client allows it, I would set up a rule to highlight the email subject etc, in red (or whatever) if the backup failed.  I do such things in Outlook Express, but I don't know about other email clients.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.