如何自动将定期 Google 外卖备份到云存储

Mic*_*jer 51 backup dropbox automation amazon-s3

我想创建定期的Google Takeout备份(假设每 3 个月一次)并将它们加密存储在其他一些云存储中,如 DropBox 或 S3。

尽管首选,但它不一定是云到云的解决方案。它不必是 100% 自动化,但越多越好。

Del*_*tik 7

这是部分自动化的部分答案。如果谷歌选择打击对谷歌外卖的自动访问,它可能会在未来停止工作。此答案中当前支持的功能:

+---------------------------------------------------------+--- ---------+---------------------+
| 自动化功能 | 自动化?| 支持的平台 |
+---------------------------------------------------------+--- ---------+---------------------+
| 谷歌账户登录 | 没有 | |
| 从 Mozilla Firefox 获取 cookie | 是 | Linux |
| 从 Google Chrome 获取 cookie | 是 | Linux、macOS |
| 请求归档创建 | 没有 | |
| 计划存档创建 | 有点| 外卖网站 |
| 检查存档是否已创建 | 没有 | |
| 获取存档列表 | 破碎 | 跨平台 |
| 下载所有存档文件 | 破碎 | Linux、macOS |
| 加密下载的存档文件 | 没有 | |
| 将下载的存档文件上传到 Dropbox | 没有 | |
| 将下载的存档文件上传到 AWS S3 | 没有 | |
+---------------------------------------------------------+--- ---------+---------------------+

首先,云到云解决方案无法真正发挥作用,因为 Google Takeout 和任何已知的对象存储提供商之间没有接口。在将备份文件发送给对象存储提供商之前,您必须在自己的机器上处理备份文件(如果需要,可以托管在公共云中)。

其次,由于没有谷歌外卖 API,自动化脚本需要假装是浏览器的用户来完成谷歌外卖档案的创建和下载流程。


自动化功能

谷歌账户登录

这还没有自动化。该脚本需要伪装成浏览器并解决可能的障碍,例如双因素身份验证、CAPTCHA 和其他增强的安全筛选。

从 Mozilla Firefox 获取 cookie

我有一个脚本供 Linux 用户从 Mozilla Firefox 获取 Google Takeout cookie 并将它们导出为环境变量。为此,默认/活动配置文件必须在登录时访问过https://takeout.google.com

作为单线:

+---------------------------------------------+------------+---------------------+
|             Automation Feature              | Automated? | Supported Platforms |
+---------------------------------------------+------------+---------------------+
| Google Account log-in                       | No         |                     |
| Get cookies from Mozilla Firefox            | Yes        | Linux               |
| Get cookies from Google Chrome              | Yes        | Linux, macOS        |
| Request archive creation                    | No         |                     |
| Schedule archive creation                   | Kinda      | Takeout website     |
| Check if archive is created                 | No         |                     |
| Get archive list                            | Broken     | Cross-platform      |
| Download all archive files                  | Broken     | Linux, macOS        |
| Encrypt downloaded archive files            | No         |                     |
| Upload downloaded archive files to Dropbox  | No         |                     |
| Upload downloaded archive files to AWS S3   | No         |                     |
+---------------------------------------------+------------+---------------------+

作为一个更漂亮的 Bash 脚本:

cookie_jar_path=$(mktemp) ; source_path=$(mktemp) ; firefox_profile=$(cat "$HOME/.mozilla/firefox/profiles.ini" | awk -v RS="" '{ if($1 ~ /^\[Install[0-9A-F]+\]/) { print } }' | sed -nr 's/^Default=(.*)$/\1/p' | head -1) ; cp "$HOME/.mozilla/firefox/$firefox_profile/cookies.sqlite" "$cookie_jar_path" ; sqlite3 "$cookie_jar_path" "SELECT name,value FROM moz_cookies WHERE host LIKE '%.google.com' AND (name LIKE 'SID' OR name LIKE 'HSID' OR name LIKE 'SSID' OR (name LIKE 'OSID' AND host LIKE 'takeout.google.com')) AND originAttributes LIKE '^userContextId=1' ORDER BY creationTime ASC;" | sed -e 's/|/=/' -e 's/^/export /' | tee "$source_path" ; source "$source_path" ; rm -f "$source_path" ; rm -f "$cookie_jar_path"
Run Code Online (Sandbox Code Playgroud)

