Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   BASH backup script (http://www.programmingforums.org/showthread.php?t=44594)

carlw Apr 9th, 2012 4:13 AM

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.

watael Apr 9th, 2012 10:45 AM

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'

carlw Apr 9th, 2012 10:51 AM

Re: BASH backup script
 
Quote:

Originally Posted by watael (Post 234160)
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 Apr 9th, 2012 11:13 PM

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?

sharted Apr 10th, 2012 6:18 AM

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

carlw Apr 10th, 2012 6:30 AM

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 Apr 10th, 2012 6:56 AM

Re: BASH backup script
 
Here is some strange behaviour:
:

carl@ubuntubox:/mnt/backup$ ls
carlbackup-20120410.tar.gz
carl@ubuntubox:/mnt/backup$ sudo tar xfz carlbackup-20120410.tar.gz
carl@ubuntubox:/mnt/backup$ ls
carlbackup-20120410.tar.gz  home
carl@ubuntubox:/mnt/backup$ cd home
carl@ubuntubox:/mnt/backup/home$ cd carl
carl@ubuntubox:/mnt/backup/home/carl$ ls
backupscript  etc_passwd.awk  etc_passwd_printnice.sh  home  lovescript.pl  month2.pl  mystudentadd.plx
carl@ubuntubox:/mnt/backup/home/carl$ cd
carl@ubuntubox:~$ ls
backupscript  etc_passwd.awk  etc_passwd_printnice.sh  home  lovescript.pl  month2.pl  mystudentadd.plx  testest  z
carl@ubuntubox:~$ sudo ./backupscript carl i
Running Incremental Backup
--------------------------
Updating Backup File
tar: Removing leading `/' from member names
tar: /home/carl/z: file is the archive; not dumped
carl@ubuntubox:~$


It seems to create a file called "z" in my home. I'm not sure why it's doing that.
The script is:
:

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


function incbackup {
echo "Updating Backup File"
tar -ufz $TARGETDIR$USERFILE $SOURCEDIR
}

function fullbackup {
tar -cpfz $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


sharted Apr 10th, 2012 7:12 AM

Re: BASH backup script
 
Hi Carlw,

what happens if you change the command from:

:

tar -cpfz $TARGETDIR$OUTPUTFILE $SOURCEDIR


to

:

tar -cpzf $TARGETDIR$OUTPUTFILE $SOURCEDIR

?

I seem to remember something like the f option must be followed by the archive file... The examples in the man page all show this usage but I wasn't able (skimming through) to see this explicitly stated.

Maybe this is of some help... :icon_neutral:

carlw Apr 10th, 2012 8:13 AM

Re: BASH backup script
 
Quote:

Originally Posted by sharted (Post 234174)
Hi Carlw,

what happens if you change the command from:

:

tar -cpfz $TARGETDIR$OUTPUTFILE $SOURCEDIR


to

:

tar -cpzf $TARGETDIR$OUTPUTFILE $SOURCEDIR

?

I seem to remember something like the f option must be followed by the archive file... The examples in the man page all show this usage but I wasn't able (skimming through) to see this explicitly stated.

Maybe this is of some help... :icon_neutral:

There's an issue:
carl@ubuntubox:/mnt/backup$ ls
carlbackup-20120410.tar.gz
carl@ubuntubox:/mnt/backup$ cd
carl@ubuntubox:~$ sudo ./backupscript carl i
Running Incremental Backup
--------------------------
Updating Backup File
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.
carl@ubuntubox:~$

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 "Updating Backup File"
tar -uzf $TARGETDIR$USERFILE $SOURCEDIR
}

function fullbackup {
tar -cpzf $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


sharted Apr 10th, 2012 8:43 AM

Re: BASH backup script
 
Ok, I see. You could separate the tar and compression stages of the backup. That way for the incremental procedure, you can just uncompress, update and then recompress. Alternatively, you could not compress the backup at all...


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

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