Domanda

Hello I am trying to write a simple shell script to use in a cronjob to copy a backup archive of website files to a remote server via FTP.

The script below works when I type the file name in by hand manually, but with the date and filename specified as a variable it returns that it can't find ".tar.gz" as if it is ignoring the first part of the filename.

I would be grateful if someone could tell me where I am going wrong.

#!/bin/sh
NOW=$(date +"%F")
FILE="$NOW_website_files.tar.gz"

# set the local backup dir
cd "/home/localserver/backup_files/"

# login to remote server

ftp -n "**HOST HIDDEN**" <<END
user "**USER HIDDEN**" "**PASSWORD HIDDEN**"
cd "/backup_files"
put $FILE
quit
END
È stato utile?

Soluzione

This is because it is looking for a variable name NOW_website_files which does not exist, and thus the resulting file name evaluates to .tar.gz.

To solve it, do:

#!/bin/sh
NOW=$(date +"%F")
FILE="${NOW}_website_files.tar.gz"
       ^   ^

instead of

FILE="$NOW_website_files.tar.gz"

This way it will concatenate the variable $NEW to the _website_files.tar.gz text.

Altri suggerimenti

You could do this:

FILE=$(date +"%F_website_files.tar.gz")

instead of this:

NOW=$(date +"%F")
FILE="$NOW_website_files.tar.gz"

IMPORTANT

By the way, consider adding "bi" to your FTP script as you are clealy PUTting a binary file and you don't want CR/LF translation to occur in binary files...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top