从 Google Chrome 获取 cookie

我有一个适用于 Linux 和可能的 macOS 用户的脚本,用于从 Google Chrome 获取 Google Takeout cookie 并将它们导出为环境变量。该脚本假设 Python 3venv可用并且DefaultChrome 配置文件在登录时访问了https://takeout.google.com

作为单线:

#!/bin/bash
# Extract Google Takeout cookies from Mozilla Firefox and export them as envvars
#
# The browser must have visited https://takeout.google.com as an authenticated user.

# Warn the user if they didn't run the script with `source`
[[ "${BASH_SOURCE[0]}" == "${0}" ]] &&
       echo 'WARNING: You should source this script to ensure the resulting environment variables get set.'
  
cookie_jar_path=$(mktemp)
source_path=$(mktemp)

# In case the cookie database is locked, copy the database to a temporary file.
# Edit the $firefox_profile variable below to select a specific Firefox profile.
firefox_profile=$(
    cat "$HOME/.mozilla/firefox/profiles.ini" |
    awk -v RS="" '{
        if($1 ~ /^\[Install[0-9A-F]+\]/) {
            print
        }
    }' |
    sed -nr 's/^Default=(.*)$/\1/p' |
    head -1
)
cp "$HOME/.mozilla/firefox/$firefox_profile/cookies.sqlite" "$cookie_jar_path"

# Get the cookies from the database
sqlite3 "$cookie_jar_path" \
       "SELECT name,value
        FROM moz_cookies
        WHERE host LIKE '%.google.com'
        AND (
                name LIKE 'SID' OR
                name LIKE 'HSID' OR
                name LIKE 'SSID' OR
                (name LIKE 'OSID' AND host LIKE 'takeout.google.com')
        ) AND
        originAttributes LIKE '^userContextId=1'
        ORDER BY creationTime ASC;" |
                # Reformat the output into Bash exports
                sed -e 's/|/=/' -e 's/^/export /' |
                # Save the output into a temporary file
                tee "$source_path"

# Load the cookie values into environment variables
source "$source_path"

# Clean up
rm -f "$source_path"
rm -f "$cookie_jar_path"
Run Code Online (Sandbox Code Playgroud)

作为一个更漂亮的 Bash 脚本:

if [ ! -d "$venv_path" ] ; then venv_path=$(mktemp -d) ; fi ; if [ ! -f "${venv_path}/bin/activate" ] ; then python3 -m venv "$venv_path" ; fi ; source "${venv_path}/bin/activate" ; python3 -c 'import pycookiecheat, dbus' ; if [ $? -ne 0 ] ; then pip3 install git+https://github.com/n8henrie/pycookiecheat@dev dbus-python ; fi ; source_path=$(mktemp) ; python3 -c 'import pycookiecheat, json; cookies = pycookiecheat.chrome_cookies("https://takeout.google.com") ; [print("export %s=%s;" % (key, cookies[key])) for key in ["SID", "HSID", "SSID", "OSID"]]' | tee "$source_path" ; source "$source_path" ; rm -f "$source_path" ; deactivate
Run Code Online (Sandbox Code Playgroud)

清理下载的文件:

#!/bin/bash
# Extract Google Takeout cookies from Google Chrome and export them as envvars
#
# The browser must have visited https://takeout.google.com as an authenticated user.

# Warn the user if they didn't run the script with `source`
[[ "${BASH_SOURCE[0]}" == "${0}" ]] &&
       echo 'WARNING: You should source this script to ensure the resulting environment variables get set.'

# Create a path for the Chrome cookie extraction library
if [ ! -d "$venv_path" ]
then
       venv_path=$(mktemp -d)
fi

# Create a Python 3 venv, if it doesn't already exist
if [ ! -f "${venv_path}/bin/activate" ]
then
        python3 -m venv "$venv_path"

