NAS照片备份-实战

NAS照片备份-实战

之前写的文章列举了我个人的NAS在实际生活中发挥到的作用,这篇文章主要分享一下我个人的NAS在备份家庭照片时的实施.

以下为我个人使用方案的分享,当然也有其他优秀的方案,这里不讨论不同方案的优劣

这里默认已经安装好了docker环境,实际上大部分NAS系统都已经预装了docker,只需要ssh连接到NAS执行命令即可

最小方案 - Pho + PhotoPrism

部署 PhotoPrism

PhotoPrism文档: https://docs.photoprism.app/getting-started/

这里采用docker-compose的形式部署服务,如果还没有安装docker-compose请参考文档先安装: https://docs.docker.com/compose/install/

准备docker-compose.yml

将以下内容保存为docker-compose.yml文件,放置于某个目录中

version: '3.5'
services:
  photoprism:
    image: photoprism/photoprism:latest
    depends_on:
      - mariadb
    restart: unless-stopped
    security_opt:
      - seccomp:unconfined
      - apparmor:unconfined
    ports:
      - "2342:2342" # HTTP port (host:container)
    environment:
      DOCKER_ENV: "prod"
      PHOTOPRISM_SPONSOR: "true" 
      PHOTOPRISM_ADMIN_PASSWORD: "YOUR-PASSWORD"          # INITIAL PASSWORD FOR "admin" USER, MINIMUM 8 CHARACTERS
      PHOTOPRISM_AUTH_MODE: "password"
      PHOTOPRISM_SITE_URL: "http://YOUR-NAS-IP:2342/"  # public server URL incl http:// or https:// and /path, :port is optional
      PHOTOPRISM_ORIGINALS_LIMIT: 5000
      PHOTOPRISM_HTTP_COMPRESSION: "gzip"
      PHOTOPRISM_LOG_LEVEL: "info"
      PHOTOPRISM_READONLY: "false"
      PHOTOPRISM_EXPERIMENTAL: "false"
      PHOTOPRISM_DISABLE_CHOWN: "false"
      PHOTOPRISM_DISABLE_WEBDAV: "false"
      PHOTOPRISM_DISABLE_SETTINGS: "false"
      PHOTOPRISM_DISABLE_TENSORFLOW: "false"
      PHOTOPRISM_DISABLE_FACES: "false"
      PHOTOPRISM_DISABLE_CLASSIFICATION: "false"
      PHOTOPRISM_DISABLE_RAW: "false"
      PHOTOPRISM_RAW_PRESETS: "false"
      PHOTOPRISM_JPEG_QUALITY: 85
      PHOTOPRISM_DETECT_NSFW: "false"
      PHOTOPRISM_UPLOAD_NSFW: "true"
      PHOTOPRISM_DATABASE_DRIVER: "mysql"
      PHOTOPRISM_DATABASE_SERVER: "mariadb:3306"
      PHOTOPRISM_DATABASE_NAME: "photoprism"
      PHOTOPRISM_DATABASE_USER: "photoprism"
      PHOTOPRISM_DATABASE_PASSWORD: "insecure"
      PHOTOPRISM_SITE_CAPTION: "AI-Powered Photos App"
      PHOTOPRISM_SITE_DESCRIPTION: ""
      PHOTOPRISM_SITE_AUTHOR: ""
      # PHOTOPRISM_INIT: "gpu tensorflow"
      ## Hardware Video Transcoding (for sponsors only due to high maintenance and support costs):
      # PHOTOPRISM_FFMPEG_ENCODER: "intel"        # FFmpeg encoder ("software", "intel", "nvidia", "apple", "raspberry")
      # PHOTOPRISM_FFMPEG_BITRATE: "500"              # FFmpeg encoding bitrate limit in Mbit/s (default: 50)
    ## Share hardware devices with FFmpeg and TensorFlow (optional):
    devices:
    #  - "/dev/dri:/dev/dri"                         # Intel QSV
    #  - "/dev/nvidia0:/dev/nvidia0"                 # Nvidia CUDA
    #  - "/dev/nvidiactl:/dev/nvidiactl"
    #  - "/dev/nvidia-modeset:/dev/nvidia-modeset"
    #  - "/dev/nvidia-nvswitchctl:/dev/nvidia-nvswitchctl"
    #  - "/dev/nvidia-uvm:/dev/nvidia-uvm"
    #  - "/dev/nvidia-uvm-tools:/dev/nvidia-uvm-tools"
    #  - "/dev/video11:/dev/video11"                 # Video4Linux Video Encode Device (h264_v4l2m2m)
    working_dir: "/photoprism"
    volumes:
      - "/mnt/user/media/photo2:/photoprism/originals/photo2"
      # - "/example/family:/photoprism/originals/family" # *Additional* media folders can be mounted like this
      # - "~/Import:/photoprism/import"                  # *Optional* base folder from which files can be imported to originals
      - "/mnt/user/media/photoprism/storage:/photoprism/storage"                  # *Writable* storage folder for cache, database, and sidecar files (DO NOT REMOVE)

  ## Database Server (recommended)
  mariadb:
    restart: unless-stopped
    image: mariadb:10.8
    security_opt:
      - seccomp:unconfined
      - apparmor:unconfined
    command: mysqld --innodb-buffer-pool-size=512M --transaction-isolation=READ-COMMITTED --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --max-connections=512 --innodb-rollback-on-timeout=OFF --innodb-lock-wait-timeout=120
    volumes:
      - "/mnt/user/config-cache/photoprism/mariadb:/var/lib/mysql" # CHANGE TO YOUR NAS PATH
    environment:
      MARIADB_AUTO_UPGRADE: "1"
      MARIADB_INITDB_SKIP_TZINFO: "1"
      MARIADB_DATABASE: "photoprism"
      MARIADB_USER: "photoprism"
      MARIADB_PASSWORD: "insecure"
      MARIADB_ROOT_PASSWORD: "insecure"

