树莓派外接硬盘同步onedrive照片

背景

从很多年前到现在的所有照片我都归拢到Microsoft365的1T云盘里了。
但是最近看到很多丢失的事例之后突然觉得需要在本地做一个备份了。
研究了一天,最终还是决定把荒废的RaspberryPi 3B和买了好几年没用的2T硬盘活动起来,发挥余热。

现在的照片同步模式:我跟老婆的手机都装onedrive客户端,时不时打开上传到onedrive上,现在已经积累400G。
现在加一层同步到本地移动硬盘--在树莓派里安装linux版的onedrive,定时把onedrive上更新内容同步到2T外接硬盘里。

网上找教程都很简单,但是最终实现过程中遇到一大堆坑,所以比想象花费较多时间。所以先讲最正常的配置步骤,在最后作为共享啰嗦遇到的坑。

RaspberryPi重装系统

【坑1】

OS下载路径

raspberry-pi-os-32-bit
下载到自己PC上,完成后保持zip格式。
下载完成后样子:2021-05-07-raspios-buster-armhf.zip
【坑2--armhf】

工具下载路径

写入工具
下载到自己PC上,安装。
下载完成后样子:
imager_1.6.2.exe

刻录系统到tf卡

CHOOSE OS →Use custom →Open 上面下载的zip→WRITE

image.png

image.png
image.png

第一次登录

这时还不能SSH连接,所以树莓派需要连接鼠标,键盘,显示器来设定。

打开SSH

Raspberry Pi 默认 SSH 是关闭状态。通过以下几个步骤就可以打开 SSH 了。

  1. Preferences – Raspberry Pi Configuration
  2. Interfaces tab
  3. Enable SSH

打开VNC

  1. Preferences – Raspberry Pi Configuration
  2. Interfaces tab
  3. Enable VNC

访问端PC上安装RealVNC
下载链接

打开RealVNC ,输入RaspberryPi 的局域网IP,账号密码(默认值:pi/raspberry)就可以连接上了。
通过vnc连接时,可以通过 vncserver-virtual -geometry 1920×1080 :1来临时调节分辨率。

连接wifi,设定时区,修改初始密码等等,根据需要设定即可。

必要设定

swap扩容

onedrive编译时比较耗内存,如果swap保持默认的100M的话,会很耗费时间。

  • 查看所有swap
# swapon --show
NAME      TYPE SIZE  USED PRIO
/var/swap file 100M 18.5M   -2
  • 修改swap默认大小【坑3】
# vim /etc/dphys-swapfile

修改前:CONF_SWAPSIZE=100
修改后:CONF_SWAPSIZE=2048

  • 重启
# shutdown -r now

安装onedrive

【坑4】

  • 安装和编译onedrive
# apt-get install libcurl4-openssl-dev
# apt-get install libsqlite3-dev
# apt-get install libxml2
# apt-get install pkg-config
# wget https://github.com/ldc-developers/ldc/releases/download/v1.16.0/ldc2-1.16.0-linux-armhf.tar.xz
# tar -xvf ldc2-1.16.0-linux-armhf.tar.xz
# cd ldc2-1.16.0-linux-armhf/
~/ldc2-1.16.0-linux-armhf# pwd
/root/ldc2-1.16.0-linux-armhf
# cd ..
# git clone https://github.com/abraunegg/onedrive.git
# cd onedrive
# ./configure DC=/root/ldc2-1.16.0-linux-armhf/bin/ldmd2
# make clean; make;
# make install
  • 启动
# onedrive
Configuring Global Azure AD Endpoints
Authorize this app visiting:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=XXXXXXXXXX-12c519384f0c&scope=Files.ReadWrite%20Files.ReadWrite.all%20Sites.Read.All%20Sites.ReadWrite.All%20offline_access&response_type=code&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient
# 将上面url拷贝到浏览器中执行认证,认证完成后浏览器会变成空白。拷贝此时的url到下面位置
Enter the response uri: 

Application has been successfully authorised, however no additional command switches were provided.

Please use --help for further assistance in regards to running this application.

  • 查看配置
# onedrive --display-config
onedrive version                       = v2.4.13-13-ga73c911
Config path                            = /root/.config/onedrive
Config file found in config path       = false
Config option 'check_nosync'           = false
Config option 'sync_dir'               = /root/OneDrive  # 这个位置会挂载移动硬盘
Config option 'skip_dir'               =
Config option 'skip_file'              = ~*|.~*|*.tmp
Config option 'skip_dotfiles'          = false
Config option 'skip_symlinks'          = false
Config option 'monitor_interval'       = 300
Config option 'min_notify_changes'     = 5
Config option 'log_dir'                = /var/log/onedrive/
Config option 'classify_as_big_delete' = 1000
Config option 'upload_only'            = false
Config option 'no_remote_delete'       = false
Config option 'remove_source_files'    = false
Config option 'sync_root_files'        = false
Selective sync 'sync_list' configured  = false
Business Shared Folders configured     = false

