Sync OneDrive on Ubuntu 16.04

เชื่อว่าหลายองค์กรคงใช้เครื่องมือในการจัดการธุรกิจผ่านทาง Cloud Business Platform ของ 2 ค่ายระหว่าง G-Suite ของ Google หรือ Office365 ของ Microsoft แต่เราจะมาพูดถึง Office365 ซึ่งในการทำงานกับ Production ที่เป็น Linux แล้วต้องการต่อกับ OneDrive เพื่อเป็นที่เก็บไฟล์ จะติดตั้งยังไง


Install

  • ทำการ Update และ Upgrade
# apt-get update && apt-get upgrade -y
  • ทำการ Add Repository เพื่อทำการติดตั้ง Python 3.6
# apt-get install software-properties-common -y
# add-apt-repository ppa:jonathonf/python-3.6
  • ทำการ Update
# apt-get update
  • ทำการติดตั้ง Package
# apt-get install python3.6 python3.6-dev python3-pip git unzip wget -y
  • กำหนด Priority ของ Python แต่ละเวอร์ชั่น
# update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
# update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
  • กำหนด Priority ให้รันแบบ Auto Mode
# update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.6   2         auto mode
  1            /usr/bin/python3.5   1         manual mode
  2            /usr/bin/python3.6   2         manual mode

Press  to keep the current choice[*], or type selection number:
  • ตรวจสอบเวอร์ชั่น Python
# python3 --version
Python 3.6.5
  • ดาวน์โหลด onedrived จาก Github
# git clone https://github.com/derrix060/onedriveClient.git
# cd onedriveClient
  • ทำการติดตั้ง Package ที่จำเป็นสำหรับ Python
# pip3 install -r requirements.txt
  • ทำการตั้งค่า Locale และ Language
# export LC_ALL=C.UTF-8
# export LANG=C.UTF-8
  • ทำการติดตั้ง onedrived
# python3 ./setup.py install
  • ทำการเพิ่ม Account Office365
# onedrived-pref account add -b
WARNING:root:Failed to load config file: [Errno 2] No such file or directory: '/root/.config/onedrived/onedrived_config_v2.json'. Use default.
NOTE: To better manage your OneDrive accounts, onedrived needs permission to access your account info (e.g., email address to distinguish different accounts) and read/write your OneDrive files.

Paste this URL into your browser to sign in and authorize onedrived:

https://login.microsoftonline.com/common/oauth2/authorize?client_id=6fdb55b4-c905-4612-bd23-306c3918217c&response_type=code&redirect_uri=https%3A%2F%2Fod.cnbeining.com&scope=U+s+e+r+.+R+e+a+d+++o+f+f+l+i+n+e+_+a+c+c+e+s+s

The authentication web page will finish with a blank page whose URL starts with "https://od.cnbeining.com". Paste this URL after the prompt.
Paste URL here:
  • จากนั้นนำ URL ไปเปิดใน Browser แล้วทำการลงชื่อเข้าใช้
  • จะแสดงการร้องขอสิทธิ์ที่ใช้ แล้วคลิกยอมรับ
  • คลิกยอมรับ
  • นำ URL ที่ได้ไปวางไว้ตรง Paste URL here:
Successfully authorized onedrived.
Successfully added account for ณัฐเศรษฐ์ ศักดิ์ศุภนรา (natthasath.sak@nida.ac.th)!

All OneDrive accounts associated with user "root":

  #  Account ID                          Owner Name              Email Address                  Profile Type
---  ----------------------------------  ----------------------  -----------------------------  --------------
  0  012BHBJBV6Y2GOVW7725BZO354PWSELRRZ  ณัฐเศรษฐ์ ศักดิ์ศุภนรา       natthasath.sak@nida.ac.th      Business
  • ทำการ Set Drive
# onedrived-pref drive set
Reading drives information from OneDrive server...

All available Drives of authorized accounts:

  #  Account Email                  Drive ID                                                            Type      Quota                        Status
---  -----------------------------  ------------------------------------------------------------------  --------  ---------------------------  --------
  0  natthasath.sak@nida.ac.th      b!oe2_Zi6hfU-MOResOF3H1uUHUzmc2sJLpZ5Hy3nMCB9w0c-AzS6cQILLjWPdCQci  business  15.7 GB Used / 1.0 TB Total  normal
  • จากนั้นก็ Enter ไปเรื่อย ๆ จนเสร็จ
Please enter row number of the Drive to add or modify (CTRL+C to abort): 0

Going to add/edit Drive "b!oe2_Zi6hfU-MOResOF3H1uUHUzmc2sJLpZ5Hy3nMCB9w0c-AzS6cQILLjWPdCQci" of account "natthasath.sak@nida.ac.th"...
Enter the directory path to sync with this Drive [/root/OneDrive]:
Enter the path to ignore file for this Drive [/root/.config/onedrived/ignore_v2.txt]:

Successfully configured Drive b!oe2_Zi6hfU-MOResOF3H1uUHUzmc2sJLpZ5Hy3nMCB9w0c-AzS6cQILLjWPdCQci of account natthasath.sak@nida.ac.th (012BHBJBV6Y2GOVW7725BZO354PWSELRRZ):
  Local directory: /root/OneDrive
  Ignore file path: /root/.config/onedrived/ignore_v2.txt
  • ทำการ Start onedrived เพื่อเริ่มการ Sync ในโหมด Debug มันจะถามหา Webhook
# onedrived start --debug
Starting onedrived ... OK
[2018-10-24 07:45:19,526] DEBUG: MainThread: Starting new HTTPS connection (1): api.onedrive.com
[2018-10-24 07:45:20,358] DEBUG: MainThread: https://api.onedrive.com:443 "GET /v1.0/drives HTTP/1.1" 200 364
[2018-10-24 07:45:20,365] DEBUG: MainThread: Refreshing repository session.
[2018-10-24 07:45:20,367] DEBUG: MainThread: Starting new HTTPS connection (1): login.live.com
[2018-10-24 07:45:21,444] DEBUG: MainThread: https://login.live.com:443 "POST /oauth20_token.srf HTTP/1.1" 200 1718
[2018-10-24 07:45:21,449] INFO: MainThread: Session for account a65b3dddca380e69 will expire in 3599 seconds.
[2018-10-24 07:45:21,452] DEBUG: MainThread: Will refresh session in 3479 seconds.
[2018-10-24 07:45:21,453] INFO: MainThread: Sweeping onedrived temporary files from local repositories.
[2018-10-24 07:45:21,459] DEBUG: Worker-0: Started.
[2018-10-24 07:45:21,465] DEBUG: Worker-1: Started.
[2018-10-24 07:45:21,465] DEBUG: Worker-2: Started.
[2018-10-24 07:45:21,466] DEBUG: Worker-3: Started.
[2018-10-24 07:45:21,466] DEBUG: Worker-4: Started.
[2018-10-24 07:45:21,467] DEBUG: Worker-5: Started.
[2018-10-24 07:45:21,468] DEBUG: Worker-6: Started.
[2018-10-24 07:45:21,468] DEBUG: Worker-7: Started.
[2018-10-24 07:45:21,469] DEBUG: Worker-8: Started.
[2018-10-24 07:45:21,470] DEBUG: Worker-9: Started.
[2018-10-24 07:45:21,470] DEBUG: Worker-10: Started.
[2018-10-24 07:45:21,471] DEBUG: Worker-11: Started.
[2018-10-24 07:45:21,472] DEBUG: Worker-12: Started.
[2018-10-24 07:45:21,472] DEBUG: Worker-13: Started.
[2018-10-24 07:45:21,473] DEBUG: Worker-14: Started.
[2018-10-24 07:45:21,473] DEBUG: Worker-15: Started.
[2018-10-24 07:45:21,481] CRITICAL: MainThread: Error initializing webhook: Did not find ngrok executable "ngrok".
[2018-10-24 07:45:21,483] INFO: MainThread: Shutting down. Code: Exiting with message: None.
[2018-10-24 07:45:21,487] INFO: Worker-0: Stopped.
[2018-10-24 07:45:21,491] INFO: Worker-1: Stopped.
[2018-10-24 07:45:21,491] INFO: Worker-2: Stopped.
[2018-10-24 07:45:21,491] INFO: Worker-3: Stopped.
[2018-10-24 07:45:21,491] INFO: Worker-4: Stopped.
[2018-10-24 07:45:21,491] INFO: Worker-5: Stopped.
[2018-10-24 07:45:21,491] INFO: Worker-6: Stopped.
[2018-10-24 07:45:21,491] INFO: Worker-7: Stopped.
[2018-10-24 07:45:21,491] INFO: Worker-8: Stopped.
[2018-10-24 07:45:21,491] INFO: Worker-9: Stopped.
[2018-10-24 07:45:21,492] INFO: Worker-10: Stopped.
[2018-10-24 07:45:21,492] INFO: Worker-11: Stopped.
[2018-10-24 07:45:21,492] INFO: Worker-12: Stopped.
[2018-10-24 07:45:21,492] INFO: Worker-13: Stopped.
[2018-10-24 07:45:21,492] INFO: Worker-14: Stopped.
[2018-10-24 07:45:21,499] INFO: Worker-15: Stopped.
[2018-10-24 07:45:21,500] INFO: MainThread: Shut down complete.
[2018-10-24 07:45:21,501] DEBUG: MainThread: Closing database "/root/.config/onedrived/items_a65b3dddca380e69.sqlite3".
All children are gone. Parent is exiting...
  • ทำการติดตั้ง ngrok เพื่อทำหน้าที่เป็น Webhook
# wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
unzip ngrok-stable-linux-amd64.zip
  • ทำการเชื่อมต่อ Account ด้วย Token
# ./ngrok authtoken 7mruFsQ8i1o2cikYmihKc_3BP6o3f6ayeDGweRQYQJW
# mv ngrok /usr/bin/ngrok
  • ทำการ Start HTTP Tunnel
# ngrok http 80
ngrok by @inconshreveable                                                                                                                                                                                                    (Ctrl+C to quit)

Session Status                online
Account                       FS DotNet (Plan: Free)
Version                       2.2.8
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://ae0183b8.ngrok.io -> localhost:80
Forwarding                    https://ae0183b8.ngrok.io -> localhost:80

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00
  • ทำการ Start onedrived เพื่อเริ่มการ Sync อีกครั้ง
# onedrived start</pre>
  • หากต้องการแก้ไขค่าต่าง ๆ
# vi ~/.config/user-dirs.dirs
  • หากต้องการลบ Drive
# onedrived-pref drive del
  • ตรวจสอบการ Sync ไฟล์และโฟลเดอร์
# cd ~/OneDrive
# ls -lrt
drwx------ 2 root root 4096 Oct 19 06:17 Attachments
drwx------ 2 root root 4096 Oct 19 06:17 Documents

อ่านเพิ่มเติม : https://bit.ly/2PI91MG


Leave a Reply

Your email address will not be published. Required fields are marked *