Unraid (User Script) – Backup Dockers AppData

This script is a handy tool for automating the backup process and managing disk space effectively. You can schedule it to run periodically using a cron job or any other task scheduler to ensure regular backups and efficient use of storage space.

The script uses the “cp” command to create a backup of the “/mnt/cache/appdata” directory and its contents, and stores it in a new directory with a timestamp-based name in “/mnt/disk1/appdata_Backup/”. The script also includes a feature to automatically delete older backups to keep the number of backups under control.

The number of backups to keep is configurable using the “NUM_BACKUPS_TO_KEEP” variable at the beginning of the script. By default, it is set to 5, but you can adjust it according to your needs. If the number of existing backups exceeds the specified value, the script calculates the number of directories to delete and removes the oldest backups using the “rm” command with the “-rf” option.

#!/bin/bash

# Define the number of backups to keep
NUM_BACKUPS_TO_KEEP=5

# Get the current timestamp
TIMESTAMP=$(date "+%Y-%m-%d_%H-%M-%S")

# Create a backup directory with the current timestamp
BACKUP_DIR="/mnt/disk1/appdata_Backup/appdata_$TIMESTAMP"
cp -R /mnt/cache/appdata "$BACKUP_DIR"

# Delete older backup directories
BACKUP_DIRS=($(ls -d /mnt/disk1/appdata_Backup/appdata_*))
NUM_BACKUPS=${#BACKUP_DIRS[@]}
if [ $NUM_BACKUPS -gt $NUM_BACKUPS_TO_KEEP ]; then
  NUM_DIRS_TO_DELETE=$((NUM_BACKUPS - NUM_BACKUPS_TO_KEEP))
  OLDEST_DIRS=("${BACKUP_DIRS[@]:0:$NUM_DIRS_TO_DELETE}")
  for DIR in "${OLDEST_DIRS[@]}"; do
    rm -rf "$DIR"
    echo "Deleted old backup: $DIR"
  done
fi

Related Posts