Raspberry Pi绑定外接硬盘

# fdisk -l | grep /dev/sda
Disk /dev/sda: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors
/dev/sda1             2048 1023442943 1023440896  488G  7 HPFS/NTFS/exFAT
/dev/sda2       1023442944 3907024895 2883581952  1.4T  7 HPFS/NTFS/exFAT
root@raspberrypi:/# apt-get  install ntfs-3g
root@raspberrypi:/# blkid
/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="7616-4FD8" TYPE="vfat" PARTUUID="6e16372b-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="87b585d1-84c3-486a-8f3d-77cf16f84f30" TYPE="ext4" PARTUUID="6e16372b-02"
/dev/mmcblk0: PTUUID="6e16372b" PTTYPE="dos"
/dev/sda1: LABEL="backup" UUID="B0DC-6E14" TYPE="exfat" PARTUUID="23336cb2-01"
/dev/sda2: LABEL="4synconedrive" UUID="200603E80603BE32" TYPE="ntfs" PARTUUID="23336cb2-02"
# mkdir /root/OneDrive
# vim /etc/fstab
追加下面一行
UUID="200603E80603BE32"    /root/OneDrive    ntfs-3g      async,auto,dev,exec,gid=65534,rw,uid=65534,umask=000    0    0
# mount -a
# df -h
file   size  used  avai used% mount
/dev/root         14G  6.6G  6.8G   50% /
devtmpfs         333M     0  333M    0% /dev
tmpfs            462M     0  462M    0% /dev/shm
tmpfs            462M  6.3M  456M    2% /run
tmpfs            5.0M  4.0K  5.0M    1% /run/lock
tmpfs            462M     0  462M    0% /sys/fs/cgroup
/dev/mmcblk0p1   253M   49M  204M   20% /boot
tmpfs             93M     0   93M    0% /run/user/1000
/dev/sda2        1.4T  144M  1.4T    1% /root/OneDrive
# cd /root/OneDrive/

开始同步

# onedrive --synchronize

后台执行

# nohup  onedrive --synchronize &

未完成

1:Raspberry Pi 3B说可以连接5G WIFI。简单调查一下,实现不难。
2:怎么让数据更新只下载不上传,需要研究一下onedrive的使用。

坑们

  • 坑1
    考虑到RaspberryPi 3B是64位CPU,所以最早就一直装ubuntu。
    但是在安装onedrive过程中使用网上教程一直出错。
    猜想各种原因,尝试安装各种软件和桌面,最后系统乱掉了。所以干脆从0开始装系统。
    但是据说还是官方的系统对树莓派支持更好,优化更好,稳定性更好,所以也就不纠结32位还是64位了。

  • 坑2--armhf:
    在编译onedrive时,明明有对应的编译工具路径,但是就是提示路径不存在的error。
    后来才知道【arm64】【armhf】两种区别。主要跟系统和CPU有关系。
    比如2B以前CPU和系统的原因肯定都是【armhf】。
    网上都说3B已经是64位CPU了,所以一定是【arm64】。其实这是不正确的。如果在3B上装64位ubuntu,那么是【arm64】,
    但是就如这篇文章的事实来看,3B上装32位raspberry-pi-os-32-bit就是【armhf】。

  • 坑3
    ubuntu20.04系统默认没有swap(难道我以前自己删除了?),所以按照下面步骤是可以给swap扩容的。

# fallocate -l 1G /swapfile
# chmod 600 /swapfile  
# mkswap /swapfile
# swapon /swapfile
# swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

Raspberry Pi OS有默认的100M swap空间。
如果按照上面步骤,可以给默认的[/var/swap/]扩容至2048M。
但是在追加近fstab时,发现了有意思的注释。

root@raspberrypi:~/OneDrive# cat /etc/fstab
proc            /proc           proc    defaults          0       0
PARTUUID=6e16372b-01  /boot           vfat    defaults          0       2
PARTUUID=6e16372b-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

根据实验结果,及时我把扩容后的2048添加到fstab中,系统重启后也是无效的。
因为根据上面的注释可以判断 Raspberry Pi OS swap的控制是[/etc/dphys-swapfile]

  • 坑4
    在ubuntu上一直编译onedrive不成功,所以就猜想有没有不编译的方法。
    随便测试了一下 apt install onedrive 竟然真的可以安装。
    这也就引发了后面onedrive启动一直认证不过的错误。
    最终解决方案还是回归编译onedrive的正确做法。
    相关解释:https://github.com/skilion/onedrive/issues/512

你可能感兴趣的:(树莓派外接硬盘同步onedrive照片)