| Both sides previous revision Previous revision Next revision | Previous revision |
| rsync_backup [2022/01/28 11:34] – Minor change: Adjusting "ssh-copy-id" description. grandmabetty | rsync_backup [2023/09/15 08:37] (current) – Correction: Adjusted links from german to english language grandmabetty |
|---|
| ====== rsync backup ====== | ====== rsync backup to a Synology NAS ====== |
| |
| ===== Backup your data to a NAS ===== | This tutorial covers how to backup the userdata partition of your Batocera machine to a [[https://www.synology.com/en-us/dsm/solution/what_is_nas|Synology NAS]]. This allows you to recover your data easily to a previous state in case of data loss. |
| |
| 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. | ===== Backup via Synology Active Backup For Business (ABB) ===== |
| |
| ==== Example for a Synology NAS ==== | Since the release of [[https://www.synology.com/en-us/dsm/feature/active-backup-business/pc|Synology Active Backup For Business]], backing up a Batocera system via //rsync// became much more convenient than ever before. Please check if your NAS model supports //Synology ABB// on this [[https://www.synology.com/en-us/dsm/packages/ActiveBackup|link]]. |
| |
| The following tutorial is based on a [[https://www.synology.com/en-us/dsm/solution/what_is_nas|Synology NAS]] which is running on Synology's DSM **version 6.x** or later. | ==== Tutorial ==== |
| |
| <WRAP center round info> | === Backup === |
| 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|Wake on LAN (WoL)]] capable. | |
| </WRAP> | |
| |
| === Preparing your Synology NAS === | - Install //Synology ABB// via //Synology Package Manager// |
| | - Start //Synology ABB// (if you are installing //Synology ABB// from scratch, you have register the application via the [[https://account.synology.com|Synology online account portal]], don't worry, it's completely free!) |
| | - Navigate to: //File Server// → //Add Server// |
| | - Choose //rsync server//: \\ [{{:useanasbackup10.png?400|Add a server}}] Click on //Next// |
| | - For the following input fields add the following values:\\ //**Server Address:**// ''<my_Batocera_IPAddress_or_hostname>'' \\ **Connection Mode:** Select ''rsync shell mode via SSH'' \\ **Port:** Insert your Batocera instance's SSH port (Default: ''22'') \\ **Account:** ''root'' \\ **Auth Policy:** Choose ''By password'' (or alternatively ''By SSH key'' if you have set up [[access_the_batocera_via_ssh#password-less_authentication|]] from your NAS to your Batocera instance before) \\ **Password:** ''<myBatoceraRootPassword>'' \\ [{{:useanasbackup11.png?400|Insert server data}}] Click on //Done// |
| | - Now highlight the //rsync// instance you have just created and click on //Create Task//, then select your preferred backup mode: \\ [{{:useanasbackup12.png?400|Select preferred backup mode}}] Click on //Next// |
| | - Select all folders from the Batocera filesystem you want to backup (If you are unsure: Select the whole ''userdata'' folder but no other folders): \\ [{{:useanasbackup13.png?400|Select preferred folders to backup}}] Click on //Next// |
| | - For the following input fields add the following values:\\ **Task name:** ''<myCustomTaskName>'' \\ **Local path:** Click on //Browse// and select the preferred path where the backup should be stored on your NAS: \\ [{{:useanasbackup14.png?400|Additional task settings}}] Click on //Next// and then click on //Done// |
| | - When asked, confirm if you want to start the backup immediately. If you want to start the backup later manually, highlight the according //rsync// server, click on //Task List// and then click on //Back up//. |
| |
| - Enable the following services on the Synology NAS via the Synology DSM web GUI as follows: | |
| * Under **SMB**, navigate to **Control Panel** → **File Services** → **SMB** and check "Enable SMB service".\\ {{:useanasbackup01.png?300|Enable SMB}} | |
| * Under **SSH**, navigate to **Control Panel** → **Terminal & SNMP** and check "Enable SSH service". Then, for **Port** set the value ''22''.\\ {{:useanasbackup05.png?500|Enable SSH}} | |
| * Under **rsync**, navigate to **Control Panel** → **File Services** → **rsync** and check "Enable rsync service". Then, for **SSH encryption port** set the value ''22''.\\ {{:useanasbackup02.png?600|Enable rsync}}<WRAP center round info> | |
| 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! | |
| </WRAP> | |
| - [[https://kb.synology.com/en-us/DSM/help/DSM/AdminCenter/file_user_create?version=6|A user (without administrative privileges) will need to be created on the Synology NAS]]. For this example the user will be named ''backup''. | |
| - [[https://kb.synology.com/en-us/DSM/help/DSM/AdminCenter/file_share_create?version=6|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:\\ {{:useanasbackup03.png?900|User permissions}} | |
| - [[https://kb.synology.com/en-us/DSM/help/DSM/AdminCenter/connection_security_firewall?version=6|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): | |
| |
| {{:useanasbackup04.png?1000|Firewall rule(s)}} | === Restore === |
| |
| Now [[https://wiki.batocera.org/access_the_batocera_via_ssh|SSH]] into your Batocera system and execute the following command to create a custom ''scripts'' folder: | <WRAP left round tip 100%> |
| <code>mkdir -p /userdata/scripts</code> | In case you are restoring your data based on a fresh Batocera installation, before restoring, make sure bluetooth service is stopped by executing the following command on your Batocera system: ''/etc/init.d/S32bluetooth stop''. This makes sure your bluetooth controllers are being recognized and working as before. If you don't do this, then you have to set up your bluetooth controllers from scratch again. |
| | |
| === Preparing the Batocera machine === | |
| | |
| <WRAP center round tip> | |
| 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. | |
| </WRAP> | </WRAP> |
| |
| <WRAP center round important> | If you want to restore a single file or a full backup, do so by using the //Active Backup For Business (Recovery) Portal// which was installed automatically additionally on your //Synology NAS// when installing //Synology ABB//. It's pretty straightforward: Highlight the according folder(s)/file(s), right-click and choose //Restore// (don't forget to have your Batocera system turned on!): \\ [{{:useanasbackup15.png?400|Restore a backup}}] After successful restore process, reboot your Batocera system. |
| **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. | |
| </WRAP> | |
| | |
| Now create the following script file: | |
| <code>nano /userdata/scripts/romsBackup.sh</code> | |
| 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): | |
| <code> | |
| #!/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 | |
| </code> | |
| Save the file and quit the editor. | |
| | |
| Now create the following script file: | |
| <code>nano /userdata/scripts/userdataBackup.sh</code> | |
| 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): | |
| <code> | |
| #!/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 | |
| </code> | |
| Save the file and quit the editor. | |
| | |
| Now create the following script file: | |
| <code>nano /userdata/scripts/systemBackup.sh</code> | |
| And paste the following content: | |
| <code> | |
| #!/bin/sh | |
| /userdata/scripts/romsBackup.sh | |
| /userdata/scripts/userdataBackup.sh | |
| shutdown -h now | |
| </code> | |
| Save the file and quit the editor. | |
| | |
| Now make all three scripts executable by executing: | |
| <code>chmod +x /userdata/scripts/*.sh</code> | |
| | |
| You can test your scripts by executing them as follows: | |
| | |
| Backup userdata: | |
| <code>/userdata/scripts/userdataBackup.sh</code> | |
| Backup games: | |
| <code>/userdata/scripts/romsBackup.sh</code> | |
| Backup userdata and games: | |
| <code>/userdata/scripts/systemBackup.sh</code> | |
| | |
| 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: | |
| | |
| [[https://kb.synology.com/en-global/DSM/tutorial/How_to_login_to_DSM_with_root_permission_via_SSH_Telnet|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 [[https://www.ssh.com/academy/ssh/key|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!): | |
| <code>ssh-keygen</code> Confirm //everything// without inserting //anything// and by just hitting the ''[Enter]'' key on your keyboard! | |
| | |
| Now execute the following command: | |
| <code>cat ~/.ssh/id_rsa.pub | ssh root@<Batocera_IP_address/hostname> 'cat >> /userdata/system/.ssh/authorized_keys'</code> | |
| Insert the Batocera ''root'' user's password (Default password: ''linux''). | |
| | |
| Now go back to the Synology DSM web GUI and create the according automation task as follows: | |
| Navigate to ''Control Panel'' → ''Task Scheduler'' → ''Create'' → ''Scheduled Task'' → ''User-defined script'' → ''General'': For ''Task'' insert a custom task name (e.g. ''batoceraBackup'') and for ''user'' choose ''root'' → ''Schedule'': 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.): | |
| | |
| <WRAP center round tip> | |
| 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 Panel'' → ''Info Center'' → ''Network''. 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.) | |
| </WRAP> | |
| | |
| <WRAP center round tip> | |
| 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. | |
| </WRAP> | |
| | |
| <code> | |
| #!/bin/sh | |
| sudo synonet --wake <Batocera_MAC_address> <Synology_network_interface> | |
| sleep 30 | |
| ssh root@<Batocera_IP_address/hostname> sh ../scripts/systemBackup.sh | |
| </code> | |
| 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: | That's it, you're done! |
| |
| Navigate to ''Control Panel'' → ''Task Scheduler'' → Look out for your specific task, highlight it and click on ''Run''. | |
| |