คู่มือนี้แนะนำวิธีเข้ารหัสไฟล์ Backup ใน Linux ด้วย OpenSSL (มาติดตั้งมาพร้อม Linux ทุกตัว) ไม่ต้องติดตั้งโปรแกรมเพิ่ม
ขั้นตอนปกติ: สร้าง Tar.gz
# บีบอัด
tar -zcf todays_backup.tar.gz todays_backup
# แตกไฟล์
tar -zxf todays_backup.tar.gz
เข้ารหัสขณะสร้าง Backup (Encrypt + Compress)
# tar -zcf - todays_backup | openssl des3 -salt -k "yourpassword" | dd of=todays_backup.des3
ถอดรหัสและแตกไฟล์ (Decrypt + Extract)
# dd if=todays_backup.des3 | openssl des3 -d -k "yourpassword" | tar -zxf -
อธิบายคำสั่ง
| ส่วน | คำอธิบาย |
tar -zcf - | สร้าง gzip tar archive ส่ง stdout |
openssl des3 -salt -k "pass" | เข้ารหัส DES3 (Triple DES) ใช้ Salt + Password |
dd of=file | เขียน stdin ไปไฟล์ |
dd if=file | อ่านไฟล์ส่ง stdout |
openssl des3 -d -k "pass" | ถอดรหัส DES3 (-d = decrypt) |
tar -zxf - | แตก gzip tar จาก stdin (- ต้องมีท้ายคำสั่ง) |
⚠️ คำเตือน: DES3 (3DES) ล้าสมัยและอ่อนแอลงแล้ว แนะนำใช้ AES-256 แทน ด้านล่าง
วิธีแนะนำ (AES-256 - ปลอดภัยกว่า)
# เข้ารหัส + บีบอัด (AES-256-CBC)
tar -czf - todays_backup | openssl enc -aes-256-cbc -salt -pbkdf2 -k "yourpassword" > todays_backup.enc
# ถอดรหัส + แตกไฟล์
openssl enc -d -aes-256-cbc -pbkdf2 -k "yourpassword" -in todays_backup.enc | tar -xzf -
ตัวเลือก OpenSSL ที่สำคัญ
| ตัวเลือก | คำอธิบาย |
-aes-256-cbc | อัลกอริทึม AES 256-bit CBC mode (มาตรฐานปัจจุบัน) |
-salt | ใช้ Salt ป้องกัน Rainbow Table |
-pbkdf2 | ใช้ PBKDF2 Key Derivation (แรงกว่าเก่า) |
-iter 100000 | จำนวน Iteration (มาก = ช้ากว่า แตะแรงกว่า) |
-base64 (-a) | Encode output เป็น Base64 (สำหรับ copy-paste) |
Best Practices
- ใช้ Password Manager เก็บรหัสผ่าน (Bitwarden, KeePass, 1Password)
- ทดสอบถอดรหัสทันทีหลังสร้าง Backup
- เก็บ Backup หลายสถานที่ (3-2-1 Rule)
- หมุนเวียนรหัสผ่านเป็นระยะ
- ใช้ GPG แทน OpenSSL สำหรับการใช้งานจริง (Key management ดีกว่า)
การเข้ารหัส Backup ด้วย OpenSSL ใช้ได้ทันที ไม่ต้องติดตั้งเพิ่ม แต่อย่าลืมทดสอบการกู้คืนข้อมูล!