Q. How do I use tar command over secure ssh session?
A. The GNU version of the tar archiving utility (and other old version of tar) can be use through network over ssh session. Do not use telnet command, it is insecure. You can use Unix/Linux pipes to create actives. Following command backups /wwwdata directory to dumpserver.nixcraft.in (IP 192.168.1.201) host over ssh session.
The default first SCSI tape drive under Linux is /dev/st0. You can read more about tape drives naming convention used under Linux here.
# tar zcvf - /wwwdata | ssh root@dumpserver.nixcraft.in "cat > /backup/wwwdata.tar.gz"
OR# tar zcvf - /wwwdata | ssh root@192.168.1.201 "cat > /backup/wwwdata.tar.gz"
Output:
tar: Removing leading `/' from member names /wwwdata/ /wwwdata/n/nixcraft.in/ /wwwdata/c/cyberciti.biz/ .... .. ... Password:
You can also use dd command for clarity purpose:# tar cvzf - /wwwdata | ssh ssh root@192.168.1.201 "dd of=/backup/wwwdata.tar.gz"
It is also possible to dump backup to remote tape device:# tar cvzf - /wwwdata | ssh ssh root@192.168.1.201 "cat > /dev/nst0"
OR you can use mt to rewind tape and then dump it using cat command:# tar cvzf - /wwwdata | ssh ssh root@192.168.1.201 $(mt -f /dev/nst0 rewind; cat > /dev/nst0)$
You can restore tar backup over ssh session: # cd /
If you wish to use above command in cron job or scripts then consider SSH keys to get rid of the passwords.
# ssh root@192.168.1.201 "cat /backup/wwwdata.tar.gz" | tar zxvf -
前面介绍过使用tar执行目录复制,以及使用tar和ssh实现远程复制的方法。今天为了执行备份,在网上找了如下一篇文章,介绍联合使用ssh和tar实现备份。原文是繁体的,为了阅读方便,将它转换为简体。 $target 表示目的机器,指远端机器 要求远端备份并拉回 local 解开本地备份并回存到远端去 备份 local 资料,丟到远端去压缩并存档 把远端资料拉回 local,作解压缩动作 |