Backup ISPConfig with Rsync to Repository

โดยปกติบน ISPConfig จะมีให้เราสามารถ Backup ข้อมูลของแต่ละเว็บพร้อมฐานข้อมูล และเรายังสามารถ Backup ของ VM อีกด้วย ซึ่งถ้าหากเว็บไซต์เราพังขึ้นมา แล้วเกิดซวย 2 เด้ง ไฟล์ Backup ดันหายด้วย เหลือทางเลือกเดียวนั่นคือเราต้องทำการ Recovery VM มันจะส่งผลกระทบทุกเว็บไซต์ที่อยู่บน ISPConfig โดนย้อนข้อมูลกลับไป เพราะเว็บไซต์เดียว จึงเป็นที่มาของการทำ Repository เอาไว้เก็บไฟล์และฐานข้อมูลของทุกเว็บไซต์ แยกจาก ISPConfig อีกทีนึง


Configuration

  • ติดตั้ง Package บนเครื่องที่ใช้เป็น Backup Repository
# apt-get install rsync -y
  • ลองทดสอบ SSH ว่าสามารถ Connect เชื่อมต่อกันได้จริง จะมีการถามหา Password ถ้า Connect ไม่ได้อาจติด Firewall
# ssh root@lab-isp.lab.local
  • สร้าง SSH Keygen ขึ้นมาเพื่อจะได้ไม่ต้องใส่ Password ตอนทำ rsync ซึ่งจะ Remote ผ่าน SSH
# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
SHA256:uZqAkicIhfOowfguiwUpdKHLo4Qt8d1ENQIb0nz7jGU root@lab-rsync.lab.local
The key's randomart image is:
+---[RSA 2048]----+
|  .+o.o.o        |
| ...++.. .       |
|+o......         |
|*Xo. o. E.       |
|@*+ . .*S        |
|B*.. . o.        |
|B = . .          |
|o* . o           |
|oo. o            |
+----[SHA256]-----+
  • ลองเข้าไปดู SSH Public Key ที่เราได้สร้างขึ้น
# cd .ssh
# ls -lrt
-rw-r--r-- 1 root root 396 Jul 4 15:54 id_rsa.pub
-rw------- 1 root root 1679 Jul 4 15:54 id_rsa
-rw-r--r-- 1 root root 666 Jul 6 08:54 known_hosts
  • แก้ไข SSH Permission บนเครื่องปลายทางที่จะ Remote เพื่อให้สามารถ SSH ด้วย root
# vi /etc/ssh/sshd_config
PermitRootLogin yes
  •  เมื่อแก้ไขเสร็จแล้วให้ Restart Service
# service sshd restart
  • ทำการ Copy SSH Public Key ไปยังเครื่องที่เราต้องการจะ Remote ผ่าน SSH
# ssh-copy-id -i ~/.ssh/id_rsa.pub lab-isp.lab.local
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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
root@lab-isp.lab.local's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'lab-isp.lab.local'"
and check to make sure that only the key(s) you wanted were added.
  • สร้างโฟลเดอร์ไว้เก็บไฟล์ Script
# mkdir script
# cd script
# vi backup_isp.sh
  • จากนั้นเขียน Shell Script ให้ sync ไปที่ /var/backup ตามด้วยโฟลเดอร์ของแต่ละ Host
#!/bin/bash

# lab-isp.lab.local
rsync -av root@lab-isp.lab.local:/var/backup/web1 /var/backup/lab-isp.lab.local/
  • ตั้ง Crontab เพื่อให้ทำงานตาม Schedule ผมตั้งเป็นตอนเที่ยงคืนของทุกวัน
# crontab -e
0 0 * * * /root/script/backup_isp.sh
  • ลองดู Crontab ที่เราตั้งไว้
# crontab -l
0 0 * * * /root/script/backup_isp.sh

Leave a Reply

Your email address will not be published. Required fields are marked *