Bash backup script for review

Any other programming language that is not covered in the Web Scripting category.

Bash backup script for review

Postby safety on Tue Jan 03, 2006 8:02 am

Here is a script to look over and make suggestions. This will hopefully be a good learning experience for me.
Code: Select all
#!/bin/bash

##########################################################
# Check first to see if the backup directory has multiple
# copies of backups.
# Next, check for the existence of only one backup file.
# Finally, if the directory does not exist, create it.
##########################################################

# Create variables
# Put what you want to back up
FILES=$HOME/

# This is the path to different user accounts
USER1=/Users/user
USER2=/Users/user1

# This is where you want the backs up to go
# For instance /Volumes/external_hard_drive
BACKUP=/volumes/space

echo
echo "This program will attempt to backup your home directory."
echo "Please make sure you have edited the variables in the file \
before it starts."

# Make sure the user is ready to run the program.
question=
while [ x$question = x ]; do
       echo
       echo -n "Are you ready? [y/n]: "
       read ans
       case $ans in
               y* | Y*)
                       question=1
                       ;;
               n* | N*)
                       echo "Please restart this program when you are ready.";
                       echo
                       exit 1
                       ;;
       esac
done
echo "Starting backup tests."

if [ "$HOME" = "$USER" ];
then
ARCHIVENAME=user.tar
COMPRESS=user.tar.bz2
COPY=user1.tar.bz2
elif [ "$HOME" = "$USER1" ];
then
ARCHIVENAME=user1.tar
COMPRESS=user1.tar.bz2
COPY=user1_1.tar.bz2
else
echo "$HOME can't be found."
fi

if [ -e $BACKUP/$COPY ];
then
cd $HOME
echo "Rotating and backing up files..."
echo "This may take some time depending on file sizes."
find $FILES -iname ".ds_store" -delete > /dev/null 2>&1
rm $BACKUP/$COPY > /dev/null 2>&1
mv $BACKUP/$COMPRESS $BACKUP/$COPY > /dev/null 2>&1
tar -cf $ARCHIVENAME $FILES > /dev/null 2>&1
bzip2 -9 $ARCHIVENAME > /dev/null 2>&1
cp -f $COMPRESS $BACKUP > /dev/null 2>&1
rm $COMPRESS > /dev/null 2>&1
elif [ -e $BACKUP/$COMPRESS ];
then
cd $HOME
echo "Rotating files for the first time and creating backups..."
echo "This may take some time depending on file sizes."
find $FILES -iname ".ds_store" -delete > /dev/null 2>&1
mv $BACKUP/$COMPRESS $BACKUP/$COPY > /dev/null 2>&1
tar -cf $ARCHIVENAME $FILES > /dev/null 2>&1
bzip2 -9 $ARCHIVENAME > /dev/null 2>&1
cp -f $COMPRESS $BACKUP > /dev/null 2>&1
rm $COMPRESS > /dev/null 2>&1
else
cd $HOME
echo "A backup directory does not exist, and will be created."
echo "This may take some time depending on file sizes."
find $FILES -iname ".ds_store" -delete > /dev/null 2>&1
mkdir $BACKUP > /dev/null 2>&1
tar -cf $ARCHIVENAME $FILES > /dev/null 2>&1
bzip2 -9 $ARCHIVENAME > /dev/null 2>&1
cp -f $COMPRESS $BACKUP > /dev/null 2>&1
rm $COMPRESS > /dev/null 2>&1
echo "A backup directory has been created"
fi
if [ -e $BACKUP/$COPY ];
then
echo "Your daily backups are complete."
elif [ -e $BACKUP/$COMPRESS ];
then
echo "Jolly good show, the backup worked!"
else
echo "Dagnabit, the backup failed. Time to debug."
fi

exit 0
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." Image
safety
Contributer
Contributer
 
Posts: 36
Joined: Mon Oct 24, 2005 5:23 am
Location: USA

Postby ravenhair on Tue Jan 03, 2006 4:19 pm

does it work? What don't you like about it?

comments:

why no command line options? I'd put in the ablility to set the options from the command line. I mean what id you want to put this into a cron job? at l east let me atuomaticaly say yes.
ravenhair
Regular
Regular
 
Posts: 80
Joined: Tue Nov 08, 2005 5:51 pm

Postby safety on Tue Jan 03, 2006 5:58 pm

ravenhair wrote:does it work? What don't you like about it?

comments:

why no command line options? I'd put in the ablility to set the options from the command line. I mean what id you want to put this into a cron job? at l east let me atuomaticaly say yes.


Yes, the script works fine. I want comments and suggestions on it. How can we streamline it and make it better? For instance, instead of the variables for different users to back up their home directories, I should have used:
Code: Select all
ARCHIVENAME=`basename -a $HOME.tar`
COMPRESS=`basename -a $HOME.tar.bz2`
COPY=`basename -a $HOME\1.tar.bz2`

That will save several lines of code and need less user input. I can also use a function to create the archive and move it around.
Code: Select all
function backer {
        find $FILES -iname ".ds_store" -delete > /dev/null 2>&1
        cd $HOME
        tar -cf $ARCHIVENAME $FILES > /dev/null 2>&1
        bzip2 -9 $ARCHIVENAME > /dev/null 2>&1
        cp -f $COMPRESS $BACKUP > /dev/null 2>&1
        rm $COMPRESS > /dev/null 2>&1
}
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." Image
safety
Contributer
Contributer
 
Posts: 36
Joined: Mon Oct 24, 2005 5:23 am
Location: USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron