3

Am trying to backup set folders based on yesterday's date by using a bash script and CRON task.

The folder structure of the site is like this:

/home/admin/domains/mysite.com/public_html/media/2014/March

And I would want to back-up that folder to this file:

/home/admin/domains/mysite.com/public_html/bk/mediabackup-March-2014.tar.gz

So created this script:


#!/bin/bash

NOW=$(date -d "12 hours ago" '+%m')

NOWYEAR=$(date -d "12 hours ago" '+%Y')

MONTHS=(Dummy January February March April May June July August September October November December)

NOWMONTH=${MONTHS[3]}

FILE="/home/admin/domains/mysite.com/public_html/bk/mediabackup-$NOWMONTH-$NOWYEAR.tar.gz"

PATH="/home/admin/domains/mysite.com/public_html/media/$NOWYEAR/$NOWMONTH"

tar -zcvf $FILE $PATH

When I run this script though shell, I would do this:

bash script.sh

And it could come up "command not found" - not sure why it's not working?

Any help would be great, thanks :)

10
  • Whats the result of echo ${FILE} and echo ${PATH} ? Commented Mar 29, 2014 at 14:03
  • Which command was not found? As for variables, it is the shell that deals with those. The program tar gets the actual values in its command line. (Also "command not found" implies that a command was not found, not that some file path on the command line was invalid) Commented Mar 29, 2014 at 14:04
  • 3
    You used PATH as a variable. You overwrote its default value therefore all commands can't be found, even bash.
    – alvits
    Commented Mar 29, 2014 at 14:06
  • 2
    Don't use PATH as a variable. Replace it with something like SOURCEPATH and you'll be fine.
    – alvits
    Commented Mar 29, 2014 at 14:08
  • 2
    Don't use uppercase variable names in the future to avoid namespace collisions. Commented Mar 29, 2014 at 14:12

2 Answers 2

8

By overriding the shell's built-in PATH variable, you are causing it to not find the tar command. Use another variable name, and generally, refrain from using uppercase variable names.

2

No need to hard-code the month names:

read year month < <(date -d "12 hours ago" "+%Y %B")
echo "$month-$year"
March-2014
1
  • Helpful, but it doesn't actually answer the question.
    – chepner
    Commented Mar 30, 2014 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.