Bash 用于将数据备份到 google 驱动器的脚本
Bash script to backup data to google drive
我想创建一个 bash 脚本来登录我的 google 驱动器帐户并备份我的主文件夹。我做了一些研究,我知道我们需要某种 OAuth 来使用 Google 驱动器 API,但我还是个新手,所以我想我可以从堆栈中获得一些可靠的指导社区。
我想你不需要重新发明轮子,使用 gdrive 就可以了
设置:
wget -O drive "https://drive.google.com/uc?id=0B3X9GlR6EmbnMHBMVWtKaEZXdDg"
mv drive /usr/sbin/drive
chmod 755 /usr/sbin/drive
现在,只需 运行 drive
即可启动身份验证过程。你会得到一个 link 这样的 来粘贴并浏览到你的网络浏览器:
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/auth?client_id=123456789123-7n0vf5akeru7on6o2fjinrecpdoe99eg.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
验证并为应用程序提供访问您的 Google 驱动器的权限,然后您将获得一个验证码以复制并粘贴回您的 shell:
Enter verification code: 4/9gKYAFAJ326XIP6JJHAEhs342t35LPiA5QGW0935GHWHy9
用法:
drive upload --file "/some/path/somefile.ext"
SRC:
Backing up a Directory to Google Drive on CentOS 7
注意:
如果你真的想构建一个 bash 脚本,看看这个 gist ,即:
- 自动从文件中收集 MIME 类型
- 上传多个文件
- 从文件名中删除目录前缀
- 适用于带空格的文件名
- 使用点文件进行配置和令牌
- 交互式配置
- 如果最后一个参数看起来像文件夹 ID,则上传到目标文件夹
- 更安静的输出
- 使用更长的命令行标志以提高可读性
- 通过将
curl_args="--limit-rate 500K"
添加到 $HOME/.gdrive.conf
来节流
#!/bin/bash
# based on https://gist.github.com/deanet/3427090
#
# useful $HOME/.gdrive.conf options:
# curl_args="--limit-rate 500K --progress-bar"
browser="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"
destination_folder_id=${@: -1}
if expr "$destination_folder_id" : '^[A-Za-z0-9]\{28\}$' > /dev/null
then
# all but last word
set -- "${@:0:$#}"
else
# upload to root
unset destination_folder_id
fi
if [ -e $HOME/.gdrive.conf ]
then
. $HOME/.gdrive.conf
fi
old_umask=`umask`
umask 0077
if [ -z "$username" ]
then
read -p "username: " username
unset token
echo "username=$username" >> $HOME/.gdrive.conf
fi
if [ -z "$account_type" ]
then
if expr "$username" : '^[^@]*$' > /dev/null || expr "$username" : '.*@gmail.com$' > /dev/null
then
account_type=GOOGLE
else
account_type=HOSTED
fi
fi
if [ -z "$password$token" ]
then
read -s -p "password: " password
unset token
echo
fi
if [ -z "$token" ]
then
token=`curl --silent --data-urlencode Email=$username --data-urlencode Passwd="$password" --data accountType=$account_type --data service=writely --data source=cURL "https://www.google.com/accounts/ClientLogin" | sed -ne s/Auth=//p`
sed -ie '/^token=/d' $HOME/.gdrive.conf
echo "token=$token" >> $HOME/.gdrive.conf
fi
umask $old_umask
for file in "$@"
do
slug=`basename "$file"`
mime_type=`file --brief --mime-type "$file"`
upload_link=`curl --silent --show-error --insecure --request POST --header "Content-Length: 0" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "https://docs.google.com/feeds/upload/create-session/default/private/full${destination_folder_id+/folder:$destination_folder_id/contents}?convert=false" --dump-header - | sed -ne s/"Location: "//p`
echo "$file:"
curl --request POST --output /dev/null --data-binary "@$file" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "$upload_link" $curl_args
done
我想创建一个 bash 脚本来登录我的 google 驱动器帐户并备份我的主文件夹。我做了一些研究,我知道我们需要某种 OAuth 来使用 Google 驱动器 API,但我还是个新手,所以我想我可以从堆栈中获得一些可靠的指导社区。
我想你不需要重新发明轮子,使用 gdrive 就可以了
设置:
wget -O drive "https://drive.google.com/uc?id=0B3X9GlR6EmbnMHBMVWtKaEZXdDg"
mv drive /usr/sbin/drive
chmod 755 /usr/sbin/drive
现在,只需 运行 drive
即可启动身份验证过程。你会得到一个 link 这样的 来粘贴并浏览到你的网络浏览器:
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/auth?client_id=123456789123-7n0vf5akeru7on6o2fjinrecpdoe99eg.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
验证并为应用程序提供访问您的 Google 驱动器的权限,然后您将获得一个验证码以复制并粘贴回您的 shell:
Enter verification code: 4/9gKYAFAJ326XIP6JJHAEhs342t35LPiA5QGW0935GHWHy9
用法:
drive upload --file "/some/path/somefile.ext"
SRC:
Backing up a Directory to Google Drive on CentOS 7
注意:
如果你真的想构建一个 bash 脚本,看看这个 gist ,即:
- 自动从文件中收集 MIME 类型
- 上传多个文件
- 从文件名中删除目录前缀
- 适用于带空格的文件名
- 使用点文件进行配置和令牌
- 交互式配置
- 如果最后一个参数看起来像文件夹 ID,则上传到目标文件夹
- 更安静的输出
- 使用更长的命令行标志以提高可读性
- 通过将
curl_args="--limit-rate 500K"
添加到$HOME/.gdrive.conf
来节流
#!/bin/bash
# based on https://gist.github.com/deanet/3427090
#
# useful $HOME/.gdrive.conf options:
# curl_args="--limit-rate 500K --progress-bar"
browser="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"
destination_folder_id=${@: -1}
if expr "$destination_folder_id" : '^[A-Za-z0-9]\{28\}$' > /dev/null
then
# all but last word
set -- "${@:0:$#}"
else
# upload to root
unset destination_folder_id
fi
if [ -e $HOME/.gdrive.conf ]
then
. $HOME/.gdrive.conf
fi
old_umask=`umask`
umask 0077
if [ -z "$username" ]
then
read -p "username: " username
unset token
echo "username=$username" >> $HOME/.gdrive.conf
fi
if [ -z "$account_type" ]
then
if expr "$username" : '^[^@]*$' > /dev/null || expr "$username" : '.*@gmail.com$' > /dev/null
then
account_type=GOOGLE
else
account_type=HOSTED
fi
fi
if [ -z "$password$token" ]
then
read -s -p "password: " password
unset token
echo
fi
if [ -z "$token" ]
then
token=`curl --silent --data-urlencode Email=$username --data-urlencode Passwd="$password" --data accountType=$account_type --data service=writely --data source=cURL "https://www.google.com/accounts/ClientLogin" | sed -ne s/Auth=//p`
sed -ie '/^token=/d' $HOME/.gdrive.conf
echo "token=$token" >> $HOME/.gdrive.conf
fi
umask $old_umask
for file in "$@"
do
slug=`basename "$file"`
mime_type=`file --brief --mime-type "$file"`
upload_link=`curl --silent --show-error --insecure --request POST --header "Content-Length: 0" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "https://docs.google.com/feeds/upload/create-session/default/private/full${destination_folder_id+/folder:$destination_folder_id/contents}?convert=false" --dump-header - | sed -ne s/"Location: "//p`
echo "$file:"
curl --request POST --output /dev/null --data-binary "@$file" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "$upload_link" $curl_args
done