I would like to backup some of my very important data on a remote machine.
Currently I'm just saving it to my local machine by using this command: tar -cvjf ~/backups/Backup.tar.bz2 ~/importantfiles/*

I would prefer not using another command to transger it to the remote machine, meaning I would like to just have this command being upgraded so it can transfer the data to the remote machine.

This is designed to be in a script later that is suposed to run on its own, meaning any type of required user input would completly mess it up!

Something like

tar -cvjf sftp://user:pwassword@host/Backup.tar.bz2 ~/importantfiles/*
tar -cvjf  ftp://user:pwassword@host/Backup.tar.bz2 ~/importantfiles/*

would be perfect! (No pipes (etc.), just one command!)

  • Does it need to be ftp? You don't have ssh access to this machine?
    – terdon
    Oct 14, 2013 at 13:46
  • I do not have ssh access to the machine. It's just a backup server I rent. So yes it has to be ftp or sftp.
    – BrainStone
    Oct 14, 2013 at 13:48
  • @BrainStone SFTP is part of SSH
    – kurtm
    Oct 14, 2013 at 14:08
  • I know. But it is set up in a way that every ssh connection gets closed immediatley. SFTP works though. I know it is a wierd setup but that's the way it is.
    – BrainStone
    Oct 14, 2013 at 14:22
  • 1
    SFTP is available when SSH is not if they are using sftp internal server and your shell is set to /sbin/nologin. Jan 16, 2014 at 2:35

3 Answers 3


For SSH:

tar czf - . | ssh remote "( cd /somewhere ; cat > file.tar.gz )"


tar cvf $outfile . && echo "put $outfile" | sftp remote:/tmp/
Connecting to remote...
Changing to: /tmp/
sftp> put /tmp/test.tar.gz
Uploading /tmp/test.tar.gz to /tmp/test.tar.gz

Another SFTP:

sftp -b /dev/stdin remote >/dev/null 2>&1 << EOF
cd /tmp
get $outfile
echo $?
  • So how exactly would I use this? I mean how can I set which files should be in the tar ball. And where would I put the password? And how do I set it up so that I do not have to accept the hosts fingerprint?
    – BrainStone
    Oct 14, 2013 at 13:41
  • Please learn more about ssh, man ssh, man ssh-agent, man ssh_config answer all your doubts. Which files you define locally with tar arguments, then it is piped via ssh to remote host and output is redirected to file.
    – jirib
    Oct 14, 2013 at 13:49
  • Is it possible to do it without a pipe?
    – BrainStone
    Oct 14, 2013 at 13:51
  • This also can't work because it needs to be sftp (no ssh access) or ftp
    – BrainStone
    Oct 14, 2013 at 13:55
  • For ftp, see man lftp. If you need to have more complicated scenarios, use 'sftp -b' and have a file with commands or use 'here documents' (<< EOF).
    – jirib
    Oct 14, 2013 at 14:02

Tar doesn't speak ftp or sftp. That's not its job. You cannot do this with tar alone. Using appropriate tools for each job and combining them with the shell is the normal way of doing things on unix systems.

The most obvious solution is to create the archive locally, then copy it to the remote machine.

If you don't want to create the archive locally because you don't have enough room, you can create a named pipe, make tar write to this pipe, and find an (s)ftp client that can read from pipes. Unfortunately, sftp refuses to put a pipe. Some FTP clients work, for example lftp:

mkfifo f
tar -cvjf f ~/importantfiles/* &
sleep 2
lftp -f - <<EOF
open user@host
put f Backup.tar.bz2
rm f

Put your FTP password in ~/.netrc.

Alternatively, there is a way to make your tar command save to the remote server directly, but you need some prior setup. Mount the remote server over SSHFS or curlftpfs.

mkdir -p ~/net/host
sshfs host: ~/net/host
tar -cvjf ~/net/host/Backup.tar.bz2 ~/importantfiles/*
fusermount -u ~/net/host

one line solution:

tar Jcpvf - / | ftp -u ftp://user:[email protected]/full.backup.tar.xz -

it creates a stream to stdout and pass it to the ftp server without creating file. ftp is an usual ftp client on any *nix/BSD systems.

  • I'm interested in your solution, but on my setup it creates a 0 length destination file
    – Mark
    Mar 10 at 11:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .