树莓派 安装 Syncthing 自建私有云盘 照片备份 备份手机相册

手机上存着不少照片,自己又时常折腾手机,总有数据丢失的问题,又对市面上的云盘不怎么放心,所以打算在家里利用树莓派搭建一个私有云。一番查找之后,发现了 syncthing。Syncthing 是一个跨平台,开源且免费的基于 P2P 的文件同步解决方案,支持 Windows,Mac,Linux,Android,syncthing 官方暂不支持iOS平台。

安装 syncthing

sudo apt-get install syncthing

pi@pi:~ $ sudo apt-get install syncthing
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  syncthing
0 upgraded, 1 newly installed, 0 to remove and 328 not upgraded.
Need to get 0 B/5,241 kB of archives.
After this operation, 13.9 MB of additional disk space will be used.
Selecting previously unselected package syncthing.
(Reading database ... 104795 files and directories currently installed.)
Preparing to unpack .../syncthing_1.0.0~ds1-1_armhf.deb ...
Unpacking syncthing (1.0.0~ds1-1) ...
Setting up syncthing (1.0.0~ds1-1) ...
Processing triggers for mime-support (3.62) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.31.4-3) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for desktop-file-utils (0.23-4) ...
pi@pi:~ $syncthing

我们安装最新版本 v1.18.3

树莓派输入下面命令安装

wget https://github.com/syncthing/syncthing/releases/download/v1.18.3/syncthing-linux-arm-v1.18.3.tar.gz
tar -vxf syncthing-linux-arm-v1.18.3.tar.gz
sudo cp syncthing-linux-arm-v1.18.3/syncthing /usr/bin/syncthing

配置 syncthing

安装后运行 syncthing 然后会看到下面一行,也就是配置文件位置。安Ctrl+C关闭,打开配置文件修改
sudo nano /home/pi/.config/syncthing/config.xml

23:49:02 INFO: Loading HTTPS certificate: open /home/pi/.config/syncthing/https-cert.pem: no such file or directory
sudo nano  /home/pi/.config/syncthing/config.xml

找到下面的内容:127.0.0.1:8384

<gui enabled="true" tls="false" debugging="false">
        <address>127.0.0.1:8384</address>
        <apikey>XLk262RSigRtanHWzAHX6MufD7ySiddM</apikey>
        <theme>default</theme>
 </gui>

修改成:0.0.0.0:8384

<gui enabled="true" tls="false" debugging="false">
        <address>0.0.0.0:8384</address>
        <apikey>XLk262RSigRtanHWzAHX6MufD7ySiddM</apikey>
        <theme>default</theme>
</gui>

再次运行 syncthing

在局域网的任意电脑浏览器中输入:http://树莓派的IP地址:8384
就可以打开下面的配置网页了。

手机照片备份

树莓派端(服务器端)

接下来,我们在树莓派上建个文件夹做手机照片备份。显然在树莓派的SD卡上做是不现实的,除非你是土豪拥有1T SD卡。我这里用的是树莓派外挂1TB USB移动硬盘盒。将硬盘挂载到树莓派上,详细查看文章:

1. 先弄个密码: 点击右上角设置

2. 输入用户名和密码,然后点击保存

3. 重新打开 http://树莓派的IP地址:8384 ,会要求输入用户名和密码
输入用户名和密码然后慢慢研究吧!

4. 点击Defaul Foler --> 选项 --> 移除,删除默认的共享

5. 点击“添加文件夹”, 添加一个共享文件夹放置手机照片,NAS_K30

名称和路劲按照自己的需要写, 文件夹ID 很重要这里要记住,后面手机端要用

由于这个是用于备份的所以设置成 “仅接收”,“旧文件优先”,扫描间隔整个5天。
由于我们考虑到后期要每星期自动备份所以扫描时间设置成了5天。

树莓派端配置完成

6. 记住文件夹ID, 等下手机端要输入的

手机端

1. 安装软件

支持平台:Linux Macos Windows Android
下载Android APP:https://syncthing.en.softonic.com/android
其他版本可以在网页下方找到


手机UC浏览器打开网址:https://syncthing.en.softonic.com/android


=======================或者===========================

2. 打开软件

点击显示设备ID可以查看手机的ID,点击网页管理可以打开网页来配置

3. 添加要备份的文件夹,钥匙位置要填入你服务器端文件夹的ID

文件ID一定要填入上面建立NAS_K30时的 文件夹ID。
文件夹ID 相同的就会被同步

4. 等待扫描完成

5. 添加设备 备份照片的服务器

点击设备--+

点击设备标识,扫描或者输入服务器的ID

点击这里可以显示 服务器的ID 与二维码,然后用手机扫描

勾选完成

服务器端会弹出下面对话框,点击“添加设备”

点击共享,勾选NAS_K30

选择高级, 如果需要可以输入密码,由于是局域网内使用我们暂时不要输入。

然后保存

手机上,点击K30

勾选MyNAS

点击返回

点击返回,设备可以看到同步已经开始。

Syncthing 开机自启动。弄成后台服务:

在 /etc/init.d 目录下建立启动文件

sudo nano /etc/init.d/syncthing

输入:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          Syncthing
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Syncthing
# Description:       Syncthing is for backups
### END INIT INFO
  
# Documentation available at
# http://refspecs.linuxfoundation.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptfunc.html
# Debian provides some extra functions though
. /lib/lsb/init-functions
  
DAEMON_NAME="syncthing"
DAEMON_USER=pi
DAEMON_PATH="/user/bin/syncthing"
DAEMON_OPTS=""
DAEMON_PWD="${PWD}"
DAEMON_DESC=$(get_lsb_header_val $0 "Short-Description")
DAEMON_PID="/var/run/${DAEMON_NAME}.pid"
DAEMON_NICE=0
DAEMON_LOG='/var/log/syncthing'
  
[ -r "/etc/default/${DAEMON_NAME}" ] && . "/etc/default/${DAEMON_NAME}"
  
do_start() {
  local result
  
    pidofproc -p "${DAEMON_PID}" "${DAEMON_PATH}" > /dev/null
    if [ $? -eq 0 ]; then
        log_warning_msg "${DAEMON_NAME} is already started"
        result=0
    else
        log_daemon_msg "Starting ${DAEMON_DESC}" "${DAEMON_NAME}"
        touch "${DAEMON_LOG}"
        chown $DAEMON_USER "${DAEMON_LOG}"
        chmod u+rw "${DAEMON_LOG}"
        if [ -z "${DAEMON_USER}" ]; then
            start-stop-daemon --start --quiet --oknodo --background \
                --nicelevel $DAEMON_NICE \
                --chdir "${DAEMON_PWD}" \
                --pidfile "${DAEMON_PID}" --make-pidfile \
                --exec "${DAEMON_PATH}" -- $DAEMON_OPTS
            result=$?
        else
            start-stop-daemon --start --quiet --oknodo --background \
                --nicelevel $DAEMON_NICE \
                --chdir "${DAEMON_PWD}" \
                --pidfile "${DAEMON_PID}" --make-pidfile \
                --chuid "${DAEMON_USER}" \
                --exec "${DAEMON_PATH}" -- $DAEMON_OPTS
            result=$?
        fi
        log_end_msg $result
    fi
    return $result
}
  
do_stop() {
    local result
  
    pidofproc -p "${DAEMON_PID}" "${DAEMON_PATH}" > /dev/null
    if [ $? -ne 0 ]; then
        log_warning_msg "${DAEMON_NAME} is not started"
        result=0
    else
        log_daemon_msg "Stopping ${DAEMON_DESC}" "${DAEMON_NAME}"
        killproc -p "${DAEMON_PID}" "${DAEMON_PATH}"
        result=$?
        log_end_msg $result
        rm "${DAEMON_PID}"
    fi
    return $result
}
  
do_restart() {
    local result
    do_stop
    result=$?
    if [ $result = 0 ]; then
        do_start
        result=$?
    fi
    return $result
}
  
do_status() {
    local result
    status_of_proc -p "${DAEMON_PID}" "${DAEMON_PATH}" "${DAEMON_NAME}"
    result=$?
    return $result
}
  
do_usage() {
    echo $"Usage: $0 {start | stop | restart | status}"
    exit 1
}
  
case "$1" in
start)   do_start;   exit $? ;;
stop)    do_stop;    exit $? ;;
restart) do_restart; exit $? ;;
status)  do_status;  exit $? ;;
*)       do_usage;   exit  1 ;;
esac

加可执行权限

sudo chmod +x /etc/init.d/syncthing

添加默认启动

sudo update-rc.d syncthing defaults

移除默认启动,这个弄错了要用

sudo update-rc.d -f syncthing remove

之后你就可以通过命令管理 Syncthing,建议先用 status 查看状态。

sudo service syncthing status
sudo service syncthing start
sudo service syncthing stop
sudo service syncthing restart

pi@pi:~ $ sudo service syncthing status
● syncthing.service - LSB: Syncthing
   Loaded: loaded (/etc/init.d/syncthing; generated)
   Active: active (running) since Sun 2021-10-31 10:12:17 CST; 9min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 357 ExecStart=/etc/init.d/syncthing start (code=exited, status=0/SUCCESS)
    Tasks: 20 (limit: 2200)
   Memory: 56.3M
   CGroup: /system.slice/syncthing.service
           ├─398 /usr/bin/syncthing
           └─703 /usr/bin/syncthing

Oct 31 10:12:16 pisystemd[1]: Starting LSB: Syncthing...
Oct 31 10:12:17 pisyncthing[357]: Starting Syncthing: syncthing.
Oct 31 10:12:17 pi systemd[1]: Started LSB: Syncthing.
pi@pi:~ $

syncthing 命令行控制

syncthing <command> serve decrypt cli
命令行交互控制
syncthing cli config #查看配置情况
syncthing cli config version get #查看配置情况
syncthing cli config folders list #查看所有配置的文件夹ID
syncthing cli operations restart #重启
syncthing cli operations shutdown #关闭
syncthing cli config folders Fold_ID paused get #获取当前目录的状态
syncthing cli config folders Fold_ID pause set "true" #停止当前目录的同步
syncthing cli config folders Fold_ID pause set "false" #设置当前目录的同步

pi@pi:~ $ syncthing --help
Usage: syncthing <command>
Flags:
  -h, --help    Show context-sensitive help.
Commands:
  serve
    Run Syncthing
  decrypt <path>
    Decrypt or verify an encrypted folder
  cli
    Command line interface for Syncthing
Run "syncthing <command> --help" for more information on a command.
:~ $ syncthing --help
Usage: syncthing <command>
Flags:
  -h, --help    Show context-sensitive help.
Commands:
  serve
    Run Syncthing
  decrypt <path>
    Decrypt or verify an encrypted folder
  cli
    Command line interface for Syncthing
Run "syncthing <command> --help" for more information on a command.

Syncthing 天然具有内网穿透功能,完全可以替代百度云盘。

  1. 树莓盘和手机端安装然间个人电脑

猜你喜欢

转载自blog.csdn.net/qq_32824605/article/details/130185240