![]() |
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/bashThe 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. |
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' |
Re: BASH backup script
Quote:
|
Re: BASH backup script
OK so after reading: http://wiki.bash-hackers.org/syntax/expansion/globs
I changed my code: :
#!/bin/bashI 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? |
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 |
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. |
Re: BASH backup script
Here is some strange behaviour:
:
carl@ubuntubox:/mnt/backup$ lsIt seems to create a file called "z" in my home. I'm not sure why it's doing that. The script is: :
#!/bin/bash |
Re: BASH backup script
Hi Carlw,
what happens if you change the command from: :
tar -cpfz $TARGETDIR$OUTPUTFILE $SOURCEDIRto :
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: |
Re: BASH backup script
Quote:
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 |
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 8:25 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC