This is G o o g l e's cache of http://www.debianclub.com/node/12 as retrieved on 17 Feb 2007 14:53:13 GMT.
G o o g l e's cache is the snapshot that we took of the page as we crawled the web.
The page may have changed since that time. Click here for the current page without highlighting.
This cached page may reference images which are no longer available. Click here for the cached text only.
To link to or bookmark this page, use the following url: http://www.google.com/search?hl=en&q=cache%3Ahttp%3A%2F%2Fwww.debianclub.com%2Fnode%2F12&btnG=Search


Google is neither affiliated with the authors of this page nor responsible for its content.

User login

Links

debianclub.com
debian.org
ubuntuclub.com
blognone.com

Syndicate

Syndicate content

ทำ Mirror ด้วย rsync แบบง่าย

เอามาจาก ThaiLinuxCafe: ทำ mirror ด้วย rsync อย่างง่าย

ต้องการ syncronize ไดเรคทอรี่ /var/www ของเครื่อง server1.example.com
มายังไดเรคทอรี่ /var/www ของเครื่อง mirror.example.com
ซึ่งมีข้อดีกว่าการคัดลอกธรรมดาด้วย cp หรือ scp ตรงที่
ถ้าเราลบไฟล์ใน server1 ไฟล์ใน mirror จะถูกลบตามไปด้วย
ทำให้ทั้งสองเครื่องมีความสดใหม่เหมือนกัน

ทำที่ทั้งสองเครื่อง server1.example.com และ mirror.example.com
ติดตั้งและปรับแต่ง rsync
# aptitude install rsync
# vi /etc/default/rsync

RSYNC_ENABLE=true

# dpkg-reconfigure rsync

ทำที่เครื่อง server1.example.com
เพิ่มผู้ใช้ชื่อ someuser
server1:# useradd -m -s /bin/bash someuser
( -m คือให้คัดลอกรูปแบบจากไดเรคทอรี่ /etc/skel/
-s คือให้ใช้เชลล์คือ /bin/bash )
ทำที่เครื่อง mirror.example.com
ทำการคัดลอก
mirror:# rsync -avz -e ssh someuser@server1.example.com:/var/www/ /var/www/
<<<--- ใส่ค่า password ของ someuser

rsync จะทำการคัดลอกไฟล์ทั้งหมดใน /var/www ที่ someuser มีสิทธิในการอ่านทั้งหมด
มาที่ /var/www ของเครื่อง mirror

หมายเหตุ
ตรวจสอบเนื้อที่การใช้ไฟล์ด้วยคำสั่ง
# cd /var/www
# du -c | grep total

ทดสอบกับ debian etch

ผมก็ใช้เห

ผมก็ใช้เหมือนกัน :-)

ทำให้ rsync ไม่ต้องถามรหัสผ่าน

ต่อไปเป็นการทำให้ rsync ไม่ต้องถามรหัสผ่าน โดยการใช้ public key

ทำที่ mirror.example.com
สร้าง public key เพื่อให้ไม่ต้องใส่รหัสผ่าน
mirror:# ssh-keygen -t dsa -b 1024
Enter file in which to save the key (/root/.ssh/id_dsa): <<<--- [Enter]
Enter passphrase (empty for no passphrase): <<<---[Enter]
Enter same passphrase again: <<<---[Enter]

คัดลอก key ไปยัง server1
mirror:# scp ~/.ssh/id_dsa.pub someuser@server1.example.com:/home/someuser/
Password: <<<--- ใส่ค่ารหัสผ่านของ someuser ( บน server1 )

ssh ไปยัง server1
mirror:# ssh server1.example.com -l someuser
Are you sure you want to continue connecting (yes/no)? <<<--- yes
Password: <<<--- ใส่ค่ารหัสผ่านของ someuser

ทำให้ key ที่สร้างไว้ ถูกใช้จาก rsync
someuser@server1:~$ mkdir ~/.ssh
someuser@server1:~$ chmod 700 ~/.ssh
someuser@server1:~$ mv id_dsa.pub ~/.ssh
someuser@server1:~$ cd ~/.ssh
someuser@server1:~/.ssh$ touch authorized_keys
someuser@server1:~/.ssh$ chmod 600 authorized_keys
someuser@server1:~/.ssh$ cat id_dsa.pub >> authorized_keys
someuser@server1:~/.ssh$ exit

ลบ known_hosts เก่าทิ้งก่อน
mirror:# rm .ssh/known_hosts

ทดสอบครั้งแรก
mirror:# rsync -avz --delete -e "ssh -i /root/.ssh/id_dsa" \
someuser@server1.example.com:/var/www/ /var/www/
Are you sure you want to continue connecting (yes/no)? <<<--- yes

เรียบร้อย ครั้งต่อไปเขาจะไม่ถามอีกแล้ว
เวลาจะใช้งาน ก็ใช้คำสั่งเดิม
mirror:# rsync -avz --delete -e "ssh -i /root/.ssh/id_dsa" \
someuser@server1.example.com:/var/www/ /var/www/

จบแล้วครับ

(ตรงนี้ถ้าเขียนเป็น Book ได้ ผมว่าจะดูสวยงามกว่าทำเป็นคอมเมนต์น่ะครับ)
(update: แก้ dsa เป็น 1024บิต - แก้แล้ว)

rsync อัตโนมัติผ่าน cron

ต่อไปเป็นการให้ rsync ทำงานอัตโนมัติผ่าน cron

ทำที่เครื่อง mirror.example.com
แก้ไข crontab
# crontab -e

...
0 0 * * * /usr/bin/rsync -azq --delete -e "ssh -i /root/.ssh/id_dsa" someuser@server1.example.com:/var/www/ /var/www/
...

( พิมพ์ต่อกันเป็นบรรทัดเดียวกันนะครับ - ให้ทำการ sync ตอนเที่ยงคืน ทุกวัน )

จบแล้วครับ