- 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



