It’s a simple one-liner that one tends to forget (that is, one that I forget as I untar more than I manually tar). So creating a tar file from directory and all subdirectories is as simple as
tar -cvf file.tar directory
To compress it on the fly, pipe it to your favourite compression application (compress
, 7za
, gzip
) – I prefer gzip for speed, 7-zip for compression ratio on most data. So that would translate the line above into:
tar -cvf - directory | gzip -9c > file.tar.gz
or, for the corresponding 7zip version:
tar -cvf - directory | 7za a -si directory.tar.7z
From there, scp it to your location of choice! Remember that 7-zip does not maintain permissions and ownership, so tar it first before compression.
How to scp it?
scp -PPORT directory.tar.(7z|gz) USER@host:/path/to/transfer/location
If appropriate, use the keyless SSH login for this so that you need to enter the password manually in the process…
Oh, and should you want to add a $NOW or $DATE variable:
NOW="$(date +"%d-%m-%Y")"
allows you to use $NOW as a variable in your filename such as THIS_IS_MY_FILE_$NOW.tar
Just so that I have it documented – but you knew that already! 🙂