docs/guides/unraid/pages/docker-app-backup.md

107 lines
3.0 KiB
Markdown
Raw Normal View History

---
title: Docker application backup
description: How to backup a Docker application on Unraid
published: true
date: 2023-06-27T07:54:18.672Z
tags: linux, server, unraid, backup
editor: markdown
dateCreated: 2023-06-27T07:54:18.672Z
---
# Docker application backup
This guide will show you how to backup a Docker application on Unraid. _Tested on Unraid 6.12.1_
## Get Started
- Begin by installing the [User Scripts plugin](https://forums.unraid.net/topic/48286-plugin-ca-user-scripts/).
- Now we need to create the script. Go to `Settings > User Utilites > User Scripts` and click `Add New Script`.
- Give the script a name, for example `Backup Docker Application`.
- Paste the following into the script:
```bash
#!/bin/bash
# --- DO NOT TOUCH ---
now="$(date +"%Y-%m-%d"@%H.%M)"
# --- Variables ---
# The name of the application
applicationName="application_name"
# The name of the backup
backupName="backup-$(date +%Y-%m-%d_%H-%M-%S)"
# Set the path to the application's data directory
applicationData="/mnt/user/appdata/application_name/"
# Set the path to the backup directory
# Example: /mnt/user/Storage/Backups/$applicationName/
backupDirectory=""
2023-06-27 14:45:38 +01:00
# Set Number backups to keep
backupsToKeep=30
# The user of your account
# Example: lee
user=""
# --- Backup Application ---
# Start notification
2023-06-27 14:45:38 +01:00
echo "Backup of $applicationName starting."
/usr/local/emhttp/plugins/dynamix/scripts/notify -s "$applicationName Backup" -d "Backup of $applicationName starting."
# Create backup directory
mkdir -p "$backupDirectory"
cd "$backupDirectory"
# Create the backup tarball
2023-06-27 14:35:47 +01:00
tar -cf "$applicationName-$now".tar "$applicationData"
# Set Permissions
2023-06-27 14:45:38 +01:00
chown "$user:$user" "$applicationName-$now".tar
# Cleanup old backups
2023-06-27 14:45:38 +01:00
for file in $(ls -t); do
if [[ $count -ge $backupsToKeep ]]; then
rm -- "$file"
echo "Deleted old backup: $file"
fi
((count++))
done
# Stop notification
2023-06-27 14:45:38 +01:00
echo "Backup of $applicationName completed!"
/usr/local/emhttp/plugins/dynamix/scripts/notify -s "$applicationName Backup" -d "Backup of $applicationName completed!"
```
- You can now test the script by clicking `Run Script`. If everything is working correctly, you should see a notification in the top right corner of the screen.
- Now we need to set the script to run on a schedule. Next to `Run In Background` you will see a dropdown menu. Select how often you want it to run and click `Apply`.
## Restore a backup
- Delete the application's data directory.
- Copy the backup to the application's data directory.
Move the backup with the following command: (or via the Unraid web interface)
```bash
mv <backup_name>.tar /mnt/user/appdata/application_name/
```
- To restore a backup, simply extract the tarball and copy the files to the application's data directory.
You can extract the tarball by running the following command:
```bash
tar -xvf <backup_name>.tar
```
This command will extract the tarball to the current directory.
## Finished
You have now successfully setup a backup script for your Docker application. You can view the backups in the directory you set in the script.