使用 gphotos-sync 备份 Google 相册

9 Comments

2019年7月 Google 更新政策,Google Photos 与 Google Drive 不会双向同步,之前通过 Google Drive,使用 rclone 备份 Google Photos 这个方法就失效了。在博友那里看到了新工具:gphotos-sync · GitHub

折腾一圈,发现 gphotos-sync 无法直接保存照片到使用 rclone 挂载的 OneDrive 上。放弃……

接着找大硬盘 VPS,不得不说,BuyVM 这货真心便宜。

安装 Python 3

gphotos-sync 需要 Python 3,CentOS 自带 Python 2,得安装,方法参考:CentOS 7 编译安装 Python 3.7.4

嫌麻烦的话还是一键脚本安装吧,比如:Python 3.6一键安装脚本 for CentOS/Debian

#CentOS系统
wget https://www.moerats.com/usr/shell/Python3/CentOS_Python3.6.sh && sh CentOS_Python3.6.sh

#Debian系统
wget https://www.moerats.com/usr/shell/Python3/Debian_Python3.6.sh && sh Debian_Python3.6.sh

安装 gphotos-sync

官网:Google Photos Sync,目前版本 2.9.2。执行下面命令安装即可:

pip3 install gphotos-sync

安装完成,执行 gphotos-sync --help 就能看到帮助信息▼展开

# gphotos-sync --help
usage: gphotos-sync [-h] [--album ALBUM] [--logfile LOGFILE]
[--compare-folder COMPARE_FOLDER] [--favourites-only]
[--flush-index] [--rescan] [--retry-download]
[--skip-video] [--skip-shared-albums]
[--start-date START_DATE] [--end-date END_DATE]
[--log-level LOG_LEVEL] [--db-path DB_PATH]
[--albums-path ALBUMS_PATH] [--photos-path PHOTOS_PATH]
[--use-flat-path] [--new-token] [--index-only]
[--skip-index] [--do-delete] [--skip-files]
[--skip-albums] [--get-locations] [--use-hardlinks]
[--no-album-index]
root_folder

Google Photos download tool

positional arguments:
root_folder root of the local folders to download into

optional arguments:
-h, --help show this help message and exit
--album ALBUM only synchronize the contents of a single album.use
quotes e.g. "album name" for album names with spaces
--logfile LOGFILE full path to debug level logfile, default:
<root>/gphotos.log.If a directory is specified then a
unique filename will begenerated.
--compare-folder COMPARE_FOLDER
root of the local folders to compare to the Photos
Library
--favourites-only only download media marked as favourite (star)
--flush-index delete the index db, re-scan everything
--rescan rescan entire library, ignoring last scan date. Use
this if you have added photos to the library that
predate the last sync, or you have deleted some of the
local files
--retry-download check for the existence of files marked as already
downloaded and re-download any missing ones. Use this
if you have deleted some local files
--skip-video skip video types in sync
--skip-shared-albums skip albums that only appear in 'Sharing'
--start-date START_DATE
Set the earliest date of files to syncformat YYYY-MM-
DD
--end-date END_DATE Set the latest date of files to syncformat YYYY-MM-DD
--log-level LOG_LEVEL
Set log level. Options: critical, error, warning,
info, debug
--db-path DB_PATH Specify a pre-existing folder for the index database.
Defaults to the root of the local download folders
--albums-path ALBUMS_PATH
Specify a folder for the albums Defaults to the
'albums' in the local download folders
--photos-path PHOTOS_PATH
Specify a folder for the photo files. Defaults to the
'photos' in the local download folders
--use-flat-path mandate use of a flat directory structure ('YYYY-MMM')
and not a nested one ('YYYY/MM') .
--new-token Request new token
--index-only Only build the index of files in .gphotos.db - no
downloads
--skip-index Use index from previous run and start download
immediately
--do-delete Remove local copies of files that were deleted. Must
be used with --flush-index since the deleted items
must be removed from the index
--skip-files Dont download files, just refresh the album links (for
testing)
--skip-albums Dont download albums (for testing)
--get-locations Scrape the Google Photos website for location metadata
and add it to the local files' EXIF metadata
--use-hardlinks Use hardlinks instead of symbolic links in albums and
comparison folders
--no-album-index only index the photos library - skip indexing of
folder contents (for testing)

version: 2.9.2, database schema version 5.6

申请 Google Photos 访问 API client id

创建项目

打开 Google Developer Console 控制台,创建一个新项目,如:google-photos-sync

 1095-google-new-project

在 google-photos-sync 项目中激活 Photos Library API

进入 API 库,搜索 Google Photos,激活 Photos Library API。

 1095-google-photos-api

创建 OAuth 同意屏幕

填写应用名称,然后保存即可。(其余信息不用理会)

 1095-google-screen-auth

创建 OAuth 客户端 ID

凭据(Credentials)菜单,创建「OAuth 客户端 ID」(OAuth client ID)凭证。

 1095-google-new-credentials

「应用类型」一定要选择「其他(Other)」,点击创建。

 1095-google-client-id-others

创建完成,点击凭证右侧下载按钮把 json 格式凭证信息下载并重命名为:client_secret.json

 1095-google-download-json

client_secret.json 上传到指定路径

这个文件,不同操作系统类型,放置到不同目录:

  • Mac OS X:~/Library/Application Support/gphotos-sync/
  • Linux:~/.config/gphotos-sync/
  • Windows:C:\Users\\AppData\Local\gphotos-sync\gphotos-sync\

启动 gphotos-sync 备份 GooglePhotos 相册

gphotos-sync /mnt/googlephotos

gphotos-sync 一般安装在 /usr/local/bin/ 目录,如果上述命令无法执行,可换成:

/usr/local/bin/gphotos-sync /mnt/googlephotos

其中 /mnt/googlephotos 是保存 Google 照片的目录,自己指定一个就行。

首次启动,会提示 Google 账号授权,根据提示信息的网址,复制到浏览器打开后进行授权,把生成的 token 填写到命令窗口即可。

 1095-google-first-auth

如无意外,gphotos-sync 就开始进行索引、下载、备份照片,以年月的形式来保存照片。第一次同步实际可能会比较长。

然后可以添加定时任务,自动备份,比如,每天凌晨4点执行备份:

crontab -e
0 4 * * * /usr/local/bin/gphotos-sync /mnt/googlephotos  > /dev/null 2>&1 &

实测,定时执行命令加上 --flush-index --use-hardlinks 参数比较好。

0 4 * * * /usr/local/bin/gphotos-sync --flush-index --use-hardlinks /mnt/googlephotos  > /dev/null 2>&1 &

如有需要,还可以使用 rclone 将备份文件同步至 OneDrive

参考资料

Google Photos Sync 项目:gphotos-sync

Logix - How To Backup Google Photos To Your Computer With gphotos-sync(英文)

后记、更新

2019.8.27:Windows 下同步效果不理想,相册一个都没有同步下来,照片也只下载了很少一部分。(估计是 gbk 编码原因)

声明:除非注明,常阳时光文章均为原创
本文地址:https://cyhour.com/1095/
转载时必须以链接形式注明原始出处。
Views: 253 Tags:  ,  ,  ,  ,  , 

Comments:9

  1. 干货,不过没有用过这个东西

    2019.08.27 17:17 # 回复
    1楼
  2. 非技术的路过。

    2019.08.28 16:04 # 回复
    2楼
  3. 看不懂

    2019.08.29 12:39 # 回复
    3楼
  4. 浮游

    几天更新了这么多文章,更新速度真心佩服

    2019.08.31 06:15 # 回复
    4楼
  5. BuyVM 这一款Out of Stock

    2019.09.17 12:11 # 回复
    5楼

发表留言

Vultr 送$25 | 搬瓦工年付$19补货,优惠码 BWH34QMFYT2R | 推荐VPS信息