check_repo_stats

main
Anthony 2025-06-03 02:11:11 +08:00
parent 3c87bbd882
commit b4b2240fac
1 changed files with 32 additions and 53 deletions

View File

@ -94,71 +94,50 @@ validate_dependencies() {
done
}
# Function: Validate repository access with robust password handling
# Function: Validate repository access - simplified approach based on working check_backup_repo.sh
validate_repository() {
log_message "Attempting to access Restic repository..."
# Get password using robust method
# Ensure the backup repository path exists
mkdir -p "$BACKUP_REPO_PATH"
# Get password using robust method, but fallback to simple method if needed
local restic_password
if ! restic_password=$(get_restic_password); then
log_message "ERROR: Unable to access Restic password from any source."
log_message "Checked locations:"
log_message " - Primary: $PASSWORD_FILE"
log_message " - Environment: RESTIC_PASSWORD"
log_message " - Alternative locations in home directories"
log_message "Current user: $(whoami) (UID: $EUID)"
log_message "Password file permissions: $(ls -la $PASSWORD_FILE 2>/dev/null || echo 'File not found')"
exit 1
log_message "WARNING: Unable to access password from robust methods, trying simple fallback..."
# Fallback to simple password access (like working script)
if [ ! -f "$PASSWORD_FILE" ]; then
log_message "Password file not found. Creating a new one with a default password."
echo "default-password" > "$PASSWORD_FILE"
fi
restic_password=$(cat "$PASSWORD_FILE")
fi
# Export password and repository
# Export password and repository (like working script)
export RESTIC_PASSWORD="$restic_password"
export RESTIC_REPOSITORY="$BACKUP_REPO_PATH"
# Set the password and repository
export RESTIC_PASSWORD=$(cat /etc/restic-password)
export RESTIC_REPOSITORY="/mnt/backups"
# Initialize the repository
restic init
# Verify it worked
restic snapshots
# Test repository access
if ! restic snapshots &>/dev/null; then
log_message "ERROR: Unable to access the Restic repository at $BACKUP_REPO_PATH"
log_message "Repository path exists: $([ -d "$BACKUP_REPO_PATH" ] && echo 'Yes' || echo 'No')"
log_message "Repository contents: $(ls -la "$BACKUP_REPO_PATH" 2>/dev/null | wc -l || echo '0') items"
# Use the same logic as the working check_backup_repo.sh
# Check if the repository contains files
if [ "$(find "$BACKUP_REPO_PATH" -mindepth 1 2>/dev/null | wc -l)" -gt 0 ]; then
log_message "Repository directory contains files. Checking if it's a valid Restic repository..."
# Check if repository needs initialization
if [ ! -d "$BACKUP_REPO_PATH" ] || [ -z "$(ls -A "$BACKUP_REPO_PATH" 2>/dev/null)" ]; then
log_message "Repository appears to be empty or non-existent. Initializing repository..."
if restic init; then
log_message "Repository initialized successfully."
else
log_message "ERROR: Failed to initialize repository."
exit 1
fi
else
# Repository exists but might not be a valid Restic repository
log_message "Repository directory exists but may not be a valid Restic repository."
log_message "Attempting to initialize as new repository..."
if restic init; then
log_message "Repository initialized successfully."
else
log_message "ERROR: Repository exists but is not accessible. Manual intervention required."
log_message "Directory contents:"
ls -la "$BACKUP_REPO_PATH" 2>/dev/null || echo "Cannot list directory"
exit 1
fi
fi
# Test access again after initialization
# Test if repository is accessible
if ! restic snapshots &>/dev/null; then
log_message "ERROR: Repository initialization succeeded but still cannot access repository."
log_message "ERROR: Repository directory exists but is not a valid Restic repository."
log_message "Repository contents: $(ls -la "$BACKUP_REPO_PATH" 2>/dev/null | head -10)"
exit 1
fi
log_message "Valid Restic repository found and accessible."
else
# Initialize a new Restic repository if empty (same as working script)
log_message "No files found in the backup repository. Initializing a new repository..."
if ! restic init; then
log_message "ERROR: Failed to initialize the backup repository."
exit 1
fi
log_message "Backup repository initialized successfully."
fi
log_message "Repository access validated successfully."
@ -186,7 +165,7 @@ apply_retention_policy() {
fi
}
# Function: Check for stale locks
# Function: Check for stale locks (simplified approach)
check_and_remove_stale_locks() {
log_message "Checking for stale locks in the repository..."
if [ -d "${BACKUP_REPO_PATH}/locks" ] && [ "$(ls -A "${BACKUP_REPO_PATH}/locks" 2>/dev/null)" ]; then