ก๊วนซอฟท์แวร์ </softganz> SoftGang (Gang Software)

Web &amp; Software Developer Gang.

Synology backup from server

by Little Bear @25 ต.ค. 69 15:24 ( IP : 183...166 ) | Tags : Synology

สิ่งที่ต้องเช็กเพิ่มบน Synology NAS (DSM)

Step 1: เปิดสิทธิ์ใช้งาน User Home:

  • ไปที่ Control Panel > User & Group > แท็บ Advanced
  • เลื่อนลงมาด้านล่างสุด ติ๊กถูกที่ "Enable user home service" แล้วกด Apply (ขั้นตอนนี้สำคัญมาก เพราะถ้าไม่เปิด จะไม่มีโฟลเดอร์ ~/.ssh ให้เก็บ Key)

Step 2: Generate an SSH key pair on the source server (if you don't have one):

bash ssh-keygen -t rsa -b 4096

Step 3: ตรวจสอบสิทธิ์ (Permission) ของโฟลเดอร์:

ระบบรักษาความปลอดภัยของ SSH บน Synology เข้มงวดมาก หากสิทธิ์ของโฟลเดอร์เปิดกว้างเกินไป มันจะปฏิเสธการใช้ Key ให้ล็อกอินเข้า SSH ของ NAS แล้วรันคำสั่งเหล่านี้เพื่อเคลียร์ Permission ให้ถูกต้อง

bash chmod 755 ~ chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys

เมื่อตั้งค่าเสร็จแล้ว ให้ทดลองรันคำสั่ง ssh -p 22 user@NAS_IP_ADDRESS จาก server ต้นทางอีกครั้ง ตัวระบบจะต้องล็อกอินเข้า server ได้ทันทีโดยไม่ต้องกรอกรหัสผ่าน และคุณจะสามารถเริ่มรันสคริปต์ rsync แบบอัตโนมัติได้ทันทีครับ

Step 4: Upload ssh key file to server

เนื่องจาก Synology DSM ไม่ได้ติดตั้งคำสั่ง ssh-copy-id มาให้เป็นค่าเริ่มต้น สามารถใช้วิธี คัดลอก Public Key แบบ Manual ผ่านคำสั่งมาตรฐานแทน

bash cat ~/.ssh/id_rsa.pub | ssh -p REMOTE_PORT syno_user@NAS_IP_ADDRESS "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Step 5: Test backup script

bash rsync -avz -e "ssh -p REMOTE_PORT" remote_user@REMOTE_SERVER_IP:/path/to/remote/source/folder/ /volume1/BackupFolder/

Step 6: set cron job to run backup when power on

To run your rsync backup command automatically every time your Synology NAS powers on, you can utilize the built-in Task Scheduler in DSM. Synology handles this via a trigger-based system instead of editing the traditional Linux crontab directly. Here are the precise steps to set it up:

Step 6.1: Create a Triggered Task in DSM

  1. Log in to your Synology DSM interface as an administrator.
  2. Open Control Panel > Task Scheduler.
  3. Click the Create button, select Triggered Task, and then click User-defined script.

Step 6.2: Configure the Task Settings

A configuration window will appear. Fill out the settings in the two tabs as follows:

  1. General Tab

• Task: Give your task a recognizable name (e.g., Run Rsync Backup on Boot). • User: Select root (or your specific syno_user account if your SSH keys are mapped directly to that user's home directory). • Event: Select Boot-up from the drop-down menu. • Pre-task: Leave this unselected. • Make sure the Enabled checkbox at the bottom is ticked.

  1. Task Settings Tab

• Send run details by email (Optional): Check this if you want an email report containing the rsync log every time the NAS boots up. • Run command: Paste your exact rsync pull command into the text area. ```bash

Example command (Make sure SSH key authentication is already set up for the user running this)

rsync -avz -e "ssh -p REMOTEPORT" remoteuser@REMOTESERVERIP:/path/to/remote/source/folder/ /volume1/BackupFolder/ ```

Click OK to save the task.

???? Crucial Pro-Tip for Boot-up Scripts

When a Synology NAS first boots up, it initiates startup scripts immediately—sometimes before the network interface or internet connection is fully operational. If rsync attempts to run immediately, it will fail with a connection error. To prevent this, add a short delay (e.g., 60 seconds) to your command script to give the network time to initialize. Update your Run command field to look like this:

bash sleep 60 rsync -avz -e "ssh -p REMOTE_PORT" remote_user@REMOTE_SERVER_IP:/path/to/remote/source/folder/ /volume1/BackupFolder/