Compress, encrypt and upload a directory to an FTP server in Linux

1

I want to accomplish something rather simple (at least it would seem to be):

  1. Compress a directory
  2. Encrypt the compressed file
  3. Upload the compressed & encrypted file via FTP

I also want to accomplish this in an automated fashion on Linux server.

TrueCrypt is not really an option in this case as it seems it can't compress a single file. Instead you have to make a volume that you mount and move the files into.

This question answers how to do the the upload and I know how to compress a directory. It is just the encryption that's giving me problems.

Kristoffer L

Posted 2010-05-25T14:44:20.503

Reputation: 113

Answers

2

I've used bcrypt for this pretty happily. Actually, I'm using gpg now. (Just checked, it's been that long since I automated the process involved. I don't recall why I switched, but I think it may have been that I felt gpg was easier to script.) My command line looks like this:

gpg -r RECIP --output OUTPUT_FILE_NAME --encrypt INPUT_FILE_NAME

...where

  • RECIP is the name of the key I want to use (the -r is for "recipient;" gpg mostly started life as a means of encrypting email; don't let that bother you, it's general-purpose).
  • OUTPUT_FILE_NAME is the output (encrypted) file
  • INPUT_FILE_NAME is the input file (cleartext) file

T.J. Crowder

Posted 2010-05-25T14:44:20.503

Reputation: 1 128

1

Is there a particular reason that you have to use FTP? Because if not, then SCP is going to be a better option. First because it uses an encrypted channel to send the files (no cleartext passwords for Google StreetView cars to record :-), second because it can use private key authentication, and third because it's really meant for copying.

Regardless, tar is the standard way to create a single file out of a directory, and the -z switch will use GZIP compression.

tar -zcvf OUTFILE.tgz DIRECTORY

Then you need to encrypt it. I use ccrypt, but there are other options. And maybe there are reasons not to use it that I don't know about.

ccrypt -k KEYFILE OUTFILE.tgz

Finally, copy it to your destination server. I'll assume that you have a "Transfer" directory under your home directory, and have set up private-key authentication.

scp OUTFILE.tgz.cpt YOURHOST:Transfer

If FTP really is a requirement, you can use curl to do the transfer. I've never done this myself (and have no FTP server with which to try it), but a quick read of the manpage indicates the following:

curl --upload-file OUTFILE.tgz.cpt -u USER:PASSWORD ftp:://YOURHOST/HOSTPATH

Anon

Posted 2010-05-25T14:44:20.503

Reputation: 21

Yes, there is a reason I have to use FTP.

ccrypt has no keys or are they created/stored separately? – Kristoffer L – 2010-05-26T06:47:20.773

Although the other answer seems to be what you want, I'll leave mine for someone else. I've edited to mention curl, and how to get a key from a file; man ccrypt will give you more info. – Anon – 2010-05-26T14:08:13.177