Development notes

Thoughts, notes and ideas about development

Backup Docker Volumes to the NAS

2023-06-13 2 min read Docker Backups

This guide will walk you through the process of setting up a backup solution for your Docker volumes using an NFS shared folder on a Synology NAS. We will cover the steps from preparing the NFS shared folder to configuring a cron task for automated backups.

Prepare the NFS Shared Folder on the Synology

  1. Open the Control Panel of your Synology NAS.

  2. Navigate to Shared Folder and click on Create.

  3. Give the folder a name, such as docker-bkps.

  4. Once the folder is created, select it and click on Edit.

  5. In the NFS Permissions tab, create the following configuration:

    NFS Permissions

  6. Click on Save.

Install Required Packages on the Host Machine

To perform the backup, we need to install nfs-common and rsync packages on the host machine. Run the following command:

sudo apt-get install nfs-common rsync

Create Mount Point and Mount the NFS Folder

  1. Create a mount point using the following command:
sudo mkdir -p /mnt/nfs
  1. Mount the NFS folder using the following command:
sudo mount 192.168.1.37:/volume1/docker-bkps /mnt/nfs

make sure to use the correct IP for your NAS.

  1. To make the mount permanent, open the /etc/fstab file:
sudo nano /etc/fstab
  1. Add the following line at the end of the file to define the NFS mount:
192.168.1.37:/volume1/docker-bkps /mnt/nfs nfs defaults 0 0

Create Backup Script

Create a Bash script named docker-bkp.sh with the following content:

#!/bin/bash

# Define source and destination paths
HOSTNAME=$(hostname)
SOURCE="/var/lib/docker/volumes/"
DESTINATION="/mnt/nfs/$HOSTNAME"

# Run rsync command to perform the backup
rsync -avz --delete "$SOURCE" "$DESTINATION"

To test the script manually, run the following command:

sudo ./docker-bkp.sh

After running the command sudo ls /mnt/nfs/, the backup folder should contain a directory like k3s-3, which contains all the Docker volumes.

Configure the Cron Task

  1. Create a file using the command:
sudo vim /etc/cron.daily/docker-bkp
  1. Add the following content to the file:
#!/bin/bash

sudo /home/ubuntu/docker-bkp.sh >> /home/ubuntu/docker-bkp.sh.log 2>&1
  1. Make the file executable:
sudo chmod +x /etc/cron.daily/docker-bkp
  1. Verify that the task runs without errors:
sudo run-parts /etc/cron.daily

Alternatively, Run the Script Manually

You can also run the script manually by executing the following command:

sudo /etc/cron.daily/docker-bkp

Verify Logs

To verify the logs, use the following command:

tail -f /home/ubuntu/docker-bkp.sh.log

With these steps, you have successfully set up a backup solution for your Docker volumes using an NFS shared folder on a Synology NAS.

comments powered by Disqus