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

x
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.

farzanjCommented:
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 :
do
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 \
/localservices

status=$?

if (( status == 0 ))
then
   echo "backup done" | mail "bkup_$(date)" email@server.com
   exit 0
fi
done

Open in new window

no nameAuthor Commented:
Great...now should I write a cron shell script to run it?
farzanjCommented:
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).

farzanjCommented:
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

farzanjCommented:
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?



#!/bin/sh
while [ true ]
do
echo "Backing up /dev/tape"
date
cd/
tar cvf /dev/tape . | wc - c
status=$?

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

-http://forums.fedoraforum.org/showthread.php?t=248436
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 http://www.debian-administration.org/articles/588
http://www.linux-magazine.com/w3/issue/105/060-061_kurt.pdf

-rsync scripting

#!/bin/bash
#  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
SOURCEPATH="/../source/"
DESTPATH="/../target/"
DESTHOST="10.6.40.81"
#The user you created with ssh_keys
DESTUSER="root"
LOCKFILE="/var/lock/rsyncbackup.lock"
LOGFILE="/var/log/rsyncbackup.log"
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
fi
# If it finished, create a new lock file
touch $LOCKFILE
# 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")
SECONDS=$(($STOPTIME - $STARTTIME))
echo "[$DONE]finished: - Tranfer took:$(((SECONDS/60)/60)) hours $(((SECONDS/60)%60)) minutes $((SECONDS%60)) seconds" >> $LOGFILE
# Remove lock file
rm -f $LOCKFILE
# END
#

Open in new window

tel2Commented:
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/...
or:
    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
not:
    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
    status=$?
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 .
    status=$?
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 ]
    then
        echo "backup done" | mail -s "bkup_$(date)" garcia
        exit 0
    else
        echo "Backup FAILED with status $status" | mail -s "bkup_$(date)" garcia
    fi
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.
    RETRIES=5
    while [ $RETRIES -gt 0 ]
    do
        let RETRIES=RETRIES-1
        ...  # Put the rest of the code here
    done

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!
tel2Commented:
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 ]
        then
            echo "Backup successful" | mail -s "bkup_$(date) successful" garcia
            exit 0
        else
            echo "Backup FAILED with status $status" | mail -s "bkup_$(date) FAILED" garcia
    fi

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.

Later.
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.