Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2012, 3:13 AM   #1
carlw
Programmer
 
Join Date: Mar 2012
Posts: 37
Rep Power: 0 carlw is on a distinguished road
BASH backup script

Hi Guys, I'm working on a backup script for a university project. It needs a lot of work but I'm a BASH beginner.

My issue is: I'm trying to implement a solution where the user can just enter as the first argument, the name of the account to backup and as the second argument, f for a full backup or i for an incremental backup.

My code:
#!/bin/bash
SOURCEDIR="/home/$1"
TARGETDIR="/mnt/backup/"
OUTPUTFILE=$1backup-$(date +%Y%m%d).tar.gz
USERFILE="$1backup-\w+.tar.gz"


function incbackup {
tar -xvfz $TARGETDIR$USERFILE
tar -uvfz $TARGETDIR$USERFILE
}

function fullbackup {
tar -zcvpf $TARGETDIR$OUTPUTFILE $SOURCEDIR
}

if [ $2 = f ]; then
echo "Running Full Backup"
fullbackup
exit
        elif [ $2 = i ]; then
        echo "Running Incremental Backup"
        incbackup
        exit
                else
echo "usage ./backupscript [username] [f for full or i for incremental]"
fi
exit


The full backup works OK I think. The incremental has trouble locating the existing file - I tried to use regex in the variable "$USERFILE" - I gather this won't work?

Also I've been reading through the "man tar". I think my flags are OK?
tar -xvfz $TARGETDIR$USERFILE <- "extract, listing verbosely, the file with zip"
tar -uvfz $TARGETDIR$USERFILE <- "update listing verbosely, the file with zip"

How do my flags look?

Thanks in advance.
carlw is offline   Reply With Quote
Old Apr 9th, 2012, 9:45 AM   #2
watael
Hobbyist Programmer
 
Join Date: Nov 2011
Posts: 155
Rep Power: 3 watael is on a distinguished road
Re: BASH backup script

hi,

bash doesn't understand regexes (except inside double square brackets with tilde)
You have to use globs, and extended globs.
Check bash's man page for 'Pathname Expansion'
watael is offline   Reply With Quote
Old Apr 9th, 2012, 9:51 AM   #3
carlw
Programmer
 
Join Date: Mar 2012
Posts: 37
Rep Power: 0 carlw is on a distinguished road
Re: BASH backup script

Quote:
Originally Posted by watael View Post
hi,

bash doesn't understand regexes (except inside double square brackets with tilde)
You have to use globs, and extended globs.
Check bash's man page for 'Pathname Expansion'
Thank you sir! I will read that tonight.
carlw is offline   Reply With Quote
Old Apr 9th, 2012, 10:13 PM   #4
carlw
Programmer
 
Join Date: Mar 2012
Posts: 37
Rep Power: 0 carlw is on a distinguished road
Re: BASH backup script

OK so after reading: http://wiki.bash-hackers.org/syntax/expansion/globs

I changed my code:

#!/bin/bash
SOURCEDIR="/home/$1"
TARGETDIR="/mnt/backup/"
OUTPUTFILE=$1backup-$(date +%Y%m%d).tar.gz
USERFILE="$1backup-*.tar.gz"


function incbackup {
echo "Extracting Previous Backup"
tar -xvfz $TARGETDIR$USERFILE
echo "Updating Backup File"
tar -uvfz $TARGETDIR$USERFILE
}

function fullbackup {
tar -zcvpf $TARGETDIR$OUTPUTFILE $SOURCEDIR
}

if [ $2 = f ]; then
echo "Running Full Backup"
echo "-------------------"
fullbackup
exit
        elif [ $2 = i ]; then
        echo "Running Incremental Backup"
        echo "--------------------------"
        incbackup
        exit
                else
echo "usage ./backupscript [username] [f for full or i for incremental]"
fi
exit

I had done a full backup on the 8th of April and here's the output when I try to do an incremental today:

