![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Mar 2012
Posts: 29
Rep Power: 0
![]() |
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
exitThe 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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Nov 2011
Posts: 148
Rep Power: 2
![]() |
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' |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2012
Posts: 29
Rep Power: 0
![]() |
Re: BASH backup script
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2012
Posts: 29
Rep Power: 0
![]() |
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
exitI 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? |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Feb 2012
Posts: 119
Rep Power: 2
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2012
Posts: 29
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Mar 2012
Posts: 29
Rep Power: 0
![]() |
Re: BASH backup script
oh no! Sharted is gone....I'll never get an answer!
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jan 2011
Location: Australia
Posts: 195
Rep Power: 3
![]() |
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 Another alternative could be to put quotes around $@, or is it $*. One will give a word, the other a list of quoted words. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Mar 2012
Posts: 29
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Feb 2012
Posts: 119
Rep Power: 2
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |