The most recent version of this page is a draft.DiffThis version is outdated by a newer approved version.DiffThis version (2022/03/01 13:22) is a draft.
Approvals: 0/1

This is an old revision of the document!


rsync backup

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

The following 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 Wake on LAN (WoL) capable.

Preparing your Synology NAS

  1. Enable the following services on the Synology NAS via the Synology DSM web GUI as follows:
    • Under SMB, navigate to Control PanelFile ServicesSMB and check “Enable SMB service”.
      Enable SMB
    • Under SSH, navigate to Control PanelTerminal & SNMP and check “Enable SSH service”. Then, for Port set the value 22.
      Enable SSH
    • 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!

  2. Create a shared folder where the Batocera backup data can be stored to. For this example, the folder will be named batoceraBackup. Enable the read/write data permission for the user backup on the user permissions page:
    User permissions
  3. 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 services:
    • Windows file server
    • Encrypted terminal service (includes encrypted rsync and SFTP)

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):

Firewall rule(s)

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

mkdir -p /userdata/scripts

Preparing the Batocera machine

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

  • romsBackup.sh → Which will backup your /userdata/roms folder.
  • 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 folder as there are too many issues on different emulators when importing savestates back from a backup!
  • 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.

Now create the following script file:

nano /userdata/scripts/romsBackup.sh

And paste the following content (replace <your_password> with the according backup user its user password and <Synology_NAS_IP_address/hostname> with the static IP address/hostname of your Synology NAS):

#!/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 -Pav --delete -e ssh /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 <your_password> with the according backup user its user password and <Synology_NAS_IP_address/hostname> with the static IP address/hostname of your Synology NAS):

#!/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
rm -rf ./batoceraBackup.tar
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 --emukill && shutdown -h now

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.

Automation part (optional)

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.1646137372.txt.gz
  • Last modified: 2 years ago
  • by grandmabetty