KG -
KG -

Reputation: 7170

extract file name from shell script that creates file with date suffix

My working command to run regular backups:

 ditto -c -k --sequesterRsrc --keepParent ~/Library/'Application Support/MyProg'/ ~/Dropbox/'Application Support/MyProg'/`date "+MP_backup_%Y-%m-%d_%H%M%S"`.zip

This creates a nice file: MP_backup_2012-02-21_133445.zip. But I want to structure this script and provide a nice growl notification when complete. How would i extract the generated filename and provide a nice growl notification like "Backup succesfully complete. Backed up at MP_backup_2012-02-21_133445.zip"

My code/pseudocode goes something like this:

 ## Set the Backup path ##
 BKP="~/Dropbox/Application Support/MyProg"

 ## Set KM Source path ##
 SRC="~/Library/Application\ Support/MyProg/"

 # Creat a zipped bkup with dated suffix
 ditto -c -k --sequesterRsrc --keepParent "$SRC" "$BAK/`date "+MP_backup_%Y-%m-%d_%H%M%S"`.zip

 ## Growl a message saying the the File name is successfully created, if created else throw error ##

 if [ <backup created> ] 
  then
    growl/echo "Backup succesfully complete. Backed up at MP_backup_2012-02-21_133445.zip"
  else 
    echo "Me no find Backup FAIL!"
   fi 

Would greatly appreciate guidance in converting above pseudocode to beautiful glorious working code.

Cheers.

Thanks peeps, info from all provided answers were used to come up with the below final working solution:

## Set the Backup path ##
bkp="Dropbox/Application Support/My Prog"

## Set KM Source path ##
src="Library/Application Support/My Prog"

## Preferred Date Suffix ##
filename=$(date +"MP_backup_%Y-%m-%d_%H%M%S.zip")

# Creat a zipped bkup with dated suffix
ditto -c -k --sequesterRsrc --keepParent ~/"$src"/ ~/"$bkp"/"$filename"

## Growl the File name is successfully created ##
if [ -f ~/"$bkp"/"$filename" ]
  then
    echo "Backup successfully complete. Backed up at ~/$bkp/$filename";
  else 
    echo "Me no find Backup FAIL!";
fi 

Upvotes: 1

Views: 920

Answers (3)

Richzendy
Richzendy

Reputation: 41

perhaps:

## Set the Backup path ##
 BKP="~/Dropbox/Application Support/MyProg"

 ## Set KM Source path ##
  SRC="~/Library/Application\ Support/MyProg/"

DATE=`date +"%Y-%m-%d_%H%M%S"`

 # Creat a zipped bkup with dated suffix
 ditto -c -k --sequesterRsrc --keepParent "$SRC" "$BAK/MP_backup_$DATE.zip

 ## Growl a message saying the the File name is successfully created, if created else throw error ##

 if [ <backup created> ] 
      then
growl/echo "Backup succesfully complete. Backed up at MP_backup_$DATE.zip"
  else 
    echo "Me no find Backup FAIL!"
    fi 

Upvotes: 1

Marc B
Marc B

Reputation: 360702

Generate the filename separately and store it in a variable, so you can use that variable elsewhere:

filename="$BAK/`date "+MP_backup_%Y-%m-%d_%H%M%S"`.zip
ditto -c -k --sequesterRsrc --keepParent "$SRC" "$filename"

...

growl/echo "Backup succesfully complete. Backed up at $filename"

Upvotes: 1

Eduardo Ivanec
Eduardo Ivanec

Reputation: 11862

Just move the date invocation to a variable, then reuse it:

# Create a zipped bkup with dated suffix
BACKUPFILE=`date "+MP_backup_%Y-%m-%d_%H%M%S"`
ditto -c -k --sequesterRsrc --keepParent "$SRC" "$BAK/$BACKUPFILE.zip

(...)

growl/echo "Backup succesfully complete. Backed up at $BACKUPFILE"

Upvotes: 1

Related Questions