Moonlight Digital Logo Moonlight Digital
← กลับหน้าหลัก ← Back to Home

คู่มือสำรองข้อมูล Cloud Server ป้องกันข้อมูลหายแบบ 3-2-1 Cloud Server Backup Guide: The 3-2-1 Strategy for Businesses

เรียนรู้วิธีการวางระบบสำรองข้อมูลบนคลาวด์เซิร์ฟเวอร์ตามกฎ 3-2-1 พร้อมตัวอย่างสคริปต์ Bash Script และระบบตั้งเวลาอัตโนมัติ เพื่อป้องกันฐานข้อมูลลีดของธุรกิจสูญหายอย่างถาวร Learn how to configure a cloud server backup system using the 3-2-1 backup rule, writing custom bash automated scripts, and configuring cron jobs to secure database assets.

คู่มือสำรองข้อมูล Cloud Server ป้องกันข้อมูลหายแบบ 3-2-1

ในโลกธุรกิจปัจจุบัน "ข้อมูล (Data)" เปรียบเสมือนสินทรัพย์ที่มีค่าที่สุดในการขับเคลื่อนองค์กร ไม่ว่าจะเป็นฐานข้อมูลลูกค้า ประวัติการสั่งซื้อ หรือไฟล์สัญญาทางธุรกิจ แต่สิ่งที่น่ากลัวคือ หลายธุรกิจขนาดกลางและขนาดย่อม (SME) มักไม่มีแผนรับมือความเสียหายของระบบหลังบ้านที่ชัดเจน (Disaster Recovery Plan) เมื่อเกิดกรณีเซิร์ฟเวอร์หลักชำรุดเสียหาย หรือโดนโจมตีด้วยมัลแวร์เรียกค่าไถ่ (Ransomware) ข้อมูลทั้งหมดอาจหายสาบสูญไปอย่างกู้กลับไม่ได้

กฎการสำรองข้อมูลที่ดีที่สุดในโลกที่องค์กรสากลให้การยอมรับคือ "กฎ 3-2-1" ซึ่งบทความนี้จะอธิบายทฤษฎีนี้อย่างง่าย พร้อมทั้งแจกสคริปต์สำรองข้อมูลและคู่มือการตั้งค่าอัตโนมัติให้คุณนำไปปรับใช้ได้ทันทีครับ


1. เจาะลึกโครงสร้างกฎ 3-2-1 (The 3-2-1 Backup Principle)

กฎ 3-2-1 ไม่ใช่เรื่องซับซ้อน แต่คือหลักการกระจายความเสี่ยงเพื่อไม่ให้ข้อมูลสำคัญถูกเก็บไว้ ณ จุดๆ เดียว (Single Point of Failure):

  • เก็บข้อมูลสำรองไว้ 3 ชุด (3 Copies of Data): คือมีข้อมูลจริงที่เปิดใช้งานอยู่ 1 ชุด และมีไฟล์สำรองแยกต่างหากอีกอย่างน้อย 2 ชุด
  • จัดเก็บไว้บนอุปกรณ์จัดเก็บ 2 ประเภท (2 Different Media Types): หลีกเลี่ยงการเก็บไฟล์สำรองทั้งหมดไว้บนฮาร์ดดิสก์ลูกเดียวกับตัวเว็บหลัก เช่น เก็บข้อมูลจริงไว้บน Cloud SSD และเก็บไฟล์สำรองไว้อีกที่หนึ่งในรูปแบบ Object Storage หรือบล็อกเก็บข้อมูลภายนอก
  • ส่งข้อมูลออกไปเก็บนอกสถานที่อย่างน้อย 1 แห่ง (1 Offsite Location): คือการอัปโหลดไฟล์สำรอง 1 ชุดข้ามไปยังผู้ให้บริการคลาวด์รายอื่น (เช่น เก็บเว็บหลักบน AWS แต่ส่งไฟล์แบ็คอัปไปเก็บที่ Google Cloud Storage) หากเกิดเหตุการณ์ไฟไหม้ แผ่นดินไหว หรือคลาวด์หลักล่ม ระบบสำรองนอกสถานที่นี้จะกู้ข้อมูลธุรกิจคืนมาได้ 100%

2. ตัวอย่างสคริปต์สำรองฐานข้อมูลอัตโนมัติ (Bash Script for Backup Automation)

เพื่อความมั่นใจ ระบบสำรองข้อมูลที่ดีไม่ควรใช้คนกดทำด้วยมือ แต่ควรเขียนสคริปต์ทิ้งไว้ให้รันเบื้องหลังอัตโนมัติ ด้านล่างนี้คือตัวอย่างสคริปต์ Bash Script สำหรับดึงข้อมูลฐานข้อมูล PostgreSQL บีบอัดไฟล์ และส่งไปเก็บนอกสถานที่บน AWS S3 Object Storage:

#!/bin/bash

