note: 用 ssh + tar 備份檔案到遠端

2011051922:22


備份本地資料 並 傳到遠端主機:

$ tar cvf - /home/test | ssh [email protected] "cat > /tmp/backup.tar"

$ tar zcvf - /home/test | ssh [email protected] "cat > /tmp/backup.tar.gz"   (先在本地 gzip 壓縮後再傳出去)
 
下命令後會出現:

tar: Removing leading `/' from member names
/home/test/333.jpg
/home/test/222.jpg
/home/test/111.jpg
[email protected]'s password:   (輸入密碼)

 



將遠端的備份檔 複製回來並解開:

$ cd /   (注意!要切換到根目錄)

$ ssh [email protected] "cat /tmp/backup.tar" | tar xvf -

$ ssh [email protected] "cat /tmp/backup.tar.gz" | tar zxvf -    (抓回來,在本地端解 unzip)
 

直接將整個目錄 複製到遠端 (非打包成單一檔案)


tar -zc /home/mike/tmp | ssh [email protected] "tar -zx -C /"
tar: Removing leading `/' from member names
[email protected]'s password:

遠端 (192.168.1.100)
/home/mike 也會複製上相同的 tmp 目錄

-----

$ pwd
/mnt/sda1/

注意這兩個路徑的不同:

$ tar -zc /mnt/sda1/data | ssh [email protected] "tar -zx -C /usr/local/mysql"
   將 data 放在 192.168.0.235 的 /usr/local/mysql/mnt/sda1 底下

$ tar -zc data | ssh [email protected] "tar -zx -C /usr/local/mysql"
  將 data 放在 192.168.0.235 的 /usr/local/mysql 底下
 




參考:
http://www.cyberciti.biz/faq/howto-use-tar-command-through-network-over-ssh-session/