fi

# Enter the Python virtual environment
source "${venv_path}/bin/activate"

# Install dependencies, if they are not already installed
python3 -c 'import pycookiecheat, dbus'
if [ $? -ne 0 ]
then
        pip3 install git+https://github.com/n8henrie/pycookiecheat@dev dbus-python
fi

# Get the cookies from the database
source_path=$(mktemp)
read -r -d '' code << EOL
import pycookiecheat, json
cookies = pycookiecheat.chrome_cookies("https://takeout.google.com")
for key in ["SID", "HSID", "SSID", "OSID"]:
        print("export %s=%s" % (key, cookies[key]))
EOL
python3 -c "$code" | tee "$source_path"

# Clean up
source "$source_path"
rm -f "$source_path"
deactivate
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && rm -rf "$venv_path"
Run Code Online (Sandbox Code Playgroud)

请求归档创建

这还没有自动化。该脚本需要填写 Google 外卖表格,然后提交。

计划存档创建

目前还没有完全自动化的方法来做到这一点,但在 2019 年 5 月,Google Takeout 推出了一项功能,可以在 1 年内每 2 个月自动创建 1 个备份(总共 6 个备份)。这必须在https://takeout.google.com的浏览器中完成,同时填写存档请求表:

Google 外卖:自定义存档格式

检查存档是否已创建

这还没有自动化。如果已创建存档,Google 有时会向用户的 Gmail 收件箱发送一封电子邮件,但在我的测试中,由于未知原因,这并不总是发生。

检查存档是否已创建的唯一其他方法是定期轮询 Google Takeout。

获取存档列表

此部分目前已损坏。

谷歌停止在外卖下载页面上显示档案下载链接,并实施了一个安全令牌,将每个档案的下载链接检索限制为最多 5 次。

我有一个命令来执行此操作,假设 cookie 已在上面的“获取 cookie”部分中设置为环境变量:

curl -sL -H "Cookie: SID=${SID}; HSID=${HSID}; SSID=${SSID}; OSID=${OSID};" \
'https://takeout.google.com/settings/takeout/downloads' |
grep -Po '(?<=")https://storage.cloud.google.com/[^"]+(?=")' |
awk '!x[$0]++'

输出是以行分隔的 URL 列表,可用于下载所有可用档案。
它是用 regex 从 HTML 解析出来的

下载所有存档文件

此部分目前已损坏。

谷歌停止在外卖下载页面上显示档案下载链接,并实施了一个安全令牌,将每个档案的下载链接检索限制为最多 5 次。

以下是 Bash 中的代码,用于获取存档文件的 URL 并将它们全部下载,假设 cookie 已在上面的“获取 cookie”部分中设置为环境变量:

curl -sL -H "Cookie: SID=${SID}; HSID=${HSID}; SSID=${SSID}; OSID=${OSID};" \
'https://takeout.google.com/settings/takeout/downloads' |
grep -Po '(?<=")https://storage.cloud.google.com/[^"]+(?=")' |
awk '!x[$0]++' |
xargs -n1 -P1 -I{} curl -LOJ -C - -H "Cookie: SID=${SID}; HSID=${HSID}; SSID=${SSID}; OSID=${OSID};" {}

我已经在 Linux 上测试过它,但语法也应该与 macOS 兼容。

各部分说明:

  1. curl 带有身份验证 cookie 的命令:

    curl -sL -H "Cookie: SID=${SID}; HSID=${HSID}; SSID=${SSID}; OSID=${OSID};" \
  2. 包含下载链接的页面的 URL

    'https://takeout.google.com/settings/takeout/downloads' |
  3. 过滤器只匹配下载链接

    grep -Po'(?
    
  4. 过滤掉重复链接

    awk '!x[$0]++' |
  5. 一一下载列表中的每个文件:

    xargs -n1 -P1 -I{} curl -LOJ -C - -H "Cookie: SID=${SID}; HSID=${HSID}; SSID=${SSID}; OSID=${OSID};" {}

    注意:并行下载(更改-P1为更高的数字)是可能的,但 Google 似乎限制了除一个连接之外的所有连接。

    注意: -C -跳过已经存在的文件,但它可能无法成功恢复现有文件的下载。

