Re: Problem with IF statement
Colin Copland wrote:
>
> I have a script I have written to backup a simple fileserver but am
> having problems with the if statement in it.
>
> No matter the date a full backup is run. What I wish to happen is a
> full backup to be run on a Fri and all other days a incremental one
> should run.
>
> Does anyone see any apparent errors in the script?
>
> Thanks,
> Colin
>
> =========================================== Backup Script ========
>
> #!/bin/bash
>
> # Set dates
>
> bdate='date +%a%m%Y'
> mdate=`date +%d%m%Y`
>
> if
>
> # Check whether full or inc backup is required
>
> ${bdate}="Fri%m%Y"
What is this supposed to do.
That's no condition as you might expect. First of all you need some
test operator to embed your condition, either if [ ... ] or if [[
.... ]]
Read any manual to learn about basic shell syntax.
Then, I guess, you'd rather want to dynamically interrogate the current
date, instead of comparing against a string. The output of the date
command as a value can be embedded in your program like $(date ...)
Read also about how to use the different quotes "...", '...', and
`...`.
You might want something like...
case $(date +%d%m%Y) in
Fri*) : whatever to do on Friday
;;
*) : whatever to do on other days
;;
esac
(Even a simpler date format would do the job if you just compare the
week day, BTW.)
[skipping the rest of your program]
Janis
>
> then
>
> #---------------------
> # Full Backup
> #---------------------
>
> # Remove old backup files older than 30 days
>
> PATHTOCLEAN="/var/backup"
> DAYSOLD="30"
> find $PATHTOCLEAN -mtime +$DAYSOLD -exec rm {} \;
>
> # Backup /home directories to /var/backup
>
> tar -czf /var/backup/backup-${mdate}-full.tar.gz /home/colin /home/sarah
>
> # Burn backup file to DVD
>
> # Erase dvd in drive
> dvd+rw-format -force /dev/hdc
>
> # Create the ISO image
> mkisofs -r -o /tmp/backup.iso /var/backup/backup-${mdate}-full.tar.gz
>
> # Write the ISO onto the DVD
> growisofs -Z /dev/hdc=/tmp/backup.iso
>
> else
>
> #---------------------
> # Incremental Backup
> #---------------------
>
> # Remove old backup files older than 30 days
>
> PATHTOCLEAN="/var/backup"
> DAYSOLD="30"
> find $PATHTOCLEAN -mtime +$DAYSOLD -exec rm {} \;
>
> # Backup /home directories to /var/backup
>
> find /home/colin /home/sarah -mtime -1 -type f -print | tar zcvf
> /var/backup/backup-${mdate}-inc.tar.gz -T -
>
> fi
>
> --
> GNU/Linux
> 09:14:01 up 1:10, 1 user, load average: 0.62, 0.43, 0.40
|