gdrive该工具是由Go语言编写的,单个二进制文件即可使用,采用Google API方式进行授权,并在本地缓存授权。

安装配置

# macOS
brew install gdrive
# Linux
wget -O /usr/bin/gdrive "https://docs.google.com/uc?id=0B3X9GlR6EmbnQ0FtZmJJUXEyRTA&export=download"
chmod +x /usr/bin/gdrive

安装很简单. 配置也很简单,前提你可以访问Google.

[root@c3 ~]# gdrive about
Authentication needed
Go to the following url in your browser:
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=367116221053-xxxx.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=state

Enter verification code: xxxx
User: ops.ysicing@gmail.com
Used: 4.0 GB
Free: 12.2 GB
Total: 16.1 GB
Max upload size: 5.2 TB

gdrive程序会自动将你的token保存在当前用户目录下的.gdrive目录中,所以不需要了可以把这个文件删掉.当然你也可以删除授权

常用操作

gdrive help <cmd> 帮助操作 因为是基于api, 故有点复杂.

# 默认都是根目录,可以通过-p指定
#关键字检索
gdrive list -q "name contains '关键字'" 
 
#列出指定目录下的文件,不支持递归显示
gdrive list -q "'文件夹ID' in parents"

#从Google Drive根目录处创建文件夹
gdrive mkdir "文件夹名"
 
#上传文件到Google Drive根目录处(如果需要上传文件夹可以使用-r, --recursive 来实现递归上传)
gdrive upload /path/to/file 
 
#下载指定文件到当前位置(如果需要下载文件夹可以使用-r, --recursive 来实现递归下载)
gdrive download 文件ID

简单实现备份

# 结合脚本最完美
# Syncing directory to drive
[root@c3 ~]# gdrive mkdir blog-backup
Directory 1Pqx6bD2IwZXJniUSFykdPnFokuueHS3G created
[root@c3 ~]# gdrive sync upload /root/repo/
handbook/ home/     ss/
[root@c3 ~]# gdrive sync upload /root/repo/handbook 1Pqx6bD2IwZXJniUSFykdPnFokuueHS3G
Starting sync...
Collecting local and remote file information...
Found 888 local files and 0 remote files

325 remote directories are missing
[0001/0325] Creating directory blog-backup/post

具体可以看看此项目的 readme.md