Ver 1.8 Improved Snapshot Display

main
Anthony 2025-01-08 01:15:11 +08:00
parent a36be25ae9
commit dc085515a5
3 changed files with 115 additions and 39 deletions

View File

@ -1,5 +1,45 @@
# Changelog
## Version 1.8
### Added
- **`view_snapshots.sh` Enhancements**:
- Introduced support for a new `all` tag to display all snapshots regardless of specific tags.
- Added functionality to dynamically fetch and display all available tags in the repository, improving usability and flexibility.
- Enhanced error handling for empty repositories or inaccessible snapshots with detailed log messages.
- **Improved Snapshot Display**:
- For the `all` tag, all snapshots are displayed in a formatted output including `short_id`, `time`, and associated tags.
- Enhanced the script to log raw snapshot data during errors for easier debugging.
### Fixed
- Resolved issues with fetching available tags when no snapshots existed in the repository, ensuring consistent script behavior.
- Fixed a missing password issue when executing `restic -r /mnt/backups snapshots --json`, ensuring `RESTIC_PASSWORD` is securely passed in all cases.
- Corrected formatting and display of available tags to ensure no duplicate or unnecessary entries are shown.
### Updated
- **Logging and Usability**:
- Improved log formatting to include more descriptive timestamps and detailed error or success messages for each step.
- Standardized output for snapshot retrieval by tag, including snapshot ID, timestamp, and tags in a clean and readable format.
- **Environment Validation**:
- Updated `view_snapshots.sh` to ensure all necessary environment variables (`RESTIC_REPOSITORY`, `RESTIC_PASSWORD`) are set before executing Restic commands.
- Validated repository access and password file existence at the start of the script to avoid runtime errors.
### Improved
- **Modularity and Robustness**:
- Refactored `view_snapshots.sh` to handle dynamic tags more effectively, making it easier to extend or customize in the future.
- Centralized dependency validation (`restic`, `jq`) and repository checks to reduce redundancy across script operations.
- **Error Handling**:
- Added detailed error messages when repository access fails or tags cannot be fetched, ensuring issues are easy to debug.
- Ensured graceful script exits with appropriate logs when critical validation checks fail.
- **Code Maintenance**:
- Reduced redundancy by consolidating snapshot and tag-related logic into reusable functions.
- Enhanced maintainability by improving script readability, modularity, and inline documentation.
## Version 1.7
### Added

View File

@ -1,5 +1,5 @@
type: update
jpsVersion: 1.7
jpsVersion: 1.8
name: MightyBox WordPress Backup/Restore Addon
id: mb-backup-manager
description: Custom Backup and Restore Addon for WordPress using Restic. Supports backing up databases, core files, media files, and full backups with scheduling and retention policies.
@ -113,10 +113,10 @@ menu:
title: Restore from Snapshot
submitButtonText: Restore Now
- caption: View Full Backups
action: viewFullBackups
confirmText: Are you sure you want to view full backups?
successText: Full backups listed successfully.
- caption: View All Backups
action: viewAllBackups
confirmText: Are you sure you want to view all backups?
successText: All backups listed successfully.
- caption: View Core Backups
action: viewCoreBackups
@ -230,7 +230,7 @@ actions:
type: info
message: "${response.out}"
viewFullBackups:
viewAllBackups:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/view_snapshots.sh main_backup

View File

@ -3,42 +3,78 @@
# Exit immediately on error
set -e
# Display usage if no tag is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <tag>"
echo "Available tags: main_backup, wordpress_db, core_files, media_themes, full_backup"
exit 1
fi
# Assign input argument
TAG=$1
# Configuration
backupPath='/mnt/backups'
password_file="/etc/restic-password" # Use a password file instead of embedding the password
password_file="/etc/restic-password"
LOG_FILE="/var/log/restic_snapshot.log"
# Validate if the password file exists
if [ ! -f "$password_file" ]; then
echo "Error: Password file not found at $password_file"
# 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 <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"
exit 1
fi
}
# Set Restic environment variables
export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD=$(cat "$password_file")
# Validate the provided tag against known tags
case "$TAG" in
main_backup|wordpress_db|core_files|media_themes|full_backup)
# Use the --json flag to output the snapshots in JSON format
if restic -r "$RESTIC_REPOSITORY" snapshots --tag "$TAG" --json; then
echo "Snapshots for tag '$TAG' displayed successfully."
else
echo "Error: Unable to display snapshots for tag '$TAG'."
# 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
;;
*)
echo "Error: Unknown tag '$TAG'."
echo "Available tags: main_backup, wordpress_db, core_files, media_themes, full_backup"
done
}
# Validate environment
validate_environment() {
if [ ! -f "$password_file" ]; then
log_message "ERROR: Password file not found at $password_file"
exit 1
;;
esac
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 "$@"