Kiến Thức Linux Linux Căn Bản

Thiết lập đồng bộ hóa tập tin Unison giữa hai máy chủ trên Debian

Unison File Synchronization

Hướng dẫn này cho thấy cách thiết lập đồng bộ hóa tập tin giữa hai máy chủ Debian 10 với đồng bộ. Unison là một công cụ đồng bộ hóa tệp tương tự như Rsync, sự khác biệt lớn là nó theo dõi/đồng bộ hóa các thay đổi ở cả hai hướng, tức là các tệp thay đổi trên Server1 sẽ được nhân rộng thành Server2 và ngược lại.

1. Sơ bộ

Trong hướng dẫn này tôi sẽ sử dụng hai máy chủ Debian sau:

  • Server1.example.com với địa chỉ IP 192.168.0.100
  • Server2.example.com với địa chỉ IP 192.168.0.101

Tôi muốn đồng bộ hoá thư mục /var/www giữa hai máy chủ. Tôi sẽ chạy dưới dạng người dùng gốc trong hướng dẫn này để Unison có đủ quyền để đồng bộ hóa người dùng và quyền nhóm.

Tất cả các lệnh trong hướng dẫn này đều chạy như người dùng gốc. Đăng nhập vào cả hai máy chủ trên vỏ dưới dạng gốc và bắt đầu với bước 2 “Cài đặt Unison“.

2. Cài đặt Unison

server1/server2:

Giàn hợp xướng phải được đặt trên server1 và Server2; Vì chúng ta kết nối từ Server1 đến Server2 bằng SSH, chúng ta cũng cần các gói SSH và tôi sẽ cài đặt trình biên tập nano để chỉnh sửa tập tin trên vỏ. Điều này có thể đạt được như sau:

apt-get -y install unison openssh-server ssh nano

3. Tạo một cặp khóa Private/Public trên Server1

server1:

Bây giờ chúng tôi tạo một cặp khoá private/public  trên Trang Server1.example.com:

ssh-keygen -t dsa
root@server1:~# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa): <-- ENTER
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): <-- ENTER
Enter same passphrase again: <-- ENTER
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
ba:82:e1:a1:42:9b:d4:c8:99:c8:bd:8b:7d:4d:d4:66 root@server1
The key's randomart image is:
+---[DSA 1024]----+
| |
| |
| . |
| . E |
|+ * . S |
|.Ooo o |
|ooo+. + |
|oo=... o |
|.. oo.. |
+-----------------+
root@server1:~#

Điều quan trọng là bạn không đi vào mật khẩu nếu không gương sẽ không hoạt động mà không có tương tác của con người nên chỉ cần nhấn enter!

Tiếp theo, chúng tôi sao chép khoá công khai của mình đến Trang Server2.example.com:

ssh-copy-id -i $HOME/.ssh/id_dsa.pub [email protected]
ssh-copy-id -i $HOME/.ssh/id_dsa.pub [email protected]
The authenticity of host '192.168.1.102 (192.168.0.101)' can't be established.
ECDSA key fingerprint is 2b:3c:35:ad:3d:e2:fc:16:2f:55:5c:e1:2c:d7:3d:a9.
Are you sure you want to continue connecting (yes/no)? <-- yes (you will see this only if this is the first time you connect to server2)
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: <-- server2 root password
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

Bây giờ kiểm tra Server2 xem khoá công khai của Server1 đã được chuyển đúng chưa.

server2:

cat $HOME/.ssh/authorized_keys
root@server2:/home/administrator# cat $HOME/.ssh/authorized_keys
ssh-dss AAAAB3NzaC1kc3MAAACBAKHLdAztIr8muZIlQYuE/4f75kmgTwWqJRZJ1dTqHDnHWsy48emDU8v85hxAPg43k9aF7/zAwpA0MNNNk5T9Tx/DyUkK/KcyVP2f4p8tvovrkUvoxsZACkTUmFqKdq2x6/AGfjsCRmkpLhZuad7r5rKEXHRh8KYGHqD1Id8wcpy5AAAAFQCww3OekKcKMshMAwBK3XQmmYEGUwAAAIEAgjztlwh8OFYxwQve/RrhI2sceCXwS/yjQyH7q0zdWB9Fr4s/16T2PLBT+7M3vb+JlPDO3JRqgaYbel1kS2F2iKrY0EX0FI3/9fVDfWoz3mhCscPLriqy5AcsHitxQNfiZgA5wDiSjWpk1v+FbIC+VuqbKdQuE4MBKj19N9YALIUAAACABQ4NDsa2UBc8jsxvghjoLhUWF7HChaCksXQcL6i98VNRcemtPC6wpIri75iR4Uhv1666bDOBAdmIBX9Qf7A/+czPKPaj4CGI1hVy1pgYMa3btnEvoSnH/ONtjpOz9q+3up1OOOn+5fud7xjJn+Fq8WoGROgarBpCbQU3w2GUUnM= root@server1

