Install Syncthing on Raspberry Pi Self-built private cloud disk photo backup backup mobile phone album

There are a lot of photos stored on the mobile phone, and I often toss the mobile phone, there is always the problem of data loss, and I am not at ease with the cloud disks on the market, so I plan to use the Raspberry Pi to build a private cloud at home. After some searching, I found syncthing. Syncthing is a cross-platform, open source and free P2P-based file synchronization solution that supports Windows, Mac, Linux, Android, and syncthing officially does not support the iOS platform.

install 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

We install the latest version v1.18.3

Raspberry Pi enter the following command to install

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

configure syncthing

Run syncthing after installation and you will see the following line, which is the location of the configuration file. An Ctrl+C to close, open the configuration file to modify
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

Find the following: 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>

Modify to: 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>

run syncthing again

Enter in any computer browser on the LAN: http://IP address of the Raspberry Pi: 8384
to open the following configuration web page.

phone photo backup

Raspberry Pi (server)

Next, we create a folder on the Raspberry Pi to back up the photos on the phone. Obviously, it is unrealistic to do it on the SD card of the Raspberry Pi, unless you are rich and own a 1T SD card. What I use here is a 1TB USB mobile hard disk box attached to the Raspberry Pi. Mount the hard disk to the Raspberry Pi, see the article for details:

1. Get a password first: Click Settings in the upper right corner

2. Enter your username and password and click Save

3. Re-open http://IP address of Raspberry Pi: 8384, you will be asked to enter the user name and password.
Enter the user name and password and study slowly!

4. Click Default Foler --> Options --> Remove to delete the default share

5. Click "Add Folder" to add a shared folder to place photos on the phone, NAS_K30

Write the name and path according to your own needs, the folder ID is very important to remember here, you will use it later on the mobile phone

Since this is used for backup, it is set to "receive only", "old files first", and the scan interval is 5 days.
Since we consider that we will automatically backup every week in the later stage, the scanning time is set to 5 days.

Raspberry Pi configuration is complete

6. Remember the folder ID, and wait for the input on the mobile phone

Mobile terminal

1. Install the software

Support platform: Linux Macos Windows Android
Download Android APP: https://syncthing.en.softonic.com/android
Other versions can be found at the bottom of the page


Mobile UC browser to open URL: https://syncthing.en.softonic.com/android


======================== or =========================== =

2. Open the software

Click Display Device ID to view the ID of the mobile phone, and click Web Management to open the web page for configuration

3. Add the folder to be backed up, and the key location should be filled with the ID of your server-side folder

The file ID must be filled in the folder ID when creating NAS_K30 above.
Folders with the same ID will be synchronized

4. Wait for the scan to complete

5. Add a server for device backup photos

Click on the device - +

Click Device ID, scan or enter the ID of the server

Click here to display the server ID and QR code, and then scan it with your mobile phone

Tick ​​completed

The following dialog box will pop up on the server side, click "Add Device"

Click share, check NAS_K30

Select Advanced, if necessary, you can enter the password, because it is used in the LAN, we do not enter it for the time being.

then save

On the mobile phone, click K30

Tick ​​MyNAS

click back

Tap Back and the device can see that the sync has started.

Syncthing starts automatically at boot. Make it a background service:

Create a startup file in the /etc/init.d directory

sudo nano /etc/init.d/syncthing

enter:

#!/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

Add executable permissions

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

Add default startup

sudo update-rc.d syncthing defaults

Remove the default startup, this is a mistake to use

sudo update-rc.d -f syncthing remove

After that, you can manage Syncthing through commands. It is recommended to check the status with status first.

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 command line control

syncthing <command> serve decrypt cli
command line interactive control
syncthing cli config #View configuration status
syncthing cli config version get #View configuration status
syncthing cli config folders list #View all configured folder IDs
syncthing cli operations restart #Restart
syncthing cli operations shutdown #Close
syncthing cli config folders Fold_ID paused get #Get the status of the current directory
syncthing cli config folders Fold_ID pause set "true" #Stop the synchronization of the current directory
syncthing cli config folders Fold_ID pause set "false" #Set the synchronization of the current directory

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 naturally has the function of intranet penetration, which can completely replace Baidu cloud disk.

  1. Raspberry disk and mobile phone installed ranjian personal computer

Guess you like

Origin blog.csdn.net/qq_32824605/article/details/130185240