mb-backup-manager/scripts/imports/view_snapshots.sh

172 lines
5.0 KiB
Bash
Raw Normal View History

2024-09-18 16:53:20 +00:00
#!/bin/bash
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_DIR="$HOME/mb-backups/logs"
LOG_FILE="${LOG_DIR}/restic_snapshot.log"
ERROR_LOG_FILE="${LOG_DIR}/restic_snapshot_error.log"
# Ensure log directory exists
mkdir -p "$LOG_DIR"
2025-01-07 17:15:11 +00:00
# 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
# Error logging function
log_error() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: $1" | tee -a "$ERROR_LOG_FILE"
}
# Function: Display usage and tags
2025-01-07 17:15:11 +00:00
display_usage() {
local tags
tags=$(fetch_available_tags)
2025-01-07 17:15:11 +00:00
echo "Usage: $0 <tag>"
echo "Available tags:"
if [ -z "$tags" ]; then
echo " (No tags found or repository inaccessible)"
else
echo " all"
echo "$tags" | sed 's/^/ /'
fi
2025-01-07 14:31:03 +00:00
exit 1
2025-01-07 17:15:11 +00:00
}
# Function: Fetch available tags
fetch_available_tags() {
log_message "Fetching available tags..."
local snapshot_json
# Retrieve JSON snapshot data
snapshot_json=$(RESTIC_PASSWORD=$(cat "$password_file") restic -r "$backupPath" snapshots --json 2>/dev/null)
if [ $? -ne 0 ]; then
log_error "Failed to retrieve snapshots. Check repository path or password."
return 1
fi
# Extract unique tags from JSON data
local tags
tags=$(echo "$snapshot_json" | jq -r '.[].tags[]' 2>/dev/null | sort -u | uniq)
if [ -z "$tags" ]; then
log_message "No tags found in repository snapshots."
fi
echo "$tags"
return 0
}
# Function: Validate repository access
validate_repository() {
log_message "Validating repository access..."
# Check if repository path exists
if [ ! -d "$backupPath" ]; then
log_error "Backup path '$backupPath' does not exist or is not accessible."
echo "Restic repository path not found. Please verify that the path exists and is mounted."
exit 1
fi
# Check if Restic can access the repository
if ! RESTIC_PASSWORD=$(cat "$password_file") restic -r "$backupPath" snapshots &>/dev/null; then
# Attempt to fix permissions automatically
log_message "Attempting to fix permissions for '$backupPath'..."
2025-02-07 13:38:11 +00:00
sudo chown -R "$(whoami)":"$(whoami)" "$backupPath"
sudo chmod -R u+rwX "$backupPath"
# Retry repository validation
if ! RESTIC_PASSWORD=$(cat "$password_file") restic -r "$backupPath" snapshots &>/dev/null; then
log_error "Unable to access Restic repository at '$backupPath'."
echo "Restic repository is inaccessible. Please check the path, permissions, or mount status."
2025-01-07 14:31:03 +00:00
exit 1
fi
fi
log_message "Repository access validated successfully."
}
# Function: Fix permissions if necessary
fix_permissions() {
log_message "Checking permissions for '$backupPath'..."
if [ ! -r "$backupPath" ] || [ ! -w "$backupPath" ]; then
log_message "Fixing permissions for '$backupPath'..."
2025-02-07 13:38:11 +00:00
sudo chown -R "$(whoami)":"$(whoami)" "$backupPath"
sudo chmod -R u+rwX "$backupPath"
fi
2025-01-07 17:15:11 +00:00
}
# Function: Display all snapshots
display_all_snapshots() {
log_message "Retrieving all snapshots..."
if RESTIC_PASSWORD=$(cat "$password_file") restic -r "$backupPath" snapshots --json | jq -r '.[] | "\(.short_id) \(.time) \(.tags | join(", "))"' 2>/dev/null; then
log_message "All snapshots displayed successfully."
else
log_error "Unable to retrieve all snapshots."
exit 1
fi
}
# Function: Validate environment
2025-01-07 17:15:11 +00:00
validate_environment() {
if [ ! -f "$password_file" ]; then
log_error "Password file not found at $password_file"
2025-01-07 17:15:11 +00:00
exit 1
fi
if [ ! -d "$backupPath" ]; then
log_error "Backup path '$backupPath' does not exist or is not accessible."
2025-01-07 17:15:11 +00:00
exit 1
fi
validate_repository
fix_permissions
2025-01-07 17:15:11 +00:00
}
# Main script logic
main() {
# If no arguments are provided, display usage
if [ "$#" -eq 0 ]; then
2025-01-07 17:15:11 +00:00
display_usage
fi
# Assign input argument
2025-02-07 13:38:11 +00:00
TAG="$1"
# Map alias tags to show all snapshots
if [ "$TAG" == "full_backup" ] || [ "$TAG" == "everything" ]; then
TAG="all"
fi
2025-01-07 17:15:11 +00:00
# Validate environment and dependencies
validate_environment
# Handle the special case for "all"
if [ "$TAG" == "all" ]; then
display_all_snapshots
exit 0
fi
2025-01-07 17:15:11 +00:00
# Validate the tag
log_message "Validating tag '$TAG'..."
local available_tags
available_tags=$(fetch_available_tags)
if ! echo "$available_tags" | grep -qw "$TAG"; then
log_error "Unknown or unused tag '$TAG'."
2025-01-07 17:15:11 +00:00
display_usage
fi
# Display snapshots for the provided tag
log_message "Retrieving snapshots for tag '$TAG'..."
if RESTIC_PASSWORD=$(cat "$password_file") restic -r "$backupPath" snapshots --tag "$TAG" --json | jq -r '.[] | "\(.short_id) \(.time) \(.tags | join(", "))"' 2>/dev/null; then
2025-01-07 17:15:11 +00:00
log_message "Snapshots for tag '$TAG' displayed successfully."
else
log_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 main function
2025-01-07 17:15:11 +00:00
main "$@"