# Configuration
BACKUP_DIR="/var/backups/db"
DB_NAME="moonlight_production"
DATE=$(date +%Y-%m-%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/db_backup_$DATE.tar.gz"
S3_BUCKET="s3://my-moonlight-backups/database"

# Ensure backup directory exists
mkdir -p "$BACKUP_DIR"

# Step 1: Dump database and compress
echo "Starting PostgreSQL backup..."
pg_dump -F c -b -v -f "$BACKUP_FILE" "$DB_NAME"

# Step 2: Upload to Offsite AWS S3 Storage
echo "Uploading backup to AWS S3 offsite storage..."
aws s3 cp "$BACKUP_FILE" "$S3_BUCKET/db_backup_$DATE.tar.gz"

# Step 3: Remove backups older than 7 days from local disk to save space
find "$BACKUP_DIR" -type f -mtime +7 -name "*.tar.gz" -exec rm {} \;

echo "Backup process completed successfully."

3. วิธีการตั้งเวลารันคำสั่งอัตโนมัติด้วย Cron Job (Scheduling Automated Backups)

หลังสร้างสคริปต์เสร็จแล้ว เราจะทำการกำหนดเวลาให้เซิร์ฟเวอร์รันสคริปต์นี้ในเวลาที่ไม่มีคนเข้าเว็บ เช่น เวลาตี 3 ของทุกวัน โดยใช้โปรแกรม Cron Job ในระบบ Linux:

1. เปิด Terminal พิมพ์คำสั่ง: crontab -e

2. เลื่อนลงมาล่างสุดแล้วพิมพ์คำสั่งกำหนดเวลาด้านล่างนี้ (อิงตามพาร์ทไฟล์สคริปต์ของคุณ):

   0 3 * * * /bin/bash /opt/scripts/backup-db.sh >> /var/log/backup.log 2>&1

3. บันทึกและปิดไฟล์ ระบบจะเริ่มทำงานตามเวลาที่กำหนดทันที และจะพ่นบันทึกผลการทำงานเก็บไว้ใน /var/log/backup.log เพื่อให้คุณเข้ามาตรวจสอบประวัติย้อนหลังได้สบายๆ


สรุป

การทำประกันความเสี่ยงข้อมูลหายด้วยกฎ 3-2-1 และสคริปต์รันอัตโนมัติรายวัน คือแนวทางปฏิบัติที่ช่วยปกป้องเสถียรภาพและความอยู่รอดของแบรนด์ SME ยุคใหม่ได้อย่างแท้จริงครับ

Cloud Server Backup Guide: The 3-2-1 Strategy for Businesses

In the modern enterprise ecosystem, "Data" is the most valuable asset driving business growth—spanning customer databases, purchase orders, and sensitive corporate files. Unfortunately, small and medium enterprises (SMEs) often operate without a Disaster Recovery Plan. When local server hard drives crash or Ransomware compromises web nodes, business records can be lost forever.

The global standard for data protection is the 3-2-1 Backup Rule. This article explains the core principles of this strategy, provides a working database backup script, and outlines how to schedule tasks to run automatically.


1. Deep Dive: The 3-2-1 Backup Principle

The 3-2-1 rule is a straightforward risk diversification framework designed to eliminate a Single Point of Failure (SPOF):

  • Keep 3 Copies of Your Data: Maintain one primary production database and at least two separate backup copies to ensure redundant safety buffers.
  • Store Backups on 2 Different Storage Types: Never save your local backups on the same disk storage hosting your live website. If your database runs on cloud block storage, store backup files on an external object store or secondary hardware storage.
  • Keep 1 Backup Copy Offsite: Upload one backup copy to a separate cloud provider (e.g., host your website on AWS, but ship backups to Google Cloud Storage or Backblaze). If a physical disaster strikes your primary data center or hackers delete your hosting account, your offsite copy remains secure.

2. Automated Database Backup Script (Bash Script Template)

A reliable backup strategy should never rely on manual operations. It must run automatically in the background. Here is a production-ready Bash script to dump a PostgreSQL database, compress it, and upload it offsite to an AWS S3 bucket:

#!/bin/bash

# Configuration
BACKUP_DIR="/var/backups/db"
DB_NAME="moonlight_production"
DATE=$(date +%Y-%m-%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/db_backup_$DATE.tar.gz"
S3_BUCKET="s3://my-moonlight-backups/database"

# Ensure backup directory exists
mkdir -p "$BACKUP_DIR"

# Step 1: Dump database and compress
echo "Starting PostgreSQL backup..."
pg_dump -F c -b -v -f "$BACKUP_FILE" "$DB_NAME"

# Step 2: Upload to Offsite AWS S3 Storage
echo "Uploading backup to AWS S3 offsite storage..."
aws s3 cp "$BACKUP_FILE" "$S3_BUCKET/db_backup_$DATE.tar.gz"

# Step 3: Remove backups older than 7 days from local disk to save space
find "$BACKUP_DIR" -type f -mtime +7 -name "*.tar.gz" -exec rm {} \;

echo "Backup process completed successfully."

3. How to Schedule Backups Automatically Using Cron Jobs

Once your script is ready, configure the server to run the task at a quiet hour (such as 3:00 AM daily) using Linux Cron Jobs:

1. Open your terminal and run: crontab -e

2. Insert this cron instruction at the bottom of the configuration file:

   0 3 * * * /bin/bash /opt/scripts/backup-db.sh >> /var/log/backup.log 2>&1

3. Save and close the editor. The task scheduler will immediately execute the script daily at 3:00 AM, outputting execution records to /var/log/backup.log for debugging and logging audits.


Conclusion

Implementing the 3-2-1 backup strategy with automated daily scripts is a simple, high-return security investment that safeguards your SME's core databases and ensures long-term operational resilience.