加密下载的存档文件

这不是自动化的。实现取决于您喜欢如何加密文件,并且您要加密的每个文件的本地磁盘空间消耗必须加倍。

将下载的存档文件上传到 Dropbox

这还没有自动化。

将下载的存档文件上传到 AWS S3

这还不是自动化的,但它应该只是迭代下载文件列表并运行如下命令:

aws s3 cp TAKEOUT_FILE "s3://MYBUCKET/Google Takeout/"


har*_*ymc 5

Google Takeout 可让您每两个月安排一次出口,因此一年六次,最长一年。您可以选择将文件添加到云驱动器或通过电子邮件发送下载链接(下载仅保留一周)。

要对其进行编程,请浏览到https://takeout.google.com/settings/takeout?pli=1的页面。

您可以选择要包含在备份中的 Google 数据。支持的云驱动器有:Drive、Dropbox、OneDrive 和 Box。转储格式为 Zip 或 tgz。

您可以在如何下载您的 Google 数据一文中找到更多信息 。


由于 Google 外卖不提供 API,因此当其用户界面发生变化时,通过浏览器自动启动此类备份可能不起作用。

最好使用 Google Takeout 备份到某个云盘,并自动下载新文件。

您可以参考我的 这个答案 ,了解访问 Google Drive 进行同步的方法。可能可以将 Google Drive 映射到 Windows,因此使用 Windows 任务将新备份同步到本地磁盘(尽管我还没有尝试过)。


kro*_*owe 2

您可以通过 Google Drive 将数据备份到第三方存储解决方案,而不是使用直接 API 来备份 Google Takeout(目前这似乎几乎不可能做到)。许多 Google 服务都允许备份到 Google Drive,您可以使用以下工具备份 Google Drive:

GoogleCL - GoogleCL 将 Google 服务引入命令行。

gdatacopier - Google 文档的命令行文档管理实用程序。

FUSE Google Drive - Google Drive 的 FUSE 用户空间文件系统,用 C 编写。

Grive - Google Drive 客户端的独立开源实现。它使用 Google 文档列表 API 与 Google 中的服务器进行通信。该代码是用 C++ 编写的。

gdrive-cli - GDrive 的命令行界面。这使用了 GDrive API,而不是 GDocs API,这很有趣。要使用它,您需要注册一个 Chrome 应用程序。它必须至少可以由您安装,但不需要发布。存储库中有一个样板应用程序,您可以将其用作起点。

python-fuse 示例- 包含 Python FUSE 文件系统的一些幻灯片和示例。

其中大部分似乎都在 Ubuntu 存储库中。我自己使用过 Fuse、gdrive 和 GoogleCL,它们都工作得很好。根据您想要的控制级别,这将非常简单或非常复杂。这取决于你。从 EC2/S3 服务器上应该可以直接完成。只需将您需要的所有命令一一找出,然后将其放入 cron 作业的脚本中即可。

如果你不想那么辛苦,你也可以使用像Spinbackup这样的服务。我确信还有其他人也同样好,但我还没有尝试过。

  • 谷歌外卖*是*最好的工具,因为它支持比其他工具更多的服务。这个问题是有效的。 (22认同)
  • @krowe:你的答案非常有用,但它仅与谷歌驱动器相关。 Google 外卖可让您从 25 种不同的 Google 服务下载所有数据,而不仅仅是 Google 云端硬盘。 (6认同)
  • @krowe:Gmail、日历、通讯录、照片、环聊历史记录和位置历史记录是我广泛使用的服务,并且希望确保 Google 不会丢失数据。这些服务的数据均不包含在 Google 云端硬盘中。仅仅因为我不知道更好的解决方案,或者根本不存在更好的解决方案,并不会让您的答案更加正确。再说一次,我并不是说你的回答不好,它只是没有回答实际的问题。 (6认同)