LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help changing backup script (https://www.linuxquestions.org/questions/programming-9/help-changing-backup-script-330007/)

phatboyz 06-03-2005 01:43 PM

Help changing backup script
 
At the bottom off this post is the backup script that I am using that I found off the net. What I need to do is on saturday erase the tape using mt erase then on sunday morning I will use cat to write to the tape, but is gotta check the date to get the file name. Then on the rest of the week monday / saturday I'll neet to write the incrementals. The problem is that this is way over my head and I need some guidence. I'd like to learn more about bash scripting if I had time so I am asking for help. It doesn't need to be a part of this script. Actually I don't want it to be apart. What I do need to know is how to search for a file in a directly by date or something.
I am sure someone here know what I am looking for. Also can someone show me how to remove filles older than 5 days. I'd like to keep 5 days worth and then delete anything older than that.
#!/bin/sh

# jason pepas's backup script - see http://jason.pepas.com
# shell script tutorials:
# http://www.freeos.com/guides/lsst/
# http://www.linuxnewbie.org/nhf/inte...bashscript.html


# --------------------------------------------------
# set variables:
# --------------------------------------------------


directoryname=`date +%Y-%m-%d`"_"`hostname`"_backup"
current="current_"`hostname`
fullbackuplabel="Full Backup of "`hostname`" on "`date '+%B %e, %Y'`
fullbackupname=`date +%Y-%m-%d`"_full.tar.gz"
fullbackuplogname=`date +%Y-%m-%d`"_full.log"
incrementalbackuplabel="Incremental Backup of "`hostname`" on "`date '+%B %e, %Y'`
incrementalbackupname=`date +%Y-%m-%d`"_incremental"`date +%H%M`".tar.gz"
incrementalbackuplogname=`date +%Y-%m-%d`"_incremental"`date +%H%M`".log"


# --------------------------------------------------
# functions:
# --------------------------------------------------


fullbackup()
{
# create backup directory
if test ! -e /backup/$directoryname; then
echo "Creating /backup/$directoryname directory"
mkdir /backup/$directoryname
fi

# create (or update) a shortcut called current to this directory
echo "Updating /backup/$current pointer"
rm /backup/$current
ln -s /backup/$directoryname /backup/$current

# keep track of creation date of full backup (used with incremental backups)
echo "Updating /backup/$current/lastfullbackupdate"
date>/backup/$current/lastfullbackupdate

# create backup
echo "Running tar..."
tar --create --label "$fullbackuplabel" --files-from /backup/scripts/whattobackup --exclude-from /backup/scripts/whatnottobackup --ignore-failed-read --absolute-names --verbose --gzip --file /backup/$current/$fullbackupname > /backup/$current/$fullbackuplogname 2>&1
gzip /backup/$current/$fullbackuplogname
echo "Done. Created /backup/$current/$fullbackupname"
echo "To view the log, type:"
echo " zcat /backup/$current/$fullbackuplogname"
}


incrementalbackup()
{
# create variable with date of last full backup
lastfullbackupdatevar=`cat /backup/$current/lastfullbackupdate`

# check for existence of incremental backup
if test -e "/backup/$current/$incrementalbackupname"; then

echo "Your last incremental backup was less than 60 seconds ago."
echo "Wait a minute and try again."

else

# create incremental backup
echo "Running tar..."
tar --create --label "$incrementalbackuplabel" --files-from /backup/scripts/whattobackup --exclude-from /backup/scripts/whatnottobackup --ignore-failed-read --after-date "$lastfullbackupdatevar" --absolute-names --verbose --gzip --file
/backup/$current$incrementalbackupname > /backup/$current $incrementalbackuplogname 2>&1

gzip /backup/$current/$incrementalbackuplogname
echo "Done. Created /backup/$current/$incrementalbackupname"
echo "To view the log, type:"
echo " zcat /backup/$current/$incrementalbackuplogname"

fi
}


# --------------------------------------------------
# main routine:
# --------------------------------------------------

# first get a list of all packages installed.
#dpkg --get-selections > /etc/apt/selections

# clear out apt's packages
#apt-get clean

# now perform the backup.
echo "---------- Backup Script Running... ----------"

if test `date +%A` = "Sunday" && ! -e "/backup/$directoryname"; then

# if it is sunday and you havent yet done a full backup, do so
echo "Performing Weekly Full Backup..."
fullbackup;

elif test ! -e /backup/$current/*full.tar.gz; then

# if there is no current fullbackup, make one
echo "No Current Full Backup - Performing Full Backup Now..."
fullbackup;

else

# otherwise, do an incremental backup
echo "Performing Incremental Backup..."
incrementalbackup;

fi # end if statement

echo "---------- Backup Script Done ----------"

chrism01 06-05-2005 11:18 PM

Try this page on the 'find' cmd:
http://www.tldp.org/LDP/abs/html/moreadv.html

osvaldomarques 06-09-2005 03:09 PM

Hi Phatboyz,

I took a look at the direction pointed by Chrism01 and I thought is very interesting. But, resuming your needs, I understood you need to give some decisions based on the day of the week. I would recommend you to give a look into the man page for "date". It is a powerful command and is frequently used on the script you use. For you get a numeric day of the week you could use:
Code:

DOW=`date +"%w"`
It will give a number between 0 and 6, 0 is Sunday and 6 is Saturday. Then you have to decide above its value
Code:

if [ $DOW -eq 0 ]; then
  # run the Sunday routines
elif [ $DOW -eq 6 ]; then
  # run the Saturday routines
else
  # run the normal week days routines
fi

or
Code:

case $DOW in
  0)
      # run Sunday routines
      ;;
  6)
      # run Saturday routines
      ;;
  *)
      # run the normal week days routines
      ;;
esac

For removing the old files, the right choice is the find command; (read the man page for it)
Code:

find <directory> -type f -mtime +5 -exec rm {} \;
Be careful to do not save any file which you can preserve in this directory because it will vanish after five days.

phatboyz 06-10-2005 07:10 AM

Thanks, Yea I started using the find command with -name to find just *.tar.gz and this seems to work fine. Yea I am going to go back and use your code for the date and stuff. I think it will be easyer to have cron run it everyday and then let the script decide when it wants to do something.

Thank you for all your help and I am sure that I am not done either.

eddiebaby1023 06-11-2005 12:36 PM

Quote:

Originally posted by phatboyz
Thanks, Yea I started using the find command with -name to find just *.tar.gz and this seems to work fine. Yea I am going to go back and use your code for the date and stuff. I think it will be easyer to have cron run it everyday and then let the script decide when it wants to do something.

Thank you for all your help and I am sure that I am not done either.

If you're running the job through cron, make three scripts - one for each case above, and tell cron to run them on the appropriate day(s).


All times are GMT -5. The time now is 10:22 AM.