check_repo_stats
parent
3c87bbd882
commit
b4b2240fac
|
|
@ -94,71 +94,50 @@ validate_dependencies() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function: Validate repository access with robust password handling
|
# Function: Validate repository access - simplified approach based on working check_backup_repo.sh
|
||||||
validate_repository() {
|
validate_repository() {
|
||||||
log_message "Attempting to access Restic 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
|
local restic_password
|
||||||
if ! restic_password=$(get_restic_password); then
|
if ! restic_password=$(get_restic_password); then
|
||||||
log_message "ERROR: Unable to access Restic password from any source."
|
log_message "WARNING: Unable to access password from robust methods, trying simple fallback..."
|
||||||
log_message "Checked locations:"
|
|
||||||
log_message " - Primary: $PASSWORD_FILE"
|
# Fallback to simple password access (like working script)
|
||||||
log_message " - Environment: RESTIC_PASSWORD"
|
if [ ! -f "$PASSWORD_FILE" ]; then
|
||||||
log_message " - Alternative locations in home directories"
|
log_message "Password file not found. Creating a new one with a default password."
|
||||||
log_message "Current user: $(whoami) (UID: $EUID)"
|
echo "default-password" > "$PASSWORD_FILE"
|
||||||
log_message "Password file permissions: $(ls -la $PASSWORD_FILE 2>/dev/null || echo 'File not found')"
|
fi
|
||||||
exit 1
|
restic_password=$(cat "$PASSWORD_FILE")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Export password and repository
|
# Export password and repository (like working script)
|
||||||
export RESTIC_PASSWORD="$restic_password"
|
export RESTIC_PASSWORD="$restic_password"
|
||||||
export RESTIC_REPOSITORY="$BACKUP_REPO_PATH"
|
export RESTIC_REPOSITORY="$BACKUP_REPO_PATH"
|
||||||
|
|
||||||
# Set the password and repository
|
# Use the same logic as the working check_backup_repo.sh
|
||||||
export RESTIC_PASSWORD=$(cat /etc/restic-password)
|
# Check if the repository contains files
|
||||||
export RESTIC_REPOSITORY="/mnt/backups"
|
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..."
|
||||||
|
|
||||||
# Initialize the repository
|
# Test if repository is accessible
|
||||||
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"
|
|
||||||
|
|
||||||
# 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
|
|
||||||
if ! restic snapshots &>/dev/null; then
|
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
|
exit 1
|
||||||
fi
|
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
|
fi
|
||||||
|
|
||||||
log_message "Repository access validated successfully."
|
log_message "Repository access validated successfully."
|
||||||
|
|
@ -186,7 +165,7 @@ apply_retention_policy() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function: Check for stale locks
|
# Function: Check for stale locks (simplified approach)
|
||||||
check_and_remove_stale_locks() {
|
check_and_remove_stale_locks() {
|
||||||
log_message "Checking for stale locks in the repository..."
|
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
|
if [ -d "${BACKUP_REPO_PATH}/locks" ] && [ "$(ls -A "${BACKUP_REPO_PATH}/locks" 2>/dev/null)" ]; then
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue