[ Updated, 29 Juni 2015 ]
Di post ini saya akan menjelaskan cara mengunakan Rsync untuk mensinkronisasi 2 buah file di antara 2 server ubuntu, cara ini juga bisa diterapkan antara debian dengan debian atau antara ubuntu dengan debian. Rsync dengan bantuan rsa key, kenapa ? agar setelahnya proses rsync akan bisa berjalan dengan baik tanpa masalah dan aman.
Di sini saya mengunakan 2 komputer server ubuntu, proxy1 dan proxy2, dengan sistem operasi versi 14.04 32-bit. Kedua server ini masing-masing memiliki ip address yang berbeda segmen, proxy1 192.168.20.1 dan proxy2 192.168.80.1.
N.B. Apabila tidak mengunakan user root maka gunakan sudo untuk user sudoer.
Langkah-langkah rsync antara 2 server ubuntu adalah sebagai berikut :
1. Memeriksa konfigurasi sshd_config di masing server ubuntu, proxy1 dan proxy2.Proxy1root@proxy1: cat /etc/ssh/sshd_config |egrep "RSA|Pubkey|Authorized" |grep -v "#"
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
Jika tidak sama seperti hasil diatas maka di konfigurasi dahulu.
root@proxy1: nano /etc/ssh/sshd_config
Cari baris syntax diatas lalu di uncomment, simpan dan restart ssh.
root@proxy1: service ssh restart
Kemudian ulangi command diatas,
root@proxy1: cat /etc/ssh/sshd_config |egrep "RSA|Pubkey|Authorized" |grep -v "#"
Seharusnya hasilnya sudah sama.
Proxy2root@proxy2: cat /etc/ssh/sshd_config |egrep "RSA|Pubkey|Authorized" |grep -v "#"
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
2. Membuat rsa key dengan ssh-keygen di proxy1 dan di proxy2.Proxy1root@proxy1:~# nano /etc/ssh/sshd_config
root@proxy1:~# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
51:68:ec:f4:9f:c3:af:01:1b:2b:98:d9:04:99:77:68 root@proxy1
The key's randomart image is:
+--[ RSA 2048]----+
| . .. |
| o=o |
| ++E.. |
| +.o. |
| S oo . |
| * == |
| + o o .o |
| . .. |
| .. |
+-----------------+
Memeriksa keberadaan file rsa yang sudah dibuat dengan ssh-keygen di proxy1.
root@proxy1:~# cd .ssh/
root@proxy1:~/.ssh# ls -lrt
total 8
-rw-r--r-- 1 root root 393 Jun 26 13:13 id_rsa.pub
-rw------- 1 root root 1675 Jun 26 13:13 id_rsa
Proxy2root@proxy2:~# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
96:47:b5:05:ad:43:c1:21:71:b8:ea:95:ae:9b:1f:67 root@proxy2
The key's randomart image is:
+--[ RSA 2048]----+
| o+*=. |
| .+oo. |
| .o.. |
| o. o |
| S... . |
| ...o |
| . o. E |
| ...+ |
| ++. |
+-----------------+
Memeriksa keberadaan file rsa yang sudah dibuat dengan ssh-keygen di proxy2.
root@proxy2:~# cd .ssh/
root@proxy2:~/.ssh# ls -lrt
total 8
-rw-r--r-- 1 root root 393 Jun 26 13:16 id_rsa.pub
-rw------- 1 root root 1679 Jun 26 13:16 id_rsa
3. Upload/transfer file rsa key dari proxy1 ke proxy2.
Karena saya tidak mengunakan port default [berbeda], maka dengan command,
root@proxy1:~/.ssh# scp -P 213 -r id_rsa.pub 192.168.80.1:/root/.ssh/authorized_keys
The authenticity of host '[192.168.80.1]:213 ([192.168.80.1]:213)' can't be established.
ECDSA key fingerprint is 0a:9d:c8:ad:20:15:62:11:87:ad:b3:2a:95:e2:c8:60.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.80.1]:213' (ECDSA) to the list of known hosts.
root@192.168.80.1's password:
id_rsa.pub 100% 393 0.4KB/s 00:00
4. Upload/transfer file rsa key dari proxy2 ke proxy1.
root@proxy2:~/.ssh# scp -P 212 -r id_rsa.pub 192.168.20.1:/root/.ssh/authorized_keys
The authenticity of host '[192.168.20.1]:212 ([192.168.20.1]:212)' can't be established.
ECDSA key fingerprint is bc:8d:a3:0c:1a:a4:21:ca:39:24:f2:73:5a:1f:0b:5f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.20.1]:212' (ECDSA) to the list of known hosts.
Password:
id_rsa.pub 100% 393 0.4KB/s 00:00
5. Tes koneksi ssh antar server ubuntu.
Proxy1Karena mengunakan rsa key dan buka mengunakan port default [berbeda], dengan command,
root@proxy1: ssh 192.168.80.1 -p 213
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-53-generic i686)
* Documentation: https://help.ubuntu.com/
System information as of Fri Jun 26 13:28:19 WIB 2015
System load: 0.0 Processes: 136
Usage of /: 1.7% of 450.52GB Users logged in: 1
Memory usage: 5% IP address for p130p1: 192.168.80.1
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
Last login: Fri Jun 26 13:15:17 2015 from 10.7.70.82
root@proxy2:~#
Sukses!Proxy2root@proxy2: ssh 192.168.20.1 -p 212
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.8.0-29-generic i686)
* Documentation: https://help.ubuntu.com/
System information as of Fri Jun 26 13:29:21 WIB 2015
System load: 0.02 Processes: 134
Usage of /: 24.0% of 224.94GB Users logged in: 1
Memory usage: 4% IP address for eth0: 192.168.20.1
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
Last login: Fri Jun 26 13:09:27 2015 from 10.7.70.82
root@proxy1:~#
Sukses Juga!6. Ujicoba sikronisasi antar server, proxy 1 dan proxy2.
Di sini saya akan mengunakan Rsync untuk mensinkronisasi 2 file yaitu file realname.cfg dari direktori lightsquid antara 2 server ubuntu, proxy1 dan proxy2. Bisa juga mensinkronisasi dua folder.
Dan file realname.cfg lightsquid di Proxy1 sebagai sumber [source], sedangkan file realname.cfg di Proxy2 sebagai destination [tujuan].
root@proxy2: rsync -av -e "ssh -p 223" 192.168.20.1:/var/www/lightsquid/realname.cfg /var/www/lightsquid/realname.cfg
receiving incremental file list
realname.cfg
sent 223 bytes received 12,048 bytes 24,542.00 bytes/sec
total size is 19,548 speedup is 1.59
Untuk memeriksa hasil sinkronisasinya, lihat perubahan
tanggal dan
waktu pada file tujuan yaitu realname.cfg, dengan,
root@proxy2: ls -lah /var/www/lightsquid
7. Membuat Bash Script.
Dari command Rsync diatas, maka untuk mempermudahnya dibuatkan saja bash script yang akan dijalankan secara otomatis melalui cronjob.
Buat file bash script.
Misalnya dibuat bash script rsync di proxy2.
root@proxy2: mkdir /rsync-lightsquid
root@proxy2: nano /rsync-lightsquid/realname.sh
Isinya,
#!/bin/bash
dest=/var/www/lightsquid/`date +%A` # direktori tujuan
rsync -av -e "ssh -p 212" 192.168.20.1:/var/www/lightsquid/realname.cfg /var/www/lightsquid/realname.cfg
Beri hak akses,
root@proxy2: chmod 700 /rsync-lightsquid/realname.sh
Trial bash script realname.sh.root@proxy2: cd /rsync-lightsquid
root@proxy2: ./realname.sh
receiving incremental file list
realname.cfg
sent 217 bytes received 228 bytes 296.67 bytes/sec
total size is 20,036 speedup is 45.02
Ok.
Sukses.
8. Menjalankan bash script realname.sh via cronjob.
root@proxy2:crontab -e
# m h dom mon dow command
0 11 * * * /var/www/lightsquid/lightparser.pl today
30 12 * * 1,2,3,4,5,6 /bin/bash /rsync-lightsquid/realname.sh > /dev/null 2>&1
Tips. Bisa ditambahkan fitur pengiriman email pada cronjob agar ada laporan langsung via email, untuk lebih jelasnya silahkan baca artikel selnjutnya yaitu
Mengirimkan File Hasil Sinkronisasi [Rsync] Mengunakan Mpack Di Server Ubuntu/Debian.
Selesai.
Selamat Mencoba!.