Backups ข้อมูลขึ้น Google Drive ด้วย rclone
หลายคนคงเจอปัญหาว่าต้องการที่จะ backup ข้อมูลที่สำคัญขึ้น cloud จากพวก linux server ซึ่งก็มีวิธีการหลายๆวิธีในการใช้ แต่ในวันนี้จะมาแนะนำการ backup ข้อมูลจาก linux server ขึ้นไว้ใน google drive ด้วย rclone
Rclone คืออะไร ไว้ทำอะไร? คือแอพพลิเคชั่นที่เอาไว้ mount ไปยัง storage ต่างๆที่มีให้เลือกมากถึง 27 ชนิดให้เราได้ใช้งาน
วิธีการลง rclone สามารถติดตั้งด้วยคำสั่งเดียว
curl https://rclone.org/install.sh | sudo bash
การเพิ่ม google drive ใน rclone
rclone config
- เลือก n เพื่อสร้าง remote
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
2. ตั้งชื่อ remote เช่น google-drive เมื่อเรียบร้อยจะมี list ของ storage มาให้เราเลือก
3. เลือก google drive หมายเลข 13
Storage> 13
4. ขั้นตอนการใส่ client_id, client_secret. ให้กด enter ข้ามไปได้เลย
5. scope ให้เลือก 1 Full access all files, excluding Application Data Folder. “drive”
scope> 1
6. สำหรับ root_folder_id,service_account_file ให้กด enter ข้ามไปได้เลย
7. การแก้ไขไฟล์ config เพิ่มเติมเลือก N
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n>n
8. auto config เลือก Y หลังจากนั้นจะมี url มาสำหรับการเข้าหน้าเว็บเพื่อยื่นยัน
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine
y) Yes (default)
n) No
y/n> y
เนื่องจากเราไม่สามารถเข้าหน้าเว็บจากเครื่อง server ได้จึงจำเป็นต้องทำการ tunnel ไปยังเครื่อง server เพื่อที่จะเข้าหน้า login เพื่อยืนยัน google account
ssh root@db-server -L 53682:localhost:53682
จากคำสั่งคือเป็นการ tunnel port 53682 ไปยัง remote ที่ port 53682 ซึ่งเมื่อเรา login เราสามารถเข้าหน้า localhost:53682/auth ได้จากเครื่องเราจากนั้นก็สามารถ authen กับ google เพื่อยืนยันสิทธิ์ได้
9. เป็น team drive ให้เลือก n
Configure this as a team drive?
y) Yes
n) No (default)
y/n> n
10. หลังจากนั้นจะได้ค่า config สำหรับการเชื่อมต่อ google drive ให้ตอบ y
[google-drive]
type = drive
scope = drive
token = {“access_token”:”ya29.a0AfH6SMDaSC6PaHq0HQLgeLpf0fbffRAvQq8Dk2LbYcReOYO8N-d5aHZ_3uRRVt4S0A72MXvLsIQx_N4TRouTs1wBDwkbRLHPRMsVm0Du-7Gpb3YSyyPMzuBfiFUOEC4GzPduhII_3AFKWujJZKBrxLpa5wGcFQ-JUetj0Vw28CQ”,”token_type”:”Bearer”,”refresh_token”:”1//0gQ-64wn5TaG_CgYIARAAGBASNwF-L9Irr3UpGUB7QLCyOl92pV94T2fB8-Q2otlnLiaP9AoWHlIifxlNOr7eU5ett3P_tUZ6KVU”,”expiry”:”2020–12–19T05:28:09.134461002+07:00"} — — — — — — — — — — y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d>y
เป็นอันว่าเรียบร้อยสำหรับการตั้งค่าให้ rclone สามารถเชื่อมต่อเข้ากับ google drive ได้
11. สร้างไฟล์ gdrive.sh สำหรับ sync folder ที่ต้องการไปยัง google drive
#!/bin/bash/usr/bin/rclone copy — update — verbose — transfers 30 — checkers 8 — contimeout 60s — timeout 300s — retries 3 — low-level-retries 10 — stats 1s “/opt/backups/sql” “google-drive:00-dbs”
ใน file คำสั่งจะมี /opt/backups/sql คือตำแหน่งที่จะ clone ไปยัง google-drive:00-dbs คือ rclone ชื่อ google-drive และมี folder ปลายทางชื่อ 00-dbs
12. สุดท้ายเพื่อให้สามารถที่จะทำงานได้อัตโนมัติทุกวันเวลาตี 1 เราก็สามารถใช้ crontab ในการให้ script รันอัตโนมัติได้
10 1 * * * /opt/backups/gdrive.sh
เป็นอันว่าเรียบร้อยในการ backup ข้อมูลไปยัง google drive
Reference
https://www.howtogeek.com/451262/how-to-use-rclone-to-back-up-to-google-drive-on-linux/