rsync backup to a Synology NAS

This tutorial covers how to backup (manually or automatically) the userdata partition of your Batocera machine to a Synology NAS. This allows you to recover your data to a previous state in case of data loss.

The tutorial is based on a Synology NAS which is running on Synology's DSM version 6.x or later.

This tutorial assumes you have already set up the following:

  • Static IP address and/or hostname for your Synology NAS (you can do this via your DHCP/DNS server (mostly your router))
  • Static IP address and/or hostname for your Batocera system (you can do this via your DHCP/DNS server (mostly your router))
  • Optional: If you want to set up an automated backup (which is also covered in this tutorial), your Batocera system has to be capable of being started remotely (e.g. capable of Wake on LAN (WoL)).

There are mainly two different network protocols on how to backup data to as Synology NAS: SMB (Server Message Block) and NFS (Network File System). Both are being covered separately here. In a nutshell, both network protocols come with its adavantages/disadvantages, which you should think of while considering on how to backup your data.

  • SMB: The advantage here is that SMB is easy to understand and easy to set up. It comes with standard user/password authentication. The disadvantages of SMB is that it works best within Windows environments and in some circumstances it has its compatibility problems with Linux symlinks, which according to Batocera are being created mostly when setting up Windows games on WINE. SMB is also known to be less efficient/performant than NFS.
  • NFS: The advantage on NFS is that it has no problems with Linux symlinks. It is also more performant than SMB. For security, NFS provides IP based security and authentication security on shared folders and this is where NFS comes into its disadvantages: Authentication security is a pain to set up for the average user, but fortunately it is an optional thing. Assuming your home network is secure, you should not worry about security too much in that case as you can still use the IP based security anyway so that only the Batocera system can access the according shared backup folder on the NAS and is really limited to that single shared folder.

