INSTALL & RUN & SYNC UNISON ON CENTOS 7
PART1
INSTALL UNISON MANUALLY (CENTOS 7 CAN’T PULL FROM YUM)
https://www.digitalocean.com/community/questions/install-unison-in-centos-7
Prerequisites
$ sudo yum install ocaml ocaml-camlp4-devel ctags ctags-etags
first make sure you have wget
$ sudo yum install wget
check out latest release
type browser —> http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/
download unison tar file
$ sudo wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.3.tar.gz
extract unison tar
$ sudo tar xvfz unison-2.48.3.tar.gz
enter unison directory
$ cd unison-2.48.3
build unison
$ sudo make
copy to sbin
$ sudo cp -v unison /usr/local/sbin/
‘unison’ -> ‘/usr/local/sbin/unison’
copy to bin
$ sudo cp -v unison /usr/bin
‘unison’ -> ‘/usr/bin/unison’
go to the upper folder and delete all unison folders and files that we have downloaded
$ cd ~ $ sudo rm -rf unison*
TEST UNISON LOCALLY
http://www.cyberciti.biz/faq/unison-file-synchronizer-tool/
make test1, test2 dirs under temp and create file1, file2 and file3 under test1 folder
$ sudo mkdir /tmp/test{1,2}
$ cd /tmp/test1
$ sudo touch file{1,2,3}
We have 3 files
$ ls -l
-rw-r--r--. 1 root root 0 Jul 16 09:14 file1 -rw-r--r--. 1 root root 0 Jul 16 09:14 file2 -rw-r--r--. 1 root root 0 Jul 16 09:14 file3
also see that there is nothing under test2 folder
$ ls -l /tmp/test2
now sync test1 and test2 folders
$ sudo unison /tmp/test1 /tmp/test2
it will ask questions, check out now test2 folder
$ ls -l /tmp/test2
to avoid questions, let’s run it in batch mode, first do some modifications
(for file2 type foo or whatever, save&exit)
$ sudo rm /tmp/test1/file3
$ sudo vi /tmp/test2/file2
let’s unison again but now in batch mode to avoid questions
$ sudo unison -batch /tmp/test1 /tmp/test2
PART2
TEST UNISON REMOTELY
We want to sync moodledata folder from web1 client and to web2 server host (10.1.1.11)
On web1, create new file test.txt, type hello world or whatever save&exit
[msen@web1 ~]$ sudo vi /var/moodledata/test.txt
let’s test that connections to connect remote server, enter password afterwards
[msen@web1 ~]$ unison -testServer /var/moodledata/ ssh://10.1.1.11//var/moodledata
msen@10.1.1.11's password: Connected [//web1//var/moodledata -> //web2//var/moodledata]
Now let’s sync test.txt file, actually it will sync all files under /var/moodledata to the remote server
[msen@web1 ~]$ unison -batch /var/moodledata ssh://10.1.1.11//var/moodledata
new file ----> test.txt local : new file modified on 2015-07-27 at 9:13:42 size 13 rw-r--r-- web2 : absent Propagating updates UNISON 2.48.3 started propagating changes at 09:25:30.52 on 27 Jul 2015 [BGN] Copying test.txt from /var/moodledata to //web2//var/moodledata [END] Copying test.txt UNISON 2.48.3 finished propagating changes at 09:25:30.61 on 27 Jul 2015 Saving synchronizer state Synchronization complete at 09:25:30 (1 item transferred, 0 skipped, 0 failed)
let’s delete text.txt from web2 remote server
[msen@web2 ~]$ sudo rm /var/moodledata/test2.txt
go to now web1 client server and re-run the batch
[msen@web1 ~]$ unison -batch /var/moodledata ssh://10.1.1.11//var/moodledata
Contacting server... msen@10.1.1.11's password: Connected [//web1//var/moodledata -> //web2//var/moodledata] Looking for changes Waiting for changes from server Reconciling changes local : unchanged file modified on 2015-07-27 at 7:33:51 size 13 rw-r--r-- web2 : deleted Propagating updates UNISON 2.48.3 started propagating changes at 09:34:22.28 on 27 Jul 2015 [BGN] Deleting test.txt from /var/moodledata [END] Deleting test.txt UNISON 2.48.3 finished propagating changes at 09:34:22.29 on 27 Jul 2015 Saving synchronizer state Synchronization complete at 09:34:22 (1 item transferred, 0 skipped, 0 failed)
check it out on web1 server if it’s deleted, you should not be seen that file
[msen@web1 ~]$ ls -l /var/moodledata/
PART3
TO BYPASS SSH PASSWORD SSH WITH DSA PUBLIC KEY AUTHENTICATION – PASSWORD LESS LOGIN
client: web1
server: web2 (10.1.1.11)
go to top most folder
$ cd ~
On web1, generate dosa key pair, click enter 3 times
[msen@web1 ~]$ ssh-keygen -t dsa
Generating public/private dsa key pair. Enter file in which to save the key (/home/msen/.ssh/id_dsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/msen/.ssh/id_dsa. Your public key has been saved in /home/msen/.ssh/id_dsa.pub. The key fingerprint is: 07:51:af:d9:64:61:50:67:c8:08:3a:4f:3f:d7:00:6e msen@web1 The key's randomart image is: +--[ DSA 1024]----+ | oo+=+.o | | . o.+o+ | | o o E = | | + + * o | | S * o . | | . o | | | | | | | +-----------------+ Your key is saved under /home/msen/.ssh/id_dsa
Now give permission (~/.ssh points to the same path /home/msen/.ssh)
[msen@web1 ~]$ sudo chmod 755 /home/msen/.ssh
or since we are in ~
[msen@web1 ~] $ sudo chmod 755 .ssh
repeat the same step for the web2 server
[msen@web2 ~]$ ssh-keygen -t dsa
[msen@web2 ~]$ sudo chmod 755 /home/msen/.ssh
for both web1&web2, enter local ips like this
[msen@web2 ~]$ sudo vi /etc/hosts
10.10.0.77 web1 10.1.1.11 web2
let’s see if they ping each other
[msen@web1 ~]$ ping web2
[msen@web2 ~]$ ping web1
on web1 copy public key file ~/.ssh/id_dsa.pub on web1 to remote server web2 as ~/.ssh/authorized_keys
[msen@web1 ~]$ scp ~/.ssh/id_dsa.pub msen@web2:.ssh/authorized_keys
on web2 set the permission for authorized_keys
[msen@web2 ~]$ sudo chmod 600 ~/.ssh/authorized_keys
LET’S TEST LESS LOGIN
let’s test remote ssh on web1 and see if requires password, exit and go back after connecting web2
[msen@web1 ~]$ ssh msen@web2
[msen@web2 ~]$ exit;
if remote server has a public domain
$ ssh msen@remote-server.com
let’s test remote server by sending a file to
[msen@web1 ~]$ sudo touch 123.txt
now send it to remote server, it won’t ask any password
[msen@web1 ~]$ scp 123.txt msen@web2:/tmp
123.txt 100% 0 0.0KB/s 00:00
check it out on web2
[msen@web2 ~]$ ls -l /tmp/
-rw-r--r--. 1 msen msen 0 Jul 29 11:52 123.txt
now you can delete 123.txt on both servers
[msen@web1 ~]$ sudo rm 123.txt
[msen@web2 ~]$ sudo rm /tmp/123.txt
YAYYYY!!!
PART4
SETUP SHELL SCRIPT FOR UNISON
[msen@web1 ~]$ sudo chmod 777 -R /opt
[msen@web1 ~]$ sudo vi /opt/sync.moodle.sh
copy paste this:
#!/bin/bash # set paths / dirs #_paths="/var/www/html/moodle/ \ #/var/moodledata/" #sync only moodledata for now _paths="/var/moodledata " # binary file name _unison=/usr/bin/unison # server names # sync server1.cyberciti.com with rest of the server in cluster # i.e _rserver="server2.cyberciti.com server3.cyberciti.com" _rserver="msen@web2" # sync it for r in ${_rserver} do for p in ${_paths} do ${_unison} -batch "${p}" "ssh://${r}/${p}" done done
setup settings for unison default preference file
[msen@web1 ~]$ sudo vi .unison/default.prf
change confirmbigdel to false, so it won’t ask when one part is deleted
confirmbigdel=false
be careful not to delete anything on web2, set it back to true if you think it’s too risky
copy paste this to default.prf
# Unison preferences file # 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=false # 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
create a cronjob that runs every 1 minute
[msen@web1 ~]$ crontab -e
type this, it runs every 3 minutes:
*/3 * * * * /opt/sync.moodle.sh &>/tmp/sync.moodle.sh.log
now restart crond
[msen@web1 ~]$ sudo systemctl crond restart
create a new file under web1
[msen@web1 ~]$ sudo touch /var/moodledata/testing123.txt
check it out from web2 after 3 minutes
[msen@web2 ~]$ ls /var/moodledata/
You should see the file!
YAAAAYYYY!
First of all unison are available in yum.
and a few paths her are wrong. but overall a good introduction to unison! thank you.
LikeLike