Skip to content

Instantly share code, notes, and snippets.

@bukowa
Created October 27, 2025 19:33
Show Gist options
  • Select an option

  • Save bukowa/b34f39c4e5b331de7f2fbeb0f1dcdc0d to your computer and use it in GitHub Desktop.

Select an option

Save bukowa/b34f39c4e5b331de7f2fbeb0f1dcdc0d to your computer and use it in GitHub Desktop.
backup octoprint
#!/bin/bash
set -e
# --- Configuration ---
CONTAINER_NAME="octoprint"
BACKUP_FILENAME="/tmp/octoprint-full-snapshot-$(date +%F).tar.gz"
TEMP_DIR="/tmp/octoprint_backup_staging"
# --- This script MUST be run as root/sudo ---
if [ "$EUID" -ne 0 ]; then
echo "❌ This script must be run with sudo. Please run: sudo ./your_script_name.sh"
exit 1
fi
echo "--- Starting OctoPrint Full Snapshot Backup (as root) ---"
# 1. Forcefully clean up any old files from previous runs
echo "--> Cleaning up any previous temporary files..."
rm -rf "${TEMP_DIR}"
rm -f "${BACKUP_FILENAME}"
# 2. Stop the container to ensure data consistency
echo "--> Stopping the OctoPrint container..."
docker stop ${CONTAINER_NAME}
# 3. Gather critical information
echo "--> Gathering container information..."
IMAGE_SHA=$(docker inspect --format='{{.Image}}' ${CONTAINER_NAME})
DATA_PATH=$(docker inspect --format='{{range .Mounts}}{{.Source}}{{end}}' ${CONTAINER_NAME})
# 4. Prepare staging area
echo "--> Creating temporary staging directory..."
mkdir -p "${TEMP_DIR}"
echo "${IMAGE_SHA}" > "${TEMP_DIR}/image_sha.txt"
# 5. Create the compressed archive
echo "--> Creating compressed backup archive at ${BACKUP_FILENAME}..."
tar -czf "${BACKUP_FILENAME}" -C "${TEMP_DIR}" image_sha.txt -C "${DATA_PATH}/.." ./${DATA_PATH##*/}
# 6. Restart the container to minimize downtime
echo "--> Backup created. Restarting OctoPrint container..."
docker start ${CONTAINER_NAME}
# --- Final Instructions ---
echo ""
echo "--- ✅ SUCCESS! Backup Complete ---"
echo "The backup is located at: ${BACKUP_FILENAME}"
echo ""
echo "On your LOCAL computer, run this command to download it."
echo "Replace 'user@remote-host' with your actual login details:"
echo "------------------------------------------------------------------"
echo "scp user@remote-host:${BACKUP_FILENAME} ."
echo "------------------------------------------------------------------"
echo ""
echo "After downloading, run this command on the REMOTE server to clean up:"
echo "------------------------------------------------------------------"
echo "sudo rm ${BACKUP_FILENAME} && sudo rm -rf ${TEMP_DIR}"
echo "------------------------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment