#!/bin/bash # Exit immediately on error set -e # Configuration backupPath='/mnt/backups' password_file="/etc/restic-password" LOG_FILE="/var/log/restic_snapshot.log" # Logging function log_message() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" } # Function: Display usage display_usage() { echo "Usage: $0 " echo "Available tags:" restic -r "$RESTIC_REPOSITORY" snapshots --json 2>/dev/null | jq -r '.[].tags[]' | sort -u || echo "main_backup, wordpress_db, core_files, media_themes, full_backup" exit 1 } # Ensure dependencies are installed validate_dependencies() { for cmd in restic jq; do if ! command -v "$cmd" &>/dev/null; then log_message "ERROR: Required command '$cmd' not found. Please install it." exit 1 fi done } # Validate environment validate_environment() { if [ ! -f "$password_file" ]; then log_message "ERROR: Password file not found at $password_file" exit 1 fi if [ ! -d "$backupPath" ]; then log_message "ERROR: Backup path '$backupPath' does not exist or is not accessible." exit 1 fi } # Main script logic main() { # Ensure a tag is provided if [ "$#" -ne 1 ]; then display_usage fi # Assign input argument TAG=$1 # Set Restic environment variables export RESTIC_REPOSITORY="$backupPath" export RESTIC_PASSWORD=$(cat "$password_file") # Validate the tag if ! restic -r "$RESTIC_REPOSITORY" snapshots --json 2>/dev/null | jq -e ".[] | select(.tags[] == \"$TAG\")" &>/dev/null; then log_message "ERROR: Unknown or unused tag '$TAG'." display_usage fi # Display snapshots for the provided tag log_message "Retrieving snapshots for tag '$TAG'..." if restic -r "$RESTIC_REPOSITORY" snapshots --tag "$TAG" --json | jq -r '.[] | "\(.short_id) \(.time) \(.tags | join(", "))"' 2>/dev/null; then log_message "Snapshots for tag '$TAG' displayed successfully." else log_message "ERROR: Unable to display snapshots for tag '$TAG'." exit 1 fi } # Run validations and execute the main function validate_dependencies validate_environment main "$@"