--- tags: Unix-like --- # rsync & scp [TOC] ## rsync 可以將檔案或目錄從來源位置複製到目的位置 * 可用於主機資料同步,也可以用於異地備份 * 支援連結檔與設備檔 * 可以保留檔案的擁有者、群組與權限設定 * 以 delta transfer 演算法檢查新舊檔案之間的差異,**只傳送有變動的部份**,可加快備份速度 * 支援資料的自動壓縮與解壓縮,有效減少耗費的網路頻寬 ### 參數 | Option | Description | |--------|-------------| | `-v` | verbose 模式,輸出比較詳細的訊息 | | `-r` | recursive 遞迴備份所有子目錄下的目錄與檔案 | | `-a` | 除了遞迴備份,並將子目錄及目錄下檔案,擁有者、群組、權限及時間紀錄等,所有設定一併保留 | | `-z` | 啟用壓縮 | | `-h` | 將數字以比較容易閱讀的格式輸出,如 1059 → 1.06K | | `-P` | Progress,顯示傳輸進度 | | `--dry-run` | 輸出訊息,不會更動到任何的檔案 | | `--delete` | 同步將不存在於來源端的檔案刪除 | | `--include` | 只包含特定檔案,`--include '*.txt'` | | `--exclude` | 排除特定檔案,`--exclude '*.txt'` | | `--remove-source-files` | 自動刪除來源檔案 | | `--existing` | 只更新目的端既有檔案 | 搭配 `find` 使用: ```shell $ find . -name "whatever.*" -print0 | rsync -av --files-from=- --from0 ./ ./destination/ ``` ## scp * 指令跟 `cp` 類似,只不過 scp 可以透過 SSH 安全加密傳輸的方式,在不同的 Linux 主機之間複製檔案。 * 如果本地端的使用者帳號名稱和遠端的使用者帳號一樣,可以將使用者帳號省略 ```shell scp /path/file1 myuser@192.168.0.1:/path/file2 ``` ### 參數 | Option | Description | |--------|-------------| | `-v` | verbose 模式,輸出比較詳細的訊息 | | `-r` | recursive 遞迴備份所有子目錄下的目錄與檔案 | | `-p` | 保留檔案時間與權限 | | `-C` | 資料壓縮之後再傳送,減少網路頻寬的使用量 | ## 參考資料 * [Linux 使用 rsync 遠端檔案同步與備份工具教學與範例 ](https://blog.gtwang.org/linux/rsync-local-remote-file-synchronization-commands/) * [Linux 的 scp 指令用法教學與範例:遠端加密複製檔案與目錄](https://blog.gtwang.org/linux/linux-scp-command-tutorial-examples/)
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.