Difference between revisions of "BackUp Data to Google Drive"

From CMU ITSC Network
Line 190: Line 190:
 
[[File:05-gdrive-folder-daily.PNG|link=]]
 
[[File:05-gdrive-folder-daily.PNG|link=]]
  
 +
==== การสำรองข้อมูลที่มีไฟล์จำนวนมากใน folder ====
 +
กรณีที่มีการสำรอง folder จำพวกที่มี file source code หรือรูปภาพไฟล์เล็ก ๆ แต่จำนวนไฟล์มาก ๆ ควรที่จะมัดรวบไฟล์เป็นก้อนเดียวกันก่อนที่จะอัพโหลดขึ้นไปดังตัวอย่าง shell script ดังนี้
 +
<syntaxhighlight lang=bash>
 +
#!/bin/bash
 +
cd ~
 +
rm jumbomapmk3.tgz
 +
tar cfz jumbomapmk3.tgz jumbomapmk3
 +
rclone copy jumbomapmk3.tgz google_drive:/
 +
</syntaxhighlight>
  
 
== link ที่เกี่ยวข้อง ==
 
== link ที่เกี่ยวข้อง ==

Revision as of 09:15, 29 November 2018

บทนำ

มช. มีบริการ G Suite for Education จาก Google ซึ่งบุคลากรและนักศึกษาทุกคนสามารถเปิดใช้งานได้และ Google Drive ของ G Suite for Education มีพื้นที่เก็บข้อมูลไม่จำกัด บทความนี้จะแนะนำการใช้เครื่องมือสำรองข้อจาก Server เก็บไว้บน Google Drive แบบอัตโนมัติ

rclone

rclone เป็น open source tool ที่สามารถ copy file ไปเก็บไว้บน cloud storage ได้ และรองรับ Google Drive การทำงานจะคล้ายกับ rsync

ติดตั้ง rclone

wget https://rclone.org/install.sh && sudo bash install.sh

Google Drive folder id

เพื่อให้เป็นระเบียบควรสร้าง folder บน Google Drive ไว้เพื่อเก็บข้อมูลจาก Server แล้วใช้ folder นี้เป็น root folder ของการสำรองข้อมูล โดยจะต้องใช้ folder id โดยหาได้จาก url ของ folder ดังภาพที่ highlight สีเหลือง
04-gdrive-folder-id.PNG

เพิ่ม Google Drive

รันคำสั่ง rclone config เพื่อเข้าสู่ interactive shell หลัง // คือคำอธิบาย

rclone config
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n   // n เพื่อสร้าง remote ใหม่
name> google_drive  // ตั้งชื่อของ remote
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / A stackable unification remote, which can appear to merge the contents of several remotes
   \ "union"
 2 / Alias for a existing remote
   \ "alias"
 3 / Amazon Drive
   \ "amazon cloud drive"
 4 / Amazon S3 Compliant Storage Providers (AWS, Ceph, Dreamhost, IBM COS, Minio)
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Box
   \ "box"
 7 / Cache a remote
   \ "cache"
 8 / Dropbox
   \ "dropbox"
 9 / Encrypt/Decrypt a remote
   \ "crypt"
10 / FTP Connection
   \ "ftp"
11 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
12 / Google Drive
   \ "drive"
13 / Hubic
   \ "hubic"
14 / JottaCloud
   \ "jottacloud"
15 / Local Disk
   \ "local"
16 / Mega
   \ "mega"
17 / Microsoft Azure Blob Storage
   \ "azureblob"
18 / Microsoft OneDrive
   \ "onedrive"
19 / OpenDrive
   \ "opendrive"
20 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
21 / Pcloud
   \ "pcloud"
22 / QingCloud Object Storage
   \ "qingstor"
23 / SSH/SFTP Connection
   \ "sftp"
24 / Webdav
   \ "webdav"
25 / Yandex Disk
   \ "yandex"
26 / http Connection
   \ "http"
Storage> 12 // 12 เพื่อใช้ชนิดของ remote เป็น Google Drive
** See help for drive backend at: https://rclone.org/drive/ **