carl@ubuntubox:~$ ./backupscript carl i
Running Incremental Backup
--------------------------
Extracting Previous Backup
tar: /mnt/backup/carlbackup-20120408.tar.gz: Not found in archive
tar: Exiting with failure status due to previous errors
Updating Backup File
tar: Removing leading `/' from member names
/mnt/backup/carlbackup-20120408.tar.gz
carl@ubuntubox:~$

Do I need different flags or is it just not possible to use gzip when doing this type of incremental backup?
carlw is offline   Reply With Quote
Old Apr 10th, 2012, 5:18 AM   #5
sharted
Hobbyist Programmer
 
Join Date: Feb 2012
Posts: 119
Rep Power: 3 sharted is on a distinguished road
Re: BASH backup script

I see several things you might benefit from considering:

1) you never check the return value of tar. You can do this by look at the $? variable;

2) I _think_ this error message might be because you have more than one backup in your /mnt/backup directory. When you pass more arguments to tar's extract capability it tries to extract subsequent files from the archive. I.e. imagine that

/mnt/backup/carlbackup-*.tar.gz

expands to

/mnt/backup/carlbackup-20120407.tar.gz /mnt/backup/carlbackup-20120408.tar.gz


tar will think you want to extract /mnt/backup/carlbackup-20120408.tar.gz from /mnt/backup/carlbackup-20120407.tar.gz. Given that this first file probably doesn't exist in the backup, you'll see the error message you see.

Of course, I could be completely wrong about this.... What's in your backup directory?

Have you tried doing what you want to do just at the command line?

hth
sharted is offline   Reply With Quote
Old Apr 10th, 2012, 5:30 AM   #6
carlw
Programmer
 
Join Date: Mar 2012
Posts: 37
Rep Power: 0 carlw is on a distinguished road
Re: BASH backup script

Nice to hear from you again Sharted!

You're right, it doesn't work on the command line.

carl@ubuntubox:~$ cd /mnt/backup
carl@ubuntubox:/mnt/backup$ ls
backup-20120408.tgz carlbackup-20120408.tar.gz
carl@ubuntubox:/mnt/backup$ tar -xvfz carlbackup-20120408.tar.gz
tar: z: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
carl@ubuntubox:/mnt/backup$ tar -xvfz /mnt/backup/carlbackup-20120408.tar.gz
tar: z: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now


OH WAIT I JUST FOUND THIS -- http://stackoverflow.com/questions/7...mmand-tar-xvfz
You leave the dash off the flags for some reason.
Also I need sudo - so I'll lookup how to implement sudo in to the script.
carlw is offline   Reply With Quote
Old Apr 25th, 2012, 8:45 AM   #7
carlw
Programmer
 
Join Date: Mar 2012
Posts: 37
Rep Power: 0 carlw is on a distinguished road
Re: BASH backup script

oh no! Sharted is gone....I'll never get an answer!
carlw is offline   Reply With Quote
Old Apr 25th, 2012, 9:19 AM   #8
Raybc
Hobbyist Programmer
 
Join Date: Jan 2011
Location: Australia
Posts: 195
Rep Power: 4 Raybc is on a distinguished road
Re: BASH backup script

Where's sharted? On a well-earned holiday?
I don't know much about archiving but the test command error is from the @ parameter expanding. Why not try
if [ "$2" = f ]; then
  echo "Running Full Backup"
  echo "-------------------"
  fullbackup
  exit
elif [ "$2" = i ]; then
  echo "Running Incremental Backup"
  echo "--------------------------"
  incbackup
  exit
else
  echo "usage ./backupscript [username] [f for full or i for incremental]"
fi
Putting quotes around the $2 returns a blank string even if $2 is not defined. You can drop the test on $1 altogether, for that part anyway.

Another alternative could be to put quotes around $@, or is it $*. One will give a word, the other a list of quoted words.
Raybc is offline   Reply With Quote
Old Apr 25th, 2012, 9:44 AM   #9
carlw
Programmer
 
Join Date: Mar 2012
Posts: 37
Rep Power: 0 carlw is on a distinguished road
Re: BASH backup script

A fellow Aussie! (I'm from Perth).
Thanks for your ideas, I'll have a think about them tomorrow when I get back to testing my script.
Carl
carlw is offline   Reply With Quote
Old Apr 26th, 2012, 8:00 AM   #10
sharted
Hobbyist Programmer
 
Join Date: Feb 2012
Posts: 119
Rep Power: 3 sharted is on a distinguished road
Re: BASH backup script

Sorry guys, I'm in the middle of moving house. Gordon Bennett, it's a lot of work! I should be done by the middle of next week.
sharted is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Backup Shell Script harry289 Bash / Shell Scripting 0 Sep 14th, 2011 12:05 AM
sql dumps and bash script tralala Other Programming Languages 7 Feb 23rd, 2011 8:26 PM
Bash script not doing the job when started by cron. mark9117 Bash / Shell Scripting 4 Jul 22nd, 2009 4:16 PM
Backup Script :-) Pizentios Perl 18 Jan 12th, 2006 10:50 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC