在早前的 AmazonEC2 架站 – Security 篇 Part 2 中有介紹過利用 DropboxUploader 把 WordPress 定期備份到免費的 Dropbox 雲端儲存。一直都相安無事,但近來就出現問題了,由於博客的圖片愈來愈大,備份出來的容量似乎一旦超出 10MB ,DropboxUploader 就不能把它順利地上傳到 Dropbox 空間了。
博客不能沒有備份的,而備份的主要重點就是備份不能與正本存放於同一個儲存裝置,所以我得要設法打破 Dropbox 這 10MB 上傳的限制。
Dropbox API

在網上找尋和研究了一陣,發現 Dropbox API 應該會是備份的出路。相信大家大多數使用 Dropbox ,都只是在自己的電腦中安裝 Dropbox Client 或直接登入它的網頁介面,把檔案上傳到 Dropbox 或從 Dropbox 下載檔案。但其實,Dropbox 是有提供 SDK 及 API 給廣大的開發人員使用,讓 Dropbox Cloud Storage 整合於自己的程式中。
要利用 Dropbox API ,首先當然要有 Dropbox 的賬戶,然後得要登入以下的網站去登記一個 Dropbox App:
https://www.dropbox.com/developers
- 登入後,於 App Console 畫面中點擊 Create app。
- 因為我的應用只是想把備份上傳至 Dropbox ,所以我就選擇了 Dropbox API app 及 Files and datastores,然後就要選擇是否只局限新的 Dropbox App 只存取它自己檔案或存取所有 Dropbox 賬戶的檔案,最後就要給予它一個應用程式名稱。
- 完成後點擊 Create app,您的 Dropbox App 就創建成功並進入以下的畫面,其中一定要記下 App key 和 App Secret 這兩個數值。
- 如果只是個人應用,就無須申請 Production 和加入其他開發人員。做到這裡,最基本的 Dropbox App 配置已經完成,接下來就是下載 Dropbox SDK。於 Dropbox SDK 下載網頁中,有林林種種不同的程式開發語言的 SDK:
4. Dropbox SDK List 由於我是打算利用 Unix Cron 把博客備份到 Dropbox 去,所以順理成章地選擇了 Python SDK,因為現在幾乎所有 Linux 都預裝 Python 了。
- SSH 登入 Amazon EC2 Instance,然後 wget 了 Python SDK,按照 Dropbox 的 Installation Guide 安裝了 SDK 後 (安裝要 root 權限 ),在 SDK 中的 example folder 下,找到了 cli_client.py 這個 Python 程序。
- 由於懶人的關係,我只打算直接應用 cli_client.py 就算了,並不想親自開發專有的程序來作備份用途。試用後,其實 cli_client.py 都足夠應付一般所需了。首先,大家要 vi cli_client.py,然後加入自己早前記下來的 Dropbox App 的 App key 和 App secret。
# XXX Fill in your consumer key and secret below # You can find these at http://www.dropbox.com/developers/apps APP_KEY = '<Input App Key here>' APP_SECRET = '<Input App Secret here>'
- 然後在 Unix shell 下執行 cli_client.py,第一次應用首先要拿到 OAuth 授權 Token ,所以要打下 login,如下:
Dropbox> login 1. Go to: https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=xxxxxxxxxxxxxx 2. Click "Allow" (you might have to log in first). 3. Copy the authorization code. Enter the authorization code here:
- 它會告訴您用瀏覽器到一個 URL 去拿到授權密碼:
5. Ask for Authorization 按下允許,就會顯示出授權碼:
- 把授權碼輸入 cli_client.py 的 login 程序後,cli_client.py 基本上就已經與您的 Dropbox App 打通了。完成後,您會收到 Dropbox 寄給您一封電郵說明有一個應用程式連接到 Dropbox ,並您會發現在您的 Dropbox 中會多了一個 應用程式文件夾,當中還有您剛建立的 Dropbox App 名稱的文件夾。
- 另外,您還可以在 cli_client.py 利用 help 顯示可用的命令:
Dropbox> help account_info: display account information cat: display the contents of a file cd: change current working directory exit: exit get: Copy file from Dropbox to local file and print out the metadata. Examples: Dropbox> get file.txt ~/dropbox-file.txt login: log in to a Dropbox account login_oauth1: log in to a Dropbox account logout: log out of the current Dropbox account ls: list files in current remote directory mkdir: create a new directory mv: move/rename a file or directory put: Copy local file to Dropbox Examples: Dropbox> put ~/test.txt dropbox-copy-test.txt rm: delete a file or directory search: Search Dropbox for filenames containing the given string. thumbnail: Copy an image file's thumbnail to a local file and print out the file's metadata. Examples: Dropbox> thumbnail file.txt ~/dropbox-file.txt medium PNG Dropbox>
- 基本上都是十分簡單易明的 get, put, ls, rm 等命令,應該很快上手,利用以下的腳本 ( Script ) 作測試:
cat << EOF | ./cli_client.py put ~/db_20140530.tgz db_20140530.tgz EOF
- Yeah! 大功告成,成功地突破了 10MB 的限制,只要把腳本 ( Script ) 作少少修正,就可以繼續用 Dropbox 作為我的博客備份了。
Dropbox API – 基礎應用