Backup Docker Volumes to the NAS
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
-
Open the Control Panel of your Synology NAS.
-
Navigate to Shared Folder and click on Create.
-
Give the folder a name, such as
docker-bkps
. -
Once the folder is created, select it and click on Edit.
-
In the NFS Permissions tab, create the following configuration:
-
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
- Create a mount point using the following command:
sudo mkdir -p /mnt/nfs
- 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.
- To make the mount permanent, open the
/etc/fstab
file:
sudo nano /etc/fstab
- 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
- Create a file using the command:
sudo vim /etc/cron.daily/docker-bkp
- Add the following content to the file:
#!/bin/bash
sudo /home/ubuntu/docker-bkp.sh >> /home/ubuntu/docker-bkp.sh.log 2>&1
- Make the file executable:
sudo chmod +x /etc/cron.daily/docker-bkp
- 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.