修改文件中的以下项:

  1. 修改密码
PHOTOPRISM_ADMIN_PASSWORD: "YOUR-PASSWORD" # 修改为你自己的密码
  1. 修改IP地址
PHOTOPRISM_SITE_URL: "http://YOUR-NAS-IP:2342/" # 修改为你自己的NAS的IP地址
  1. 修改储存目录
    你需要先在你的NAS上给PhotoPrism新建一个目录用来储存它的一些信息,否则当docker容器销毁后你的配置信息也会丢失
    这里我用的是/mnt/user/media/photoprism/storage目录,你可以自行修改
volumes:
  - "/mnt/user/media/photoprism/storage:/photoprism/storage"
  - "/mnt/user/media/photo2:/photoprism/originals/photo-ip11" # 将你NAS上储存照片的目录映射到容器/photoprism/originals/目录中
  1. 修改数据库目录
    同样的,你需要先在你的NAS上给PhotoPrism新建一个目录用来储存它的数据库信息,否则当docker容器销毁后你的配置信息也会丢失
volumes:
  - "/mnt/user/config-cache/photoprism/mariadb:/var/lib/mysql" # CHANGE TO YOUR NAS PATH
  1. 如果要开启硬件加速,需要取消注释以下项,不开启直接跳过这一步
PHOTOPRISM_INIT: "gpu tensorflow"
# 根据你的硬件选择以下项,这里以intel处理器为例
PHOTOPRISM_FFMPEG_ENCODER: "intel" # FFmpeg encoder ("software", "intel", "nvidia", "apple", "raspberry")
devices:
 - "/dev/dri:/dev/dri"                         # Intel QSV
#  - "/dev/nvidia0:/dev/nvidia0"                 # Nvidia CUDA
#  - "/dev/nvidiactl:/dev/nvidiactl"
#  - "/dev/nvidia-modeset:/dev/nvidia-modeset"
#  - "/dev/nvidia-nvswitchctl:/dev/nvidia-nvswitchctl"
#  - "/dev/nvidia-uvm:/dev/nvidia-uvm"
#  - "/dev/nvidia-uvm-tools:/dev/nvidia-uvm-tools"
#  - "/dev/video11:/dev/video11"                 # Video4Linux Video Encode Device (h264_v4l2m2m)

启动服务

进入到docker-compose.yml所在目录,执行以下命令启动服务

docker-compose up -d

如果一切正常,可以通过docker ps命令看到类似以下命名的两个容器:

$ docker ps
photoprism-photoprism-1
photoprism-mariadb-1

你可以进一步使用docker logs命令查看两个容器的运行日志,看有没有错误

docker logs photoprism-photoprism-1
docker logs photoprism-mariadb-1

看到logs photoprism-photoprism-1容器出现类似这条输出说明服务已经启动成功,你可以通过http://YOUR-NAS-IP:2342/访问PhotoPrism了

time="2023-09-06T09:44:45Z" level=info msg="server: listening on 0.0.0.0:2342 [1.429356ms]"

配置PhotoPrism

使用刚才的密码在浏览器中登录PhotoPrism,登录后点击资料库 -> 索引,输入你映射进容器存放照片的目录,比如我是这样映射的:"/mnt/user/media/photo2:/photoprism/originals/photo-ip11",我这里就索引/photo-ip11即可
photoprism-repo
点击开始后耐心等待PhotoPrism索引和分析照片,这个过程的长短取决于你照片的数量和照片的大小,完成索引后即可从各个维度去查找和浏览你的照片了:
photoprism-repo

使用Pho同步照片

Pho官网: https://pho.tools

  1. 将你刚才映射到PhotoPrism的目录通过网络共享出来,使用SMB\Webdav\NFS都可以,由于不同的NAS系统可能操作方式不同,这里就不详细介绍了,下面以SMB为例.
  2. 在手机上安装Pho app并打开,切换到同步页面
  1. 点击本地相册,并选择你想要同步的手机上的相册
  2. 点击云端设置,根据你的网络储存信息填写好配置并在测试连接成功后保存
  1. 点击同步旁边的刷新按钮,Pho会自动比对你选中的相册和你的储存空间中已有的照片,找出还未上传的照片并显示.
  2. 刷新后点击同步按钮则会自动上传所有还未备份的照片,耐心等待即可
  3. 上传后即可在云端页面查看已上传的照片,在本地页面能查看本地相册的照片
  1. 在PhotoPrism的资料库中,要点击开始,索引新上传的文件后才可以在PhotoPrism中查看到新上传的照片

附加需求一 - 通过Alist同步到网盘

有时候重要的数据备份一份在NAS可能还是会有隐患,于是某些重要的照片我会选在阿里云盘和夸克网盘(88VIP送会员,不用白不用)做二次备份

Alist部署

Alist的部署方式在我之前的实战教程中有写,这里不详细说明了,可以看这里

部署好通过webdav的形式读写其中的照片: http://your-nas-ip:5244/dav

通过Pho加密上传

不想我的照片被网盘扫描,于是我在Pho ios app中增加了加密上传的功能,设置好加密方式和密码后就可以上传加密后的照片到网盘了,这样即使网盘被扫描也不会泄露我的隐私,同时在app里也能正常浏览和查看照片.

附加需求二 - 使用rclone让PhotoPrsim读取网盘中的照片

注: 这个需求如果开启了Pho的加密上传的话则不能实现

使用rclone将alist的webdav映射到NAS的目录下

详细操作方式见我的另一篇文章

修改PhotoPrsim

  1. 修改docker-compose.yml文件,在photoprism容器的volumes部分增加你在上一步用rclone映射的目录`
volumes:
  - "/mnt/user/media/photoprism/storage:/photoprism/storage"
  - "/mnt/user/media/photo2:/photoprism/originals/photo-ip11"
  - "/mnt/user/media/alist-netdisk:/photoprism/originals/alist-netdisk" # 这里是你用rclone映射的目录
  1. 重启PhotoPrism容器
docker-compose restart photoprism
  1. 在PhotoPrism的资料库中索引新添加的目录

看看其他

结束

注: 如果文章对大家有所帮助,可以通过使用或宣传Pho来支持,我会继续更新一些此类主题的实战教程.
本文作者即为Pho的个人开发者.