LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-03-2005, 01:43 PM   #1
phatboyz
Member
 
Registered: Feb 2004
Location: Mooresville NC
Distribution: CentOS 4,Free BSD,
Posts: 358

Rep: Reputation: 30
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 ----------"

Last edited by phatboyz; 06-03-2005 at 02:50 PM.
 
Old 06-05-2005, 11:18 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Try this page on the 'find' cmd:
http://www.tldp.org/LDP/abs/html/moreadv.html
 
Old 06-09-2005, 03:09 PM   #3
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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.
 
Old 06-10-2005, 07:10 AM   #4
phatboyz
Member
 
Registered: Feb 2004
Location: Mooresville NC
Distribution: CentOS 4,Free BSD,
Posts: 358

Original Poster
Rep: Reputation: 30
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.
 
Old 06-11-2005, 12:36 PM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using CP in my backup script. Echo Kilo Programming 6 06-01-2005 08:43 AM
Need a backup script enygma Linux - General 5 11-04-2004 03:49 PM
Script Backup Buto Linux - General 2 10-18-2004 05:56 PM
Things to backup when changing distro? Kernel_Sanders Linux - General 3 02-03-2004 11:49 AM
Backup Script imsajjadali Linux - General 7 01-28-2004 03:30 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration