最近在學習 MySQL 的 bin-log 時候考慮到數據備份的問題,突然想到如果能將數據通過 Linux 命令行方式備份到百度網盤,那是一件多么牛逼的事情。百度網盤有免費的 2TB 存儲空間,而且有百度做靠山,不怕數據丟失,安全可靠。說干就干,通過百度 and 谷歌找到了幾種方式,比較喜歡 bypy 的方式,使用簡單,方便。下邊簡單的總結一下如何使用 bypy 實現百度網盤數據的同步。
這是一個百度雲的 Python 客戶端,其主要目的和功能,就是為 Linux 使用者提供一種在命令行下,使用百度雲盤中2T存儲空間的方法。它提供文件列表、下載、上傳、比較、向上同步、向下同步,等操作。
系統環境:
Linux 系統 + Python 2.7
安裝軟件工具:
pip install requests pip install bypy
授權登陸:
執行 bypy info,顯示下邊信息,根據提示,通過瀏覽器訪問下邊灰色的https鏈接,如果此時百度網盤賬號正在登陸,會出現長串授權碼,復制。
[root@ineedle ~]# bypy info Please visit: # 訪問下邊這個連接,復制授權碼 https://openapi.baidu.com/oauth/2.0/authorize?scope=basic+netdisk&redirect_uri=oob&response_type=code&client_id=q8WE4EpCsau1oS0MplgMKNBn And authorize this app Paste the Authorization Code here within 10 minutes. Press [Enter] when you are done # 提示在下邊粘貼授權碼
在下邊圖示紅色位置粘貼授權碼,耐心等待一會即可(1-2分鍾)
Press [Enter] when you are done a288f3d775fa905a6911692a0808f6a8 Authorizing, please be patient, it may take upto None seconds... Authorizing/refreshing with the OpenShift server ... OpenShift server failed, authorizing/refreshing with the Heroku server ... Successfully authorized Quota: 2.015TB Used: 740.493GB
授權成功。
測試上傳和同步本地文件到雲盤
由於百度PCS API權限限制,程序只能存取百度雲端/apps/bypy目錄下面的文件和目錄。我們可以通過:
[root@ineedle ~]# bypy list /apps/bypy ($t $f $s $m $d):
把本地當前目錄下的文件同步到百度雲盤:
# bypy upload
把雲盤上的內容同步到本地:
# bypy downdir
比較本地當前目錄和雲盤根目錄,看是否一致,來判斷是否同步成功:
# bypy compare