Creating a zip file with a timestamp
You have various problems there.
-
First, you want to assign the result of the command
date +%b-%d-%y
to theTESTDATE
variable. To do so, you need to use command substitution :TESTDATE=$(date +%b-%d-%y)
What your script is currently doing is assignig the string
date +%b-%d-%y
toTESTDATE
, not the result of the command. You can test by running this:TESTDATE="date +%b-%d-%y" echo $TESTDATE
which will print
date +%b-%d-%y
. -
Second, you are using command substitution where you shouldn't, what you want is:
zipfile="${DEST}/${TESTDATE} ${d} ${BASENAME}.zip"
So, you don't want the parentheses. I also have no idea what you are trying to do with
basename
there.basename
is a default program and you don't need to place it in a variable to use it. The command above sets zipfile to/volume1/NetBackup/Backup/${TESTDATE} ${d} /usr/bin/basename
which is not a valid file name. If you explain what you're trying to do with basename I might be able to help but as it is it makes no sense. You seem to think that
DIRS
is a list, it's not, it is just the single directory/volume1/NetBackup
.Another point is a matter of style. You should always avoid using CAPITAL variable names because environmental variables are all caps and this might lead to problems if you use the same name.
Anyway, if I understand what you're trying to do here, you could write the script like this (I have made dirs
into an array since I think that's what you wanted):
#!/usr/bin/env bash
# date
testdate=$(date +%b-%d-%y)
# dirs to backup
dirs=("/volume1/NetBackup" "/volume1/foobar")
# Save zip file elsewhere on the server
dest="/volume1/NetBackup/Backup"
# set to "no" keep old zip file and add new file
delete_old_zip_files="no"
#Path to binary files
zip=/usr/syno/bin/zip
base=$(/usr/bin/basename ${dest})
echo "bb $base";
for d in ${dirs[@]}
do
## I assume this is what you wanted to do, it will just
## return the name of the current $d directory
base=$(/usr/bin/basename ${d})
zipfile="${dest}/${testdate} ${base}.zip"
echo -n "Creating $d $zipfile..."
# create zip file
${zip} -r $zipfile $d && echo "Done!" || echo ""
done
The example script above would create /volume1/NetBackup/Backup/Mar-05-14 NetBackup.zip
and /volume1/NetBackup/Backup/Mar-05-14 foobar.zip
. By the way, I would also strongly suggest you avoid creating file names with spaces, you'll thank me later.
Related videos on Youtube
Reqx
Updated on September 18, 2022Comments
-
Reqx over 1 year
I have been trying to create a script which compresses a directory with a timestamp and move it to another location on the same server, but I cant seem to get TESTDATE to display on the zip file, resulting in filed name .zip.
#!/bin/sh # date TESTDATE="date +%b-%d-%y" # dirs to backups DIRS="/volume1/NetBackup" # Save zip file elsewhere on the server DEST="/volume1/NetBackup/Backup" # set to "no" keep old zip file and add new file DELETE_OLD_ZIP_FILES="no" #Path to binary files BASENAME=/usr/bin/basename ZIP=/usr/syno/bin/zip for d in $DIRS do echo $DIRS zipfile="${DEST}/$(${TESTDATE} ${d} ${BASENAME}).zip" echo -n "Creating $zipfile..." # create zip file ${ZIP} -r $zipfile $d &>/dev/null && echo "Done!" done