4. Thực thi Unison

server1:

Bây giờ chúng ta có thể đồng bộ hóa thư mục /var/www trên cả hai máy chủ. Trên Kênh Server1 Chạy:

unison /var/www ssh://192.168.0.101//var/www

Đầu ra sẽ tương tự như cái này – bạn có thể phải trả lời một vài câu hỏi vì đây là lần đầu tiên đồng thanh đang chạy:

root@server1:/var/www# unison /var/www ssh://192.168.0.101//var/www
Contacting server...
Connected [//server1//var/www -> //server2//var/www]
Looking for changes
Warning: No archive files were found for these roots, whose canonical names are:
/var/www//server2//var/www
This can happen either because this is the first time you have synchronized these roots,
or because you have upgraded Unison to a new version with a different
archive format.Update detection may take a while on this run if the replicas are
large.Unison will assume that the 'last synchronized state' of both replicas
was completely empty. This means that any files that are different
will be reported as conflicts, and any files that exist only on one
replica will be judged as new and propagated to the other replica.
If the two replicas are identical, then no changes will be reported.If you see this message repeatedly, it may be because one of your machines
is getting its address from DHCP, which is causing its host name to change
between synchronizations. See the documentation for the UNISONLOCALHOSTNAME
environment variable for advice on how to correct this.
Donations to the Unison project are gratefully accepted:
http://www.cis.upenn.edu/~bcpierce/unison
Press return to continue.[<spc>] <-- Press Enter
Waiting for changes from server
Reconciling changeslocal server2
dir ----> example.com [f] <-- Press Enter
dir ----> example.de [f] <-- Press EnterProceed with propagating updates? [] <-- Enter "y"
Propagating updates
UNISON 2.48.4 started propagating changes at 13:24:01.10 on 05 May 2020
[BGN] Copying example.com from /var/www to //server2//var/www
[BGN] Copying example.de from /var/www to //server2//var/www
Shortcut: copied /var/www/example.de/web/index.html from local file /var/www/.unison.example.com.d3783bddaaf59b9ba4d2ed0433f9db63.unison.tmp/web/index.html
[END] Copying example.de
[END] Copying example.com
UNISON 2.48.4 finished propagating changes at 13:24:01.98 on 05 May 2020
Saving synchronizer state
Synchronization complete at 13:24:01 (2 items transferred, 0 skipped, 0 failed)

Kiểm tra danh mục /var/www trên server1 và server2 bây giờ, và bạn nên thấy rằng chúng đã đồng bộ.

Tất nhiên, chúng ta không muốn cùng chạy đồng thời, do đó chúng ta có thể tạo ra một file preferences (/root/.unison/default.prf) chứa tất cả các cài đặt mà nếu không sẽ phải chỉ định trên dòng lệnh:

nano /root/.unison/default.prf
# Roots of the synchronization
root = /var/www
root = ssh://192.168.0.101//var/www

# Paths to synchronize
#path = current
#path = common
#path = .netscape/bookmarks.html

# Some regexps specifying names and paths to ignore
#ignore = Path stats    ## ignores /var/www/stats
#ignore = Path stats/*  ## ignores /var/www/stats/*
#ignore = Path */stats  ## ignores /var/www/somedir/stats, but not /var/www/a/b/c/stats
#ignore = Name *stats   ## ignores all files/directories that end with "stats"
#ignore = Name stats*   ## ignores all files/directories that begin with "stats"
#ignore = Name *.tmp    ## ignores all files with the extension .tmp

#          When set to true, this flag causes the user interface to skip
#          asking for confirmations on non-conflicting changes. (More
#          precisely, when the user interface is done setting the
#          propagation direction for one entry and is about to move to the
#          next, it will skip over all non-conflicting entries and go
#          directly to the next conflict.)
auto=true

#          When this is set to true, the user interface will ask no
#          questions at all. Non-conflicting changes will be propagated;
#          conflicts will be skipped.
batch=true

#          !When this is set to true, Unison will request an extra
#          confirmation if it appears that the entire replica has been
#          deleted, before propagating the change. If the batch flag is
#          also set, synchronization will be aborted. When the path
#          preference is used, the same confirmation will be requested for
#          top-level paths. (At the moment, this flag only affects the
#          text user interface.) See also the mountpoint preference.
confirmbigdel=true

#          When this preference is set to true, Unison will use the
#          modification time and length of a file as a `pseudo inode
#          number' when scanning replicas for updates, instead of reading
#          the full contents of every file. Under Windows, this may cause
#          Unison to miss propagating an update if the modification time
#          and length of the file are both unchanged by the update.
#          However, Unison will never overwrite such an update with a
#          change from the other replica, since it always does a safe
#          check for updates just before propagating a change. Thus, it is
#          reasonable to use this switch under Windows most of the time
#          and occasionally run Unison once with fastcheck set to false,
#          if you are worried that Unison may have overlooked an update.
#          The default value of the preference is auto, which causes
#          Unison to use fast checking on Unix replicas (where it is safe)
#          and slow checking on Windows replicas. For backward
#          compatibility, yes, no, and default can be used in place of
#          true, false, and auto. See the section "Fast Checking" for more
#          information.
fastcheck=true

#          When this flag is set to true, the group attributes of the
#          files are synchronized. Whether the group names or the group
#          identifiers are synchronizeddepends on the preference numerids.
group=true

#          When this flag is set to true, the owner attributes of the
#          files are synchronized. Whether the owner names or the owner
#          identifiers are synchronizeddepends on the preference
#          extttnumerids.
owner=true

#          Including the preference -prefer root causes Unison always to
#          resolve conflicts in favor of root, rather than asking for
#          guidance from the user. (The syntax of root is the same as for
#          the root preference, plus the special values newer and older.)
#          This preference is overridden by the preferpartial preference.
#          This preference should be used only if you are sure you know
#          what you are doing!
prefer=newer

#          When this preference is set to true, the textual user interface
#          will print nothing at all, except in the case of errors.
#          Setting silent to true automatically sets the batch preference
#          to true.
silent=true

#          When this flag is set to true, file modification times (but not
#          directory modtimes) are propagated.
times=true

Các bình luận nên làm cho tập tin tự giải thích, ngoại trừ các hướng dẫn. Nếu bạn không xác định các chỉ thị đường đi, thì các thư mục trong các chỉ thị gốc sẽ được đồng bộ hóa. Nếu bạn chỉ định các chỉ dẫn đường đi thì các đường đi có liên quan đến đường đi gốc (ví dụ như root = /var/www và path = current dịch ra /var/www/current), và chỉ những thư mục con này mới được đồng bộ hóa, chứ không phải toàn bộ thư mục được chỉ định trong chỉ thị gốc.

Bạn có thể tìm hiểu thêm về những lựa chọn có sẵn bằng cách xem trang chủ của Nhóm Unison:

man unison

Bây giờ chúng ta đã đặt tất cả các cài đặt vào một tập tin preferences (đặc biệt là các hướng dẫn root (và lựa chọn path)), chúng ta có thể chạy không có bất kỳ đối số nào:

unison

5. Tạo Cron Job cho Unison

server1:

Chúng ta muốn tự động hóa đồng bộ hóa, đó là lý do tại sao chúng ta tạo công việc cron cho nó trên Server1.example.com:

crontab -e
*/5 * * * * /usr/bin/unison &> /dev/null

Đoạn phim này cứ 5 phút lại đồng thanh. Hãy điều chỉnh nó theo nhu cầu của bạn ( man 5 Crontab). Tôi sử dụng con đường để hợp nhất ở đây (/usr/bin/unison ) chỉ để chắc rằng Cron biết nơi để tìm hợp âm. Vị trí hợp xướng của bạn có thể khác nhau. Thực thi

which unison

6. Kiểm tra Unison

Bây giờ tôi sẽ kiểm tra sự đồng bộ hóa hai chiều của hợp âm để xem thiết lập có hoạt động hoàn toàn hay không.

Chạy lệnh sau trên Server1 để tạo file kiểm thử với nội dung “Kiểm thử 1” :

Server1

echo "Test 1" > /var/www/test.txt

Bây giờ chờ ít nhất 5 phút (như chúng tôi tạo ra một máy thử giọng chạy một lần mỗi 5 phút). Rồi chạy trên Server2:

cat /var/www/test.txt

Để hiển thị nội dung của file test.txt trên màn hình. Đầu ra phải tương tự như ảnh chụp màn hình này.

Bây giờ chạy lệnh này trên Server2 để cập nhật nội dung của tập tin kiểm thử của chúng ta thành “Kiểm thử 2” :

Server2

echo "Test 2" > /var/www/test.txt

Và chờ ít nhất 5 phút. Sau đó điều hành lệnh mèo trên Server1:

Server1

cat /var/www/test.txt

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !