0

I was attempting to create a bash script to run a backup of my Raspberry Pi to my Box account via FTP. Most of it works, but for some reason it won't convert variables to the text they stand for. When I run this script:

#!/bin/bash

FILENAME=backup-$(date +%Y-%m-%d).tar.gz

tar -czvf /tmp/$FILENAME /home/pi/

lftp -c 'open -e "set ftps:initial-prot ""; \
   set ftp:ssl-force true; \
   set ftp:ssl-protect-data true; \
   put /tmp/$FILENAME; " \
   -u "USERNAME", "PASSWORD" \
   ftps://ftp.box.com:990/Automation/RPI/Backups

It creates the archive and connects to the server fine, but it gives me this error:

put: /tmp/$FILENAME: No such file or directory

I have tried replacing $FILENAME with "backup-$(date +%Y-%m-%d).tar.gz", but that still returns

put: /home/pi/+%Y-%m-%d).tar.gz: No such file or directory

and using "backup*.tar.gz" returns similar. I can only get it to work if I use the specific file name in place of any variables or wildcards, but this doesn't work for me since I want to set up a cron job to back up automatically.

So, does anybody know how to get around this, or a better alternative? Thanks!

2 Answers2

2

In case anybody has the same question, here is the script I wound up with:

#!/bin/bash

FILENAME="backup-$(date +%Y-%m-%d).tar.gz"

echo $FILENAME
tar -czvf /tmp/$FILENAME /home/pi/


lftp << EOF
        set ftps:initial-prot
        set ftp:ssl-force true
        set ftp:ssl-protect-data true
        open -u "USERNAME","PASSWORD" ftps://ftp.box.com:990/Automation/RPI/Backups
        put /tmp/$FILENAME
        bye
EOF

rm /tmp/$FILENAME
1

You are asking two different questions. In reverse order:

  1. A better alternative might be rsync. It's as easy as FTP but a lot smarter. I highly recommend it.

  2. Your variables are not resolving because you have them encapsulated with single quotes. Observe:

    $ foo=bar $ echo $foo bar $ echo '$foo' $foo $ echo "$foo" bar

If you rework your command such that it is encapsulated by double-quotes, things should start working.

  • I'll check out rsync, I heard about it but thought it was just for keeping remote folders in sync (I plan on removing the tar files when I'm done).

    As far as the quotes, I guess that's probably the problem. I copied the ftps commands from somewhere without fully understanding the syntax, so that's my bad.

    – Trevor Gross Jan 10 '16 at 20:24