Created
July 11, 2025 17:43
-
-
Save wojtekadams/0d9087e6150437eb059dd1a28dae0c4b to your computer and use it in GitHub Desktop.
Script for Turris Omnia routers creates a schnapps snapshot, exports it, saves it to /srv/backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| LOGGER_TAG="cron-auto-snapshot" | |
| DESCRIPTION="Auto snapshot $TIMESTAMP by cron" | |
| TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") | |
| BACKUP_DIR="/srv/backups/" | |
| MAX_BACKUPS=5 | |
| if [ ! -d "$BACKUP_DIR" ]; then | |
| mkdir -p "$BACKUP_DIR" || { logger "ERROR: Failed to create the directory."; exit 1; } | |
| fi | |
| SNAPSHOT_ID=$(schnapps create "$DESCRIPTION" | awk '/Snapshot number/ {print $3}') | |
| if [ -z "$SNAPSHOT_ID" ]; then | |
| logger -t ${LOGGER_TAG} "ERROR: Failed to create the snapshot." | |
| exit 1 | |
| fi | |
| logger -t ${LOGGER_TAG} "Snapshot created: ID $SNAPSHOT_ID" | |
| EXPORT_FILE="${BACKUP_DIR}/omnia-medkit-turris-${SNAPSHOT_ID}.tar.gz" | |
| schnapps export "$SNAPSHOT_ID" "$BACKUP_DIR" | |
| if [ ! -f "$EXPORT_FILE" ]; then | |
| logger -t ${LOGGER_TAG} "ERROR: Snapshot export failed.." | |
| exit 1 | |
| fi | |
| logger -t ${LOGGER_TAG} "snapshot finshed" | |
| BACKUP_FILES=($(ls -1t "${BACKUP_DIR}"/omnia-medkit-turris-*.tar.gz 2>/dev/null)) | |
| NUM_BACKUPS=${#BACKUP_FILES[@]} | |
| if (( NUM_BACKUPS > MAX_BACKUPS )); then | |
| TO_DELETE=("${BACKUP_FILES[@]:$MAX_BACKUPS}") | |
| for f in "${TO_DELETE[@]}"; do | |
| logger -t ${LOGGER_TAG} "Removing old snapshot: $f" | |
| rm -f "$f" | |
| done | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment