fix(backup-gitea): timestamped log, safe .env loading, fix $? anti-pattern, add archive verification and NAS log copy

This commit is contained in:
Peter Wood
2026-03-25 21:23:56 -04:00
parent 5f7c0ad866
commit 74a2453edc

View File

@@ -21,9 +21,10 @@ COMPOSE_DIR="/home/acedanger/docker/gitea"
BACKUP_DIR="/home/acedanger/backups/gitea"
NAS_DIR="/mnt/share/media/backups/gitea"
NAS_LOG_DIR="/mnt/share/media/backups/logs"
COMPOSE_FILE="$COMPOSE_DIR/docker-compose.yml"
LOG_FILE="$SCRIPT_DIR/logs/gitea-backup.log"
DATE=$(date +%Y%m%d_%H%M%S)
LOG_FILE="$SCRIPT_DIR/logs/gitea-backup-${DATE}.log"
# Ensure directories exist
mkdir -p "$(dirname "$LOG_FILE")"
@@ -31,7 +32,10 @@ mkdir -p "$BACKUP_DIR"
# Load .env variables from the COMPOSE_DIR to ensure DB credentials match
if [ -f "$COMPOSE_DIR/.env" ]; then
export $(grep -v '^#' "$COMPOSE_DIR/.env" | xargs)
# shellcheck source=/dev/null
set -a
source "$COMPOSE_DIR/.env"
set +a
fi
# Logging function (Fixed to interpret colors correctly)
@@ -143,6 +147,14 @@ perform_backup() {
# Tar the temp folder into one final file
tar -czf "$BACKUP_DIR/$FINAL_ARCHIVE_NAME" -C "$TEMP_BACKUP_PATH" .
# Verify archive integrity
if ! gzip -t "$BACKUP_DIR/$FINAL_ARCHIVE_NAME" 2>/dev/null; then
log "${RED}Error: Archive verification failed for $FINAL_ARCHIVE_NAME${NC}"
rm -f "$BACKUP_DIR/$FINAL_ARCHIVE_NAME"
rm -rf "$TEMP_BACKUP_PATH"
exit 1
fi
# Remove temp folder
rm -rf "$TEMP_BACKUP_PATH"
@@ -152,8 +164,7 @@ perform_backup() {
if [[ "$SKIP_NAS" != "true" ]]; then
if [ -d "$NAS_DIR" ]; then
log "Copying to NAS ($NAS_DIR)..."
cp "$BACKUP_DIR/$FINAL_ARCHIVE_NAME" "$NAS_DIR/"
if [ $? -eq 0 ]; then
if cp "$BACKUP_DIR/$FINAL_ARCHIVE_NAME" "$NAS_DIR/"; then
log "${GREEN}NAS Copy Successful.${NC}"
else
log "${RED}NAS Copy Failed. Check permissions on $NAS_DIR${NC}"
@@ -170,6 +181,14 @@ perform_backup() {
log "Cleanup of old local backups complete."
}
# Copy log file to NAS logs directory
copy_logs_to_nas() {
if [[ -f "$LOG_FILE" && -d "$NAS_LOG_DIR" ]]; then
cp "$LOG_FILE" "$NAS_LOG_DIR/" 2>/dev/null || \
log "${YELLOW}Warning: Could not copy log to NAS logs directory${NC}"
fi
}
# Function to generate the restore script
create_restore_script() {
local TARGET_DIR=$1
@@ -211,6 +230,7 @@ check_dependencies
# Parse Arguments
if [ $# -eq 0 ]; then
perform_backup "false"
copy_logs_to_nas
exit 0
fi
@@ -218,7 +238,7 @@ while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) usage; exit 0 ;;
-l|--list) list_backups; exit 0 ;;
-n|--no-nas) perform_backup "true"; exit 0 ;;
-n|--no-nas) perform_backup "true"; copy_logs_to_nas; exit 0 ;;
*) echo "Unknown parameter: $1"; usage; exit 1 ;;
esac
shift