In summary:

  • If you are not planning to install or backup Windows games on WINE and network security is a big thing, SMB is the way to go.
  • If you want to use the full spectrum of current and future emulators, have the possibility to back everything up without issues and network security is not a big deal, NFS is the way to go.
  1. Under SSH, navigate to Control PanelTerminal & SNMP and check “Enable SSH service”. Then, for Port set the value 22.
    Enable SSH
  2. Under rsync, navigate to Control PanelFile Servicesrsync and check “Enable rsync service”. Then, for SSH encryption port set the value 22.
    Enable rsync

    You don't have to worry that port 22 is used by SSH and rsync at the same time. This is a common setup and the functionality of each service will not be interfered/disrupted in any way!

  3. Create a shared folder where the Batocera backup data can be stored to. For this example, the folder will be named batoceraBackup.
  4. Create a firewall rule on the Synology NAS so that the Batocera machine can send its data over the network to the Synology NAS. The rule must allow the following two service:
    • Encrypted terminal service (includes encrypted rsync and SFTP)
      Firewall rule(s)
  5. In addition it is also strongly recommended to limit the firewall rule to allow only the static IP address of the Batocera system (that's why it is important to set a static IP for the Batocera system).
  1. Enable the following services on the Synology NAS via the Synology DSM web GUI as follows:
    • Navigate to Control PanelFile ServicesSMB and check “Enable SMB service”.
      Enable SMB
  2. Enable the read/write data permission for the user backup on the user permissions page of the according shared folder “BatoceraBackup”:
    User permissions
  3. Edit the firewall rule we have just created before on the Synology NAS. In addition to the rule we created earlier, the rule must allow the following service:
    • Windows file server
      Firewall rule(s)
  1. Enable the following services on the Synology NAS via the Synology DSM web GUI as follows:
    • Navigate to Control PanelFile ServicesNFS and check “Enable NFS service” and for “Maximum NFS protocol” choose “NFSv4”.
      Enable NFS
  2. Now create an according NFS rule as follows:
    Edit the batocerBackup shared folder's settings we created earlier and navigate to NFS Permissions. Click on Create and for Hostname or IP insert the hostname/IP-address of your Batocera system. Leave the other settings as they are:
    Shared folder's NFS rule

    Click Save twice.

  3. Edit the firewall rule we have just created before on the Synology NAS. In addition to the rule we created earlier, the rule must allow the following service:
    • Mac/Linux file server
      Firewall rule(s)

Now SSH into your Batocera system and execute the following command to create a custom scripts folder:

mkdir -p /userdata/scripts

On the following steps we will cover on how to create three scripts here:

  • romsBackup.sh → Which will backup your /userdata/roms folder, excluding the folders /userdata/roms/steam and /userdata/roms/windows_installers as it does not make much sense to backup those folders. When using SMB, to avoid Linux symlink issues as mentioned above, we will also exclude the /userdata/roms/windows folder. If you want to backup those folders anyway, feel free to adjust the script accordingly.
  • userdataBackup.sh → Which will backup your /userdata folder, excluding the /userdata/roms folder as it may take a very long time to finish, depending on how many games you have to copy over. This way you can save time in case you want to run the scripts manually for whatever reason and backup the rest of your userdata independent from your games. Note that we will also exclude the /userdata/saves/flatpak folder as empirically there are too many issues when importing it back from a backup. If you want to backup those folders anyway, feel free to adjust the script accordingly.
  • systemBackup.sh → Which will only execute the two scripts romsBackup.sh and userdataBackup.sh.

We are separating the first two scripts instead of putting them in a single script so you can execute them manually whenever you want to, indepented from the automation steps we are covering below. The third script is exactly for those automation steps.

Beware: The rsync command's –delete option within the following script romsBackup.sh does delete every file on the target side which is not available on the source side. We are using this option due to keep the target side consistent with the source side (for example if you have deleted some games on the source side and you want them being deleted on the target side too). If you don't want this to happen then just remove the –delete option on the script. Also note that rsync by default does synchronize only files that have been changed/added since the last synchronization. This will prevent unnecessary syncing for files that have already been synced in the past.

For the following mounting steps within the scripts, please note that unlike SMB, for NFS you cannot just mount a remote shared folder as //<my_IP_address/hostname>/<my_Shared_Folder>. Instead you will have to insert the full path to the remote shared folder. On a Synology NAS it's always <my_IP_address/hostname>:/volume<x>/<my_Shared_Folder>. The value <x> in volume<x> is dependent on the volume number on the Synology NAS. If you have set up only a single volume on your Synology NAS, it's mostly volume1. If you are unsure you can SSH into your Synolog NAS to discover the real path of your shared folder.

Now create the following script file:

nano /userdata/scripts/romsBackup.sh

And paste the following content (Replace all placeholders (<…>) with the accoring values of your personal environment):

  • SMB:
    #!/bin/sh
    USER=backup
    PASS=<your_password>
    SHARE=//<Synology_NAS_IP_address/hostname>/batoceraBackup
    MOUNT_POINT=/tmp/tempMountFolder
    mkdir -p "$MOUNT_POINT"
    mount -t cifs -o username="$USER",password="$PASS" "$SHARE" "$MOUNT_POINT"
    rsync --exclude=roms/steam --exclude=roms/windows --exclude=roms/windows_installers -Pav --delete -e ssh /userdata/roms "$MOUNT_POINT"
    cd /tmp
    umount -f "$MOUNT_POINT"
    exit 0
  • NFS:
    SHARE=<Synology_NAS_IP_address/hostname>:/volume<x>/batoceraBackup
    MOUNT_POINT=/tmp/tempMountFolder
    mkdir -p "$MOUNT_POINT"
    mount -t nfs4 "$SHARE" "$MOUNT_POINT" -o nolock,rw
    rsync --exclude=roms/steam --exclude=roms/windows_installers --delete -e ssh -Pav --no-owner --no-group --no-perms /userdata/roms "$MOUNT_POINT"
    cd /tmp
    umount -f "$MOUNT_POINT"
    exit 0

Save the file and quit the editor.

Now create the following script file:

nano /userdata/scripts/userdataBackup.sh

And paste the following content (Replace all placeholders (<…>) with the accoring values of your personal environment):

  • SMB:
    #!/bin/sh
    USER=backup
    PASS=<your_password>
    SHARE=//<Synology_NAS_IP_address/hostname>/batoceraBackup
    MOUNT_POINT=/tmp/tempMountFolder
    mkdir -p "$MOUNT_POINT"
    mount -t cifs -o username="$USER",password="$PASS" "$SHARE" "$MOUNT_POINT"
    cd "$MOUNT_POINT"
    tar cfv "$MOUNT_POINT"/batoceraBackup.tar /userdata --exclude=roms --exclude=saves/flatpak
    cd /tmp
    umount -f "$MOUNT_POINT"
    exit 0
  • NFS:
    SHARE=<Synology_NAS_IP_address/hostname>:/volume<x>/batoceraBackup
    MOUNT_POINT=/tmp/tempMountFolder
    mkdir -p "$MOUNT_POINT"
    mount -t nfs4 "$SHARE" "$MOUNT_POINT" -o nolock,rw
    cd "$MOUNT_POINT"
    tar cfv "$MOUNT_POINT"/batoceraBackup.tar /userdata --exclude=roms --exclude=saves/flatpak
    cd /tmp
    umount -f "$MOUNT_POINT"
    exit 0

Save the file and quit the editor.

Now create the following script file:

nano /userdata/scripts/systemBackup.sh

And paste the following content:

#!/bin/sh
/userdata/scripts/romsBackup.sh
/userdata/scripts/userdataBackup.sh
batocera-es-swissknife --shutdown

Save the file and quit the editor.

Now make all three scripts executable by executing:

chmod +x /userdata/scripts/*.sh

You can test your scripts by executing them as follows:

Backup userdata:

/userdata/scripts/userdataBackup.sh

Backup games:

/userdata/scripts/romsBackup.sh

Backup userdata and games:

/userdata/scripts/systemBackup.sh

If you are planning to only backup your data manually by executing your scripts manually, you can stop here.

Now let's set up the optional automation part for automatic backups of your userdata and your games:

SSH with user root into your Synology NAS (Unfortunately you have to use a Synology user which is part of the administrators group, otherwise SSH access will be declined. To avoid any permission issues, we will user root here.). Then execute the following commands (replace <Batocera_IP_address/hostname> with the static IP address of your Batocera system) for passwordless login by using SSH secure authentication key from your Synology NAS to your Batocera system (if you have already created an SSH key for your root user in the past on your Synology NAS, skip the first command!):

ssh-keygen

Confirm everything without inserting anything and by just hitting the [Enter] key on your keyboard!

Now execute the following command:

cat ~/.ssh/id_rsa.pub | ssh root@<Batocera_IP_address/hostname> 'cat >> /userdata/system/.ssh/authorized_keys'

Insert the Batocera root user's password (Default password: linux).
Now test the passwordless SSH login from your Synology NAS to your Batocera system.

If passwordless SSH login from your Synology NAS to your Batocera system works, you can now go back to the Synology DSM web GUI and create the according automation task as follows: Navigate to Control PanelTask SchedulerCreateScheduled TaskUser-defined scriptGeneral: For Task insert a custom task name (e.g. batoceraBackup) and for user choose rootSchedule: Configure a desired custom schedule → Task Settings: For User-defined script insert the following (Replace <Batocera_MAC_address> with the according MAC address of your Batocera system, <Synology_network_interface> with the according network interface name through which you want to send the magic packet to (the one which is within the same Layer 2 subnet as your Batocera system (e.g. eth0) and <Batocera_IP_address/hostname> with the static IP address/hostname of your Batocera system.):

If your Synology NAS does have multiple network interfaces, to find out which network interface name you have to take, from Synology DSM web GUI navigate to: Control PanelInfo CenterNetwork. All of the available network interfaces are listed here with all the unique network/subnet informations. LAN1 is always eth0, LAN2 is always eth1, LAN 3 is always eth2, etc.)

If your Batocera system takes longer than 30 seconds to boot up, feel free to increase the sleep variable to whatever value you think does fit the real boot time of your Batocera system.

#!/bin/sh
sudo synonet --wake <Batocera_MAC_address> <Synology_network_interface>
sleep 30
ssh root@<Batocera_IP_address/hostname> sh ../scripts/systemBackup.sh

Confirm by clicking on OK.

That's it! Your scheduled backup automation should work now.

You can test it by running the task manually via the Task Scheduler as follows:

Navigate to Control PanelTask Scheduler → Look out for your specific task, highlight it and click on Run.

  • rsync_backup.txt
  • Last modified: 3 months ago
  • by grandmabetty