2024-09-18 16:53:20 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2025-01-07 14:31:03 +00:00
|
|
|
# Exit immediately on error
|
|
|
|
set -e
|
|
|
|
|
2025-01-07 17:15:11 +00:00
|
|
|
# Configuration
|
2024-09-18 16:53:20 +00:00
|
|
|
backupPath='/mnt/backups'
|
2025-01-07 17:15:11 +00:00
|
|
|
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"
|
|
|
|
}
|
2024-09-18 16:53:20 +00:00
|
|
|
|
2025-01-07 17:15:11 +00:00
|
|
|
# Function: Display usage
|
|
|
|
display_usage() {
|
|
|
|
echo "Usage: $0 <tag>"
|
|
|
|
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"
|
2025-01-07 14:31:03 +00:00
|
|
|
exit 1
|
2025-01-07 17:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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."
|
2025-01-07 14:31:03 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2025-01-07 17:15:11 +00:00
|
|
|
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'."
|
2024-09-18 16:53:20 +00:00
|
|
|
exit 1
|
2025-01-07 17:15:11 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run validations and execute the main function
|
|
|
|
validate_dependencies
|
|
|
|
validate_environment
|
|
|
|
main "$@"
|