<!DOCTYPE html>
	<html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
	<head>
<title>Sử dụng Rsync đồng bộ thư mục trên Linux và Windows</title>
<meta name="description" content="Sử dụng Rsync đồng bộ thư mục trên Linux và Windows - Savefile - Tin Tức -...">
<meta name="author" content="PHAN MEM THIEN HA">
<meta name="copyright" content="PHAN MEM THIEN HA [phamhuy842005@gmail.com]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.0">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Sử dụng Rsync đồng bộ thư mục trên Linux và Windows">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - http&#x3A;&#x002F;&#x002F;thienhashop.com&#x002F;news&#x002F;savefile&#x002F;huong-dan-phan-mem&#x002F;su-dung-rsync-dong-bo-thu-muc-tren-linux-va-windows-192.html">
<meta property="og:site_name" content="PHAN MEM THIEN HA">
<meta property="og:url" content="http://phanmemthienha.com/news/huong-dan-phan-mem/su-dung-rsync-dong-bo-thu-muc-tren-linux-va-windows-192.html">
<link rel="shortcut icon" href="http://thienhashop.com/uploads/logo-shop-thien-ha-5.png">
<link rel="canonical" href="http://phanmemthienha.com/news/huong-dan-phan-mem/su-dung-rsync-dong-bo-thu-muc-tren-linux-va-windows-192.html">
<link rel="alternate" href="http://thienhashop.com/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="http://thienhashop.com/news/rss/huong-dan-phan-mem/" title="Tin Tức - Hướng dẫn phần mềm" type="application/rss+xml">
<link rel="alternate" href="http://thienhashop.com/news/rss/huong-dan-noi-bo/" title="Tin Tức - Hướng dẫn nội bộ" type="application/rss+xml">
<link rel="alternate" href="http://thienhashop.com/news/rss/Doi-tac/" title="Tin Tức - Đối tác" type="application/rss+xml">
<link rel="alternate" href="http://thienhashop.com/news/rss/Tuyen-dung/" title="Tin Tức - Tuyển dụng" type="application/rss+xml">
<link rel="preload" as="script" href="http://thienhashop.com/assets/js/jquery/jquery.min.js">
<link rel="preload" as="script" href="http://thienhashop.com/assets/js/language/vi.js">
<link rel="preload" as="script" href="http://thienhashop.com/assets/js/global.js">
<link rel="preload" as="script" href="http://thienhashop.com/themes/default/js/news.js">
<link rel="preload" as="script" href="http://thienhashop.com/themes/popovleather/js/main.js">
<link rel="preload" as="script" href="http://thienhashop.com/themes/popovleather/js/bootstrap.min.js">
<link rel="StyleSheet" href="http://thienhashop.com/assets/css/font-awesome.min.css">
<link rel="StyleSheet" href="http://thienhashop.com/themes/popovleather/css/bootstrap.min.css">
<link rel="StyleSheet" href="http://thienhashop.com/themes/popovleather/css/style.css">
<link rel="StyleSheet" href="http://thienhashop.com/themes/popovleather/css/style.responsive.css">
<link rel="StyleSheet" href="http://thienhashop.com/themes/default/css/news.css">
<link rel="StyleSheet" href="http://thienhashop.com/assets/css/popovleather.vi.0.css?t=87">
<style>
	body{background: #fff;}
</style>
	</head>
	<body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">PHAN MEM THIEN HA</h2>
		<p class="pull-right"><a title="PHAN MEM THIEN HA" href="http://thienhashop.com/">http://thienhashop.com</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Sử dụng Rsync đồng bộ thư mục trên Linux và Windows</h1>
		<ul class="list-inline">
			<li>Thứ sáu - 22/07/2022 21:58</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
			Cài đặt Rsync trên hệ thống Linux CentOS, Ubuntu, macOS thực hiện đồng bộ thư mục giữa local và remote, giữa máy Windows với máy Linux, macOS<br />Rsync cơ bản
		</div>
		<div id="bodytext" class="clearfix">
			<h2>Rsync là gì?</h2>

<p>Rsync (remote sync) là công cụ đồng bộ file, thư mục của Linux. Nó sử dụng thuật toán khi copy dữ liệu sao cho dữ liệu phải copy là nhỏ nhất (chỉ copy những gì thay đổi giữa nguồn và gốc), khi đồng bộ nó giữ nguyên mọi thuộc tính của file, thư mục (từ chủ sở hữu, quyền truy cập file ...). Mặc định hầu hết các bản phân phối Linux có sẵn công cụ này, nếu chưa có thì cài vào:</p>

<pre>
<code class="language-bash"># TRÊN CENTOS/RED HAT
# yum install rsync

# TRÊN UBUNTU
# apt-get install rsync</code></pre>
<strong>Rsync cơ bản</strong>

<pre>
<code class="language-bash"># rsync -a thư_mục_nguồn thư_mục_đích</code></pre>
Video hướng dẫn <a href="https://www.youtube.com/watch?v=rCvsIhg-CFc" target="_blank">tại đây</a><br  />
<br  />
Giả sử trên máy chúng ta có thư mục&nbsp;<code>dir1/</code>&nbsp;trong nó chứa nhiều file, thư mục con. Giờ muốn đồng bộ toàn bộ nội dung thư mục&nbsp;<code>dir1</code>&nbsp;vào một thư mục ở vị trí khác là&nbsp;<code>dir2</code>&nbsp;(2 thư mục này cùng nằm trên một hệ thống - 1 máy), thì thực hiện lệnh sau:

<pre>
<code># rsync -a dir1/ dir2</code></pre>

<p>Tham số&nbsp;<code>-a</code>&nbsp;(Archive) cho biết sẽ đồng bộ tất cả các file, thư mục con trong dir1.</p>

<p>Nếu muốn&nbsp;<code>rsync</code>&nbsp;kiểm tra thông tin&nbsp;<code>dir1</code>,&nbsp;<code>dir2</code>&nbsp;trước khi thi hành thì thêm thiết lập&nbsp;<code>-n</code>, nếu muốn quá trình đồng bộ hiện thị thông tin thêm&nbsp;<code>-v</code></p>

<p>Ví dụ muốn đồng bộ sử dụng cả ba thiết lập trên</p>

<pre>
<code># rsync -anv dir1/ dir2</code></pre>

<h2>Rsync - Đồng bộ giữa 2 máy Linux</h2>

<p>Nếu một máy remote linux(một server, vps nào đó của bạn) có địa chỉ&nbsp;<code>IP</code>, có cho phép bạn kết nối&nbsp;<code>SSH</code>&nbsp;bàng Private Key (Cách kết nối SSH bằng Private Key&nbsp;kết nối SSH bằng Private Key). Ví dụ, cho phép gõ lệnh sau để kết nối SSH</p>

<pre>
<code># Kết nối ssh bằng Private Key đến máy địa chỉ IP, bằng user root
# ssh root@IP</code></pre>

<p>Lúc này trên máy remote có thư mục&nbsp;<code>/home/dir1</code>&nbsp;buốn đồng bộ xuống máy local ở đường dẫn /home/dir1_backup, thì gõ lệnh sau:</p>

<pre>
<code># rsync -anv root@IP:/home/dir1 /home/dir1_backup</code></pre>

<p>Tương tự như vậy, nếu theo chiều ngược lại muốn thư mục ở local đồng bộ lên server (remote) thì thay thư mục nguồn là ở máy local, đích là máy remote</p>

<pre>
<code># rsync -anv /home/dir1_backup root@IP:/home/dir1</code></pre>

<p>Rsync - Đồng bộ giữa Linux - macOS<br  />
&nbsp;</p>

<pre>
<code>#macOS to Linux
rsync --partial -avz path_on_mac root@ip:/path_on_linux

#Linux to macOS
rsync --partial -avz root@ip:/path_on_linux path_on_mac</code></pre>

<h2>Rsync - Vài thiết lập bổ sung</h2>

<ul>
	<li><code>-P</code>&nbsp;hiện thị quá trình đồng bộ (Progress)</li>
	<li><code>-z</code>&nbsp;nén dữ liệu khi truyền tải</li>
	<li><code>--exclude=thư-mục</code>&nbsp;không đồng bộ thư mục nào đó</li>
	<li><code>--no-g</code>&nbsp;không đồng bộ thông tin chủ sở hữu (thiết lập giúp tránh lỗi có thể xảy ra giữa kiểu ổ đĩa không có thuộc tính tương ứng, ví dụ FAT)</li>
	<li><code>--human-readable</code>&nbsp;hiện thị thông tin dễ đọc</li>
	<li><code>--delete</code>&nbsp;xóa file/thư mực ở máy đích nếu ở nguồn không có</li>
</ul>

<p>Ví dụ đồng bộ thư mục&nbsp;<code>root@IP:/home/dir1</code>&nbsp;về thư mục&nbsp;<code>/home/backup</code>&nbsp;của máy local, bên trong thư mục đó có thư mục&nbsp;<code>/home/dir1/dir100</code>&nbsp;sẽ không đồng bộ, quá trình đồng bộ được hiện thị %</p>

<p>&nbsp;</p>

<pre>
<code># rsync -aP root@IP:/home/dir1/ /home/backup --exclude=/home/dir1/dir100</code></pre>

<p>&nbsp;</p>

<pre>
<code>rsync  --human-readable  --partial -avz /path/local remote:/path --delete</code></pre>

<p><br  />
Rsync - Đồng bộ thư mục Windows/Linux<br  />
<br  />
<code>Rsync</code>&nbsp;là công cụ cực kỳ hữu hiệu nếu bạn làm việc giữa các hệ thống Linux, nó không có trên Windows. Nhưng tình huống có thể như sau:</p>

<p>Có một thư mục trên server Linux,&nbsp;<code>/home/dir1</code>, bạn muốn đồng bộ thư mục đó về máy Windows lưu ở thư mục&nbsp;<code>D:\backupdir1</code>&nbsp;hoặc ngược lại, thì làm điều đó như thế nào với&nbsp;<code>Rsync</code>. Có thể cài máy ảo bằng Virtual Box sau đó cài một hệ điều hành Linux như CentOS, Ubuntu, rồi chia sẻ folder máy host Windows và máy ảo Linux, từ máy ảo Linux chạy lệnh Rsync đồng bộ vào thư mục chia sẻ này. Nếu làm cách này quá trình cài đặt mất vài tiếng, và khá phức tạp.<br  />
Cách đơn giản hơn là tạo môi trường chạy lệnh Linux bằng&nbsp;<code>Docker</code><br  />
&nbsp;</p>

<h3>Sử dụng Docker chạy Ubuntu trên Windows</h3>

<p>Nếu chưa có Docker, Vào trang&nbsp;<a href="https://www.docker.com/products/docker-desktop" target="_blank">Docker Destop</a>, tải về và cài đặt nó trên Windows!</p>

<p>Sau khi có&nbsp;<code>Docker</code>&nbsp;ở Windows mở&nbsp;<code>terminate</code>&nbsp;(<code>cmd</code>&nbsp;nhưng khuyến khích dùng&nbsp;<code>PowerShell</code>), chạy các lệnh đề tạo môi trường Linux như sau:</p>

<p>Tải về&nbsp;<code>image</code>&nbsp;Ubuntu (Phiên bản Mini Ubuntu cho Docker)</p>

<pre>
<code>PS &gt;docker pull ubuntu</code></pre>

<p>Kiểm tra xem tên&nbsp;<code>image</code>&nbsp;ubuntu có trong hệ thống Docker chưa</p>

<pre>
<code>PS &gt;docker image ls</code></pre>

<p>Chạy một bản&nbsp;<code>ubuntu</code>&nbsp;(tạo Container cụ thể), hệ điều hành này sẽ thiết cấu hình chia sẻ thư mục Windows&nbsp;<code>D:\backupdir1</code>&nbsp;(nhớ phải có thư mục này trên Windows) và gắn vào vị trí&nbsp;<code>/home/backup1/</code><br  />
&nbsp;</p>

<pre>
<code>PS &gt;docker run -it -v d:/backupdir1/:/home/backup1/ ubuntu</code></pre>

<p>Sau lệnh trên, bạn đã tạo ra một container đang chạy hệ điều hành Ubuntu và đang vào tương tác với nó bằng dòng lệnh. Lúc này từ Ubuntu mọi thay đổi trong thư mục&nbsp;<code>/home/backup1</code>&nbsp;sẽ chính là thư mục&nbsp;<code>d:/backupdir1/</code>&nbsp;của máy host Windows!</p>

<p>Vì Ubutun trên là cơ bản cho Docker, nên cần cài các thành phần theo nhu cầu làm việc</p>

<pre>
<code># Cập nhật
# apt-get update
# Cài SSH cho Ubuntu
# apt install openssh-server
# Cài rsync cho Ubuntu
# apt-get install rsync</code></pre>

<p>Có SSH rồi, cấu hình để truy cập được đến Remote bằng SSH (Xem&nbsp;SSH Private Key)</p>

<p>Giờ chỉa việc Rsync máy server remote với Ubuntu vào thư mục&nbsp;<code>/home/backup1/</code></p>

<pre>
<code># rsync -aP root@IP:/home/dir1/ /home/backup1</code></pre>
Và kết quả Rsync sẽ lưu chính trong thư mục máy host Windows,&nbsp;<code>D:\backupdir1</code>. Bạn cũng có thể Resync từ local lên remote theo chiều ngược lại<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
<br  />
&nbsp;
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="http://thienhashop.com/news/huong-dan-phan-mem/su-dung-rsync-dong-bo-thu-muc-tren-linux-va-windows-192.html" title="Sử dụng Rsync đồng bộ thư mục trên Linux và Windows">http://thienhashop.com/news/huong-dan-phan-mem/su-dung-rsync-dong-bo-thu-muc-tren-linux-va-windows-192.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; PHAN MEM THIEN HA
		</div>
		<div id="contact">
			<a href="mailto:phamhuy842005@gmail.com">phamhuy842005@gmail.com</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="http://thienhashop.com/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
<script src="http://thienhashop.com/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=7,nv_my_abbr="ICT",nv_cookie_prefix="nv4c_y8Y7D",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=1,nv_is_recaptcha=1,nv_recaptcha_sitekey="6LeSarIUAAAAAHRq6xkHwO6J0YYQOdMxgqKTXoL_",nv_recaptcha_type="image",nv_recaptcha_elements=[];</script>
<script src="http://thienhashop.com/assets/js/language/vi.js"></script>
<script src="http://thienhashop.com/assets/js/global.js"></script>
<script src="http://thienhashop.com/themes/default/js/news.js"></script>
<script src="http://thienhashop.com/themes/popovleather/js/main.js"></script>
<script src="http://thienhashop.com/themes/popovleather/js/bootstrap.min.js"></script>
</body>
</html>