介紹html
rsync命令是一個遠程數據同步工具,可經過LAN/WAN快速同步多臺主機間的文件。rsync使用所謂的「rsync算法」來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不一樣部分,而不是每次都整份傳送,所以速度至關快。 rsync是一個功能很是強大的工具,其命令也有不少功能特點選項,咱們下面就對它的選項一一進行分析說明。web
經常使用場景一算法
無密碼同步vim
一、安裝rsync服務器
[root@localhost /]# yum -y install rsync
二、新建rsyncd.conf文件ssh
vim /etc/rsyncd.conf #This is the rsync daemon configuration #global settings pid file = /var/run/rsyncd.pid port = 873 lock file = /var/run/rsyncd.lock log file = /var/log/rsync.log gid = root uid = root #module settings [share_data] #共享名稱 path = /web/rsync/share_data #共享路徑 use chroot = no max connections = 15 read only = yes write only = no list = no ignore errors = yes timeout = 120 保存
三、執行命令工具
/usr/bin/rsync --daemon mkdir -p /web/rsync/share_data #新建共享目錄,實例中直接輸入要共享文件的就好,可省去次步
客戶端oop
一、安裝rsyncui
[root@localhost /]# yum -y install rsync
二、輸入命令進行同步spa
rsync -avz root@192.168.1.98:/var/space /home/hadoop/share_data
↓ ↓ ↓
服務器IP 配置文件共享目錄名 客戶端收納目錄
三、限制流量同步
限制流量同步:
rsync -avz --bwlimit=50 --progress root@192.168.1.98::share_data /home/hadoop/share_data
經常使用場景二
有密碼同步
服務端配置
一、配置文件修改
vim /etc/rsyncd.conf #This is the rsync daemon configuration #global settings pid file = /var/run/rsyncd.pid port = 873 lock file = /var/run/rsyncd.lock log file = /var/log/rsync.log gid = root uid = root #module settings [auth_data] path = /web/rsync/auth_data use chroot = no max connections = 15 read only = yes write only = no list = no ignore errors = yes timeout = 120 auth users = hadoop secrets file = /etc/rsyncd.passwd
三、執行命令
echo "hadoop:password123" > /etc/rsyncd.passwd chmod 600 /etc/rsyncd.passwd mkdir -p /web/rsync/auth_data
客戶端配置
一、保存密碼
echo "password123" > /home/hadoop/rsyncd.passwd chmod 600 /home/hadoop/rsyncd.passwd
二、遠程同步
rsync -avz --progress --password-file=/home/hadoop/rsyncd.passwd hadoop@192.168.1.98::auth_data /home/hadoop/auth_data 或者是: export RSYNC_PASSWORD="password123" rsync -avz --progress hadoop@192.168.1.98::auth_data /home/hadoop/auth_data
經常使用場景三
寫入同步
服務端
一、配置文件
vim /etc/rsyncd.conf #global settings pid file = /var/run/rsyncd.pid port = 873 lock file = /var/run/rsyncd.lock log file = /var/log/rsync.log gid = root uid = root #module settings [write_data] path = /web/rsync/write_data use chroot = no max connections = 15 read only = no list = no ignore errors = yes timeout = 120 auth users = hadoop secrets file = /etc/rsyncd.passwd 保存
二、執行命令
mkdir -p /web/rsync/write_data
客戶端
一、輸入同步命令
echo "123" > /home/hadoop/write_file export RSYNC_PASSWORD="password123" rsync -avz --progress --delete /home/hadoop/write_file hadoop@192.168.1.98::write_data
經常使用場景四
限定IP或網段
服務端
一、配置文件修改
#global settings pid file = /var/run/rsyncd.pid port = 873 lock file = /var/run/rsyncd.lock log file = /var/log/rsync.log gid = root uid = root #module settings [write_data] path = /web/rsync/write_data use chroot = no max connections = 15 read only = no list = no ignore errors = yes timeout = 120 auth users = hadoop secrets file = /etc/rsyncd.passwd hosts allow = 192.168.2.32 192.168.1.0/24
常見情景例子
指定端口
rsync -avz --port=8081 --progress /home/hadoop/auth_data hadoop@192.168.1.98::auth_data
限速
rsync --bwlimit=100 -avz --progress /home/hadoop/auth_data hadoop@192.168.1.98::auth_data
限速100kb/s同步數據
Rsync經過SSH傳輸
rsync -e "ssh -p 22" --progress /home/hadoop/auth_data hadoop@192.168.1.98::auth_data
本文參考:ggjucheng
更多命令參考