Auto update your instance with cron
This guide will help you to setup a cron job to automatically update your instance for minor and security updates. It will check periodically if a new minor or security release is available and if so, it will update your instance.
Thanks to DansNewLegs for putting this guide together.
Setup
Create the auto-update script
Create a new script file in a new runtipi/scripts
folder called auto-update.sh
and paste the following code:
#!/usr/bin/env bash
compare_major_version() {
local major_version1=$(echo "$1" | cut -d. -f1)
local major_version2=$(echo "$2" | cut -d. -f1)
if [[ "$major_version1" == "$major_version2" ]]; then
return 0 # Success (major versions match)
else
return 1 # Failure (major versions differ)
fi
}
# IMPORTANT: Change this to the path of your Runtipi installation.
runtipi_path="/opt/runtipi"
# Validate Runtipi path and VERSION file
if [[ ! -d "$runtipi_path" ]]; then
echo "Error: Runtipi path '$runtipi_path' not found or is not a directory." >&2
echo "Please correct the 'runtipi_path' variable in the script." >&2
exit 1
fi
version_file="$runtipi_path/VERSION"
if [[ ! -f "$version_file" ]]; then
echo "Error: Version file '$version_file' not found." >&2
exit 1
fi
# Get Current Version of Runtipi
current_version=$(cat "$version_file")
if [[ -z "$current_version" ]]; then
echo "Error: Could not read current version from '$version_file'." >&2
exit 1
fi
echo "Current version: $current_version"
# Get the latest release information from GitHub API
echo "Fetching latest release info..."
latest_release=$(curl -fsSL \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/runtipi/runtipi/releases/latest)
if [[ $? -ne 0 || -z "$latest_release" ]]; then
echo "Error: Failed to fetch release information from GitHub." >&2
exit 1
fi
tag_name=$(echo "$latest_release" | grep -o '"tag_name": "[^"]*' | cut -d'"' -f4)
if [[ -z "$tag_name" ]]; then
echo "Error: Could not extract tag_name from GitHub API response." >&2
exit 1
fi
echo "Latest version: $tag_name"
compare_major_version "$tag_name" "$current_version"
major_version_match=$?
is_newer=1
if [[ "$tag_name" != "$current_version" ]]; then
latest_sorted=$(printf '%s\n%s\n' "$current_version" "$tag_name" | sort -V | tail -n1)
if [[ "$latest_sorted" == "$tag_name" ]]; then
is_newer=0
fi
fi
if [[ $major_version_match -eq 0 ]] && [[ $is_newer -eq 0 ]]; then
echo "A new compatible release is available: $tag_name"
runtipi_cli="$runtipi_path/runtipi-cli"
if [[ ! -x "$runtipi_cli" ]]; then
echo "Error: Runtipi CLI '$runtipi_cli' not found or not executable." >&2
exit 1
fi
cd "$runtipi_path" || exit 1
echo "Backing up current version..."
backup_dir="backups"
backup_file="runtipi-backup-$current_version.tar.gz"
backup_path="$backup_dir/$backup_file"
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
echo "Creating backup: $backup_path"
if tar -czf "$backup_path" --exclude=./media --exclude=./"$backup_dir" .; then
echo "Backup successful."
echo "Starting update..."
if "$runtipi_cli" update latest; then
echo "Update command finished."
else
echo "Error: Runtipi update command failed. Check output above." >&2
echo "Restore from backup '$backup_path' if necessary." >&2
exit 1
fi
else
echo "Error: Failed to create backup archive '$backup_path'." >&2
exit 1
fi
else
if [[ "$tag_name" == "$current_version" ]]; then
echo "You are already running the latest version ($current_version)."
elif [[ $major_version_match -ne 0 ]]; then
echo "No new release found. Major version mismatch (Current: $current_version, Latest: $tag_name)."
else
echo "No new release found."
fi
fi
echo "Update check complete."
exit 0
This script will check if a new release is available and if so, it will backup your current installation and update to the latest release.
Make sure to change the runtipi_path
variable to the path of your Runtipi
installation.
Make the script executable
Make the script executable by running the following command:
chmod +x ./auto-update.sh
Create a cron job
Open your crontab file by running the following command:
sudo crontab -e
Add a new line at the bottom of the file with the following content:
0 4 * * * /path/to/runtipi/scripts/auto-update.sh
Or if you want to log the output of the script to a file for debugging purposes:
0 4 * * * /path/to/runtipi/scripts/auto-update.sh >> /path/to/runtipi/scripts/auto-update.log 2>&1
This will run the script every day at 4am. You can change the time to your liking. Crontab Guru is a great tool to help you with creating cron jobs.