Google Application Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> // ปล่อยว่างไว้
Google Application Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> // ปล่อยว่างไว้
Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / Full access all files, excluding Application Data Folder.
   \ "drive"
 2 / Read-only access to file metadata and file contents.
   \ "drive.readonly"
   / Access to files created by rclone only.
 3 | These are visible in the drive website.
   | File authorization is revoked when the user deauthorizes the app.
   \ "drive.file"
   / Allows read and write access to the Application Data folder.
 4 | This is not visible in the drive website.
   \ "drive.appfolder"
   / Allows read-only access to file metadata but
 5 | does not allow any access to read or download file content.
   \ "drive.metadata.readonly"
scope> 1 // 1 เพื่อให้อัพโหลดไฟล์ได้
ID of the root folder
Leave blank normally.
Fill in to access "Computers" folders. (see docs).
Enter a string value. Press Enter for the default ("").
root_folder_id> 1VLabR************************* // ปล่อยว่างหรือใช้ folder id ซึ่งได้จาก url ของ folder ตามหัวข้อ 2.2
Service Account Credentials JSON file path
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file> // ปล่อยว่างไว้
Edit advanced config? (y/n)
y) Yes
n) No
y/n> n // ตอบ n
Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine or Y didn't work
y) Yes
n) No
y/n> n // ตอบ n
If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=202264815644.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=2576703*******************eb789cf
Log in and authorize rclone for access
Enter verification code> 4/pABeqBWl**************** // เปิด url ด้านบนที่ browser ที่ log in session ของ G suite for Education แล้วทำการอนุญาต Application rclone แล้วนำ access token ที่ได้มาใส่ที่ขั้นตอนนี้
Configure this as a team drive?
y) Yes
n) No
y/n> n // ตอบ n
--------------------
[google_drive]
type = drive
scope = drive
root_folder_id = 1VLabRr***********************fMN5j
token = {"access_token":"ya29.Glt***************************************************************OrQU1srvkH","token_type":"Bearer","refresh_token":"1/H****************************c_CIc","expiry":"2018-11-29T15:40:57.0199557+07:00"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y // ตอบ y
Current remotes:

Name                 Type
====                 ====
google_drive         drive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q // ตอบ q

คำสั่งเบื้องต้น

copy file จาก local ไป remote

rclone copy /local/path remote:path

เช่น copy file db.cmu.ac.th ไปยัง folder Google drive

rclone copy db.cmu.ac.th google_drive:/

ตัวอย่างการใช้งานต่าง ๆ

การสำรองฐานข้อมูลทุกวัน

ฐานข้อมูล mysql เราไม่ควรที่จะ copy file ของฐานไปตรง ๆ เพราะมีการ lock การใช้งาน ไฟล์ที่สำรองไว้อาจจะเปิดไม่ได้
วิธีที่ถูกต้องคือต้อง dump ฐานออกมาเป็น sql แล้วสำรองไว้และถ้าต้องใช้ค่อย restore เข้าฐานใหม่
ซึ่งจะต้องใช้สำสั่ง mysqldump ออกมาใส่ไฟล์แล้วใช้ rclone upload โดยใช้ shell script ดังนี้

#!/bin/bash
cd ~
mysqldump -uDBUSERNAME -pDBPASSWORD DBNAME > DB.sql
rclone copy DB.sql gdrive:`date '+%Y-%m-%d-%H-%M-%S'`/

สมมติ save file นี่ชื่อ backup_db_to_google_drive.sh ให้สั่ง run ไฟล์นี้ทุกตีสองสี่สิบสี่นาทีใน crontab

44 2 * * * /bin/bash /path/to/file/backup_db_to_google_drive.sh > /dev/null 2>&1

ก็จะได้ folder บน Google Drive ที่ชื่อ folder เป็นวันที่และใน folder มีข้อมูลจากฐานข้อมูล ณ เวลา 2.44 ของทุกวันลักษณะนี้
05-gdrive-folder-daily.PNG

การสำรองข้อมูลที่มีไฟล์จำนวนมากใน folder

กรณีที่มีการสำรอง folder จำพวกที่มี file source code หรือรูปภาพไฟล์เล็ก ๆ แต่จำนวนไฟล์มาก ๆ ควรที่จะมัดรวบไฟล์เป็นก้อนเดียวกันก่อนที่จะอัพโหลดขึ้นไปดังตัวอย่าง shell script ดังนี้

#!/bin/bash
cd ~
rm jumbomapmk3.tgz
tar cfz jumbomapmk3.tgz jumbomapmk3
rclone copy jumbomapmk3.tgz google_drive:/

link ที่เกี่ยวข้อง

ติดต่อสอบถามเพิ่มเติม