Skip to Content
DocumentationGuidesAuto backup your apps using cron

Auto backup your apps using cron

In this guide we will show you how to automate backing up all your apps every week, month or day using Runtipi’s new backup feature.

⚠️

This script requires Runtipi version v4.0.0 or above.

Setup

Create the backup script

Create a new script file (.sh extension) in the runtipi/scripts folder (if the folder doesn’t exist you can create it manually) called backup-apps.sh and paste the following code:

#!/bin/bash # # Usage: ./backup-apps.sh [daily|weekly|monthly|yearly] [stop|ignore] # Default parameters are "daily stop" # Specify your runtipi base path runtipiPath="" # Specify path to you backup reference list (optional) backupListPath="" # Storage path for the Apps backupPath="$runtipiPath/backups" # Specify backup retention - Number of backup file to keep for each schedule dailyRetention=3 weeklyRetention=3 monthlyRetention=3 yearlyRetention=3 # Sleep Duration - How many seconds to wait after starting/stopping an apps sleepDuration=10 # Default runtipi paths appsDataPath="$runtipiPath/app-data" appsPath="$runtipiPath/apps" userConfigsPath="$runtipiPath/user-config" # Define the temporary directory for the archive creation archiveCreatingWorkDir="/tmp" # App status - Set default value to started appOriginalStatus='started' # Check for runtipi path value if [ -z "$runtipiPath" ]; then echo "Runtipi path not specified" exit 1 elif [ -d "$runtipiPath" ]; then echo "Runtipi path : $runtipiPath" else echo "Runtipi path not found : $runtipiPath" exit 1 fi # Default Parameters backupTypeDefaultValue='daily' stopAppDefaultValue='stop' # Define retention policy backupType="${1:-$backupTypeDefaultValue}" # Change keepLast to adjust retention policy case "$backupType" in daily) keepLast=$dailyRetention ;; weekly) keepLast=$weeklyRetention ;; monthly) keepLast=$monthlyRetention ;; yearly) keepLast=$yearlyRetention ;; *) echo "Invalid backup type specified. Use 'daily', 'weekly', 'monthly' or 'yearly'." exit 2 ;; esac # Check Runtipi version runtipiVersion=$(cat $runtipiPath/VERSION) runtipiMajorVersion=$(echo $runtipiVersion | cut -d. -f1 VERSION | sed 's/^v//') if [ "$major_version" -lt 4 ]; then echo "Runtipi version is $runtipiVersion, this script require at least v4.0.0" exit 4 fi # Should Apps be stopped during backup stopApp="${2:-$stopAppDefaultValue}" # List all appstores for appStore in $(ls $appsPath) do # Check optional apps reference list refBackupList=$backupListPath if [ -z "$refBackupList" ]; then backupList=$(ls $appsPath/$appStore) elif [ -f "$refBackupList" ]; then backupList=$(cat $refBackupList) else echo "Invalid path for backup reference list file." exit 3 fi # List all installed apps for app in $backupList do if [ -d "$appsPath/$appStore/$app" ]; then echo "Starting backup apps from $appStore appstore." # Stop the app if it was asked if [ "$stopApp" = 'stop' ]; then cd $runtipiPath appStatusCheck=$(docker ps -f name=^/$app"_"$appStore -q) if [ -z "$appStatusCheck" ]; then appOriginalStatus='stopped' echo "App $app is already stopped" else appOriginalStatus='started' echo "Stopping $app" ./runtipi-cli app stop $app:$appStore sleep $sleepDuration fi fi # Set destination app path backupAppPath="$backupPath/$appStore/$app" # Create destination directory if doesnt exist if [ ! -d "$backupAppPath" ]; then mkdir -p "$backupAppPath"; fi # Generate archive name backupFileName="$app-$backupType-$(date '+%Y-%m-%d').tar.gz" # Use backup directory as archive file destination archiveCreatingWorkDir="$backupAppPath" # Move to Working Directory for archive creation cd $archiveCreatingWorkDir # Declare source and destination path for directories in archive declare -A app_paths=( ["$appsPath/$appStore/$app"]="app" ["$appsDataPath/$appStore/$app"]="app-data" ["$userConfigsPath/$appStore/$app"]="user-config" ) echo "Preparing $app files" tempArchiveDir=$(mktemp -d) for src in "${!app_paths[@]}"; do dest="$tempArchiveDir/${app_paths[$src]}" echo $dest # Ensure the directory exists if [ -d "$src" ]; then # Create symbolic link for each directory ln -s "$src" "$dest" elif [ $src = "$userConfigsPath/$appStore/$app" ]; then : # echo "No user config for $app" else echo "Directory $src does not exist, skipped." fi done # Creating archive echo "Creating $app archive : $backupFileName" tar -czhf "$backupFileName" -C "$tempArchiveDir" . # Remove temporary Directory echo "Removing temporary files" rm -rf "$tempArchiveDir" # Purge old backups of the same type cd "$backupAppPath" echo "Purging old $backupType backup for $app" ls -t | grep "$app-$backupType-" | tail -n +$((keepLast+1)) | xargs -r rm -- # Restart the app if it was asked to be stopped if [ "$stopApp" = 'stop' -a "$appOriginalStatus" = 'started' ]; then cd $runtipiPath echo "Starting $app" ./runtipi-cli app start $app:$appStore sleep $sleepDuration fi fi done done

The script takes 3 arguments:

Accepted ValuesDescriptionDefaultRequired
monthly,weekly,dailySet the name of the final backup, for example if you use daily the backup name will be myapp-daily-date.tar.gzdailyno
stop,ignoreStop the app before backing up or backup anyway.stopno

Manage retention

The script allows you to change for how long you want to keep backups, this can be configured by chaning the following values:

dailyRetention=3 weeklyRetention=3 monthlyRetention=3

Each number indicates the retention period for the backup, specifying how many cycles it should be kept before deletion. For instance, a daily backup will be deleted after 3 days, while a monthly backup will be deleted after 3 months etc.

Apps selection

You can restrict the apps to backup with a list:

  • In the script, edit the backupListPath
  • Enter the path to a file describing what apps you want to backup, each app id in a new line.

Make sure to change the runtipiPath in the script to point to your actual Runtipi path.

Make the script executable

In order for cron to run the script we need to make it executable, this can easily be done with the following command:

chmod +x backup-apps.sh

Make sure you are in the runtipi/scripts directory before running the command above.

Add the script to cron

Now it’s time to add our script to crontab. We firstly need to open the crontab file using this command:

sudo crontab -e

Then we can add our crontab line, in this example we will backup our apps every day of the week at 2 a.m and the first day of the month at 3 p.m.

# Every day from Tuesday to Sunday at 2 AM - daily 0 2 * * 2-7 /path/to/runtipi/scripts/backup_runtipi_apps.sh daily # Every Monday at 2 AM - weekly 0 2 * * 1 /path/to/runtipi/scripts/backup_runtipi_apps.sh weekly # Every 1st day of the month at 3 AM - monthly 0 3 1 * * /path/to/runtipi/scripts/backup_runtipi_apps.sh monthly

After adding our line we can save and exit and that’s it! Now your apps will backup automatically!

Last updated on