How To Use tar Command Through Network Over SSH Session

Published Wednesday, 23 Jun 2021 21:00 Linkedin // Facebook // Twitter

How do I use tar command over secure ssh session running on Linux or Unix-like system? How do I extract tar archive via SSH based network connection?

The GNU version of the tar archiving utility (and other old versions of tar) can be used through the network over ssh session. Do not use telnet/nc command due to insecure conection. You can use Unix/Linux pipes to create archives. Let us see some examples of how to use the tar command over ssh securely to create archives on Linux, BSD/macOS or Unix-like system.

Syntax for using tar command over ssh

The syntax is as follows to ssh into box and run the tar command:
ssh user@box tar czf - /dir1/ > /destination/file.tar.gz
OR
ssh user@box 'cd /dir1/ && tar -cf - file | gzip -9' >file.tar.gz
The following command backups /wwwdata directory to dumpserver.nixcraft.in (IP 192.168.1.201) host over ssh session:
# tar zcvf - /wwwdata | ssh [email protected] "cat > /backup/wwwdata.tar.gz"
OR
# tar zcvf - /wwwdata | ssh [email protected] "cat > /backup/wwwdata.tar.gz"
Sample outputs:

tar: Removing leading `/' from member names
/wwwdata/
/wwwdata/n/nixcraft.in/
/wwwdata/c/cyberciti.biz/
....
..
...
Password:

In this example archive /data2/ with gpg:
$ tar zcf - /data2/ | gpg -e | ssh vivek@nas03 'cat - > data2-dd-mm-yyyy.tar.gz.gpg'
Please note that you may get an error that read as follows with ssh command when using with sudo or any other command that needs a pseudo-terminal allocation:

sudo: sorry, you must have a tty to run sudo

To avoid this problem pass the -t option to the ssh command:
# tar zcvf - /wwwdata | ssh-t[email protected] "sudocat > /backup/wwwdata.tar.gz"

Use of tar command over ssh sessions

Copying from the remote machine (server1.cyberciti.biz) to local system is as follows:
$ cd /path/local/dir/
$ ssh [email protected] 'tar zcf - /some/dir' | tar zxf -

Linux system hard drive backup/mirror using tar and ssh

Let us copy the entire hard disk drive named /dev/sdvf from local machine to the remote AWS EC2 cloud backup server:
# dd if=/dev/sdvf | ssh backupimg@vpc-aws-mumbai-backup-001 'dd of=prod-disk-hostname-sdvf-dd-mm-yyyy.img'
To restore a local drive from the image on the server, reverse the command. For instance, we can restore a local hard disk drive from the image stored on the remote AWS EC2 cloud backup server as follows:
# ssh backupimg@vpc-aws-mumbai-backup-001 'dd if=prod-disk-hostname-sdvf-dd-mm-yyyy.img' | dd of=/dev/sdvf

Moving data to a new Linux system

The problem with scp and other command copying the directory structure is that Symbolic links, special devices, sockets, named pipes, and other stuff not copied. Hence, we use tar over ssh. For example, copy all data from nuc-box. Open the terminal on x230 laptop and run the ssh command along with tar command:
$ ssh vivek@nuc-box 'tar czf - /home/vivek' | tar xvzf - -C /home/vivek

Use tar command through network over SSH session for tape device

The default first SCSI tape drive under Linux is /dev/st0. You can read more about tape drives naming convention used under Linux here . You can also use dd command for clarity purpose:
# tar cvzf - /wwwdata | ssh [email protected] "dd of=/backup/wwwdata.tar.gz"
It is also possible to dump backup to remote tape device:
# tar cvzf - /wwwdata | ssh [email protected] "cat > /dev/nst0"
One can can use mt command to rewind tape and then dump it using cat command :
# tar cvzf - /wwwdata | ssh [email protected] $(mt -f /dev/nst0 rewind; cat > /dev/nst0)$

How to extract tar over ssh

The syntax is pretty simple:
$ cat my-data.tar.gz | ssh [email protected] "tar zxvf -"
cat my-data.tar.gz | ssh [email protected] "cd /path/to/dest/; tar zxvf -"
In this example, restore tar backup over ssh session from the remote machine to local dir:
# cd /
# ssh [email protected] "cat /backup/wwwdata.tar.gz" | tar zxvf -
If you wish to use above command in cron jobs or scripts then consider SSH keys to get rid of the passwords.

How to tar over SSH with progress bar

The pv command allows you to see the progress of data through a pipeline. So the syntax is:
$ cd /dir/to/backup/
$ tar zcf - . | pv | ssh [email protected] "cat > /backups/box42/backup-dd-mm-yyyy.tgz"
$ cd /tmp/data/
$ tar zcf - . | \
pv | \
ssh vivek@centos7 "cat > /tmp/data.tgz"

Some more examples of tar over ssh:

$ tar cvjf - * | ssh vivek@nixcraft "(cd /dest/; tar xjf -)"
$ tar cvzf - mydir/ | ssh vivek@backupbox "cat > /backups/myfile.tgz"
$ tar cvzf - /var/www/html | ssh [email protected] "dd of=/backups/www.tar.gz"
$ ssh vivek@box2 "cat /backups/www.tar.gz" | tar xvzf -
$ tar cvjf - * | ssh [email protected] "(cd /dest/; tar xjf - )"
Make sure you read the tar command/ssh command/bash command man page for more info:
$ man tar
$ man bash
$ man ssh

A note about SSHFS – a FUSE filesystem

You can use sshfs to mount a remote directory and run tar command :
mkdir /data/
sshfs [email protected]:/ /data/
tar -zcvf /data/file.tar.gz /home/vivek/

Conclusion

You learned how to use the tar command over ssh sessions to transfer archives, files, and images securely. See GNU/tar home page here for more info .

**Source : How To Use tar Command Through Network Over SSH Session

comments powered by Disqus