mirror of
https://github.com/acedanger/shell.git
synced 2026-03-27 06:16:09 -07:00
fix(backup-gitea): timestamped log, safe .env loading, fix $? anti-pattern, add archive verification and NAS log copy
This commit is contained in:
@@ -21,9 +21,10 @@ COMPOSE_DIR="/home/acedanger/docker/gitea"
|
|||||||
|
|
||||||
BACKUP_DIR="/home/acedanger/backups/gitea"
|
BACKUP_DIR="/home/acedanger/backups/gitea"
|
||||||
NAS_DIR="/mnt/share/media/backups/gitea"
|
NAS_DIR="/mnt/share/media/backups/gitea"
|
||||||
|
NAS_LOG_DIR="/mnt/share/media/backups/logs"
|
||||||
COMPOSE_FILE="$COMPOSE_DIR/docker-compose.yml"
|
COMPOSE_FILE="$COMPOSE_DIR/docker-compose.yml"
|
||||||
LOG_FILE="$SCRIPT_DIR/logs/gitea-backup.log"
|
|
||||||
DATE=$(date +%Y%m%d_%H%M%S)
|
DATE=$(date +%Y%m%d_%H%M%S)
|
||||||
|
LOG_FILE="$SCRIPT_DIR/logs/gitea-backup-${DATE}.log"
|
||||||
|
|
||||||
# Ensure directories exist
|
# Ensure directories exist
|
||||||
mkdir -p "$(dirname "$LOG_FILE")"
|
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
|
# Load .env variables from the COMPOSE_DIR to ensure DB credentials match
|
||||||
if [ -f "$COMPOSE_DIR/.env" ]; then
|
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
|
fi
|
||||||
|
|
||||||
# Logging function (Fixed to interpret colors correctly)
|
# Logging function (Fixed to interpret colors correctly)
|
||||||
@@ -143,6 +147,14 @@ perform_backup() {
|
|||||||
# Tar the temp folder into one final file
|
# Tar the temp folder into one final file
|
||||||
tar -czf "$BACKUP_DIR/$FINAL_ARCHIVE_NAME" -C "$TEMP_BACKUP_PATH" .
|
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
|
# Remove temp folder
|
||||||
rm -rf "$TEMP_BACKUP_PATH"
|
rm -rf "$TEMP_BACKUP_PATH"
|
||||||
|
|
||||||
@@ -152,8 +164,7 @@ perform_backup() {
|
|||||||
if [[ "$SKIP_NAS" != "true" ]]; then
|
if [[ "$SKIP_NAS" != "true" ]]; then
|
||||||
if [ -d "$NAS_DIR" ]; then
|
if [ -d "$NAS_DIR" ]; then
|
||||||
log "Copying to NAS ($NAS_DIR)..."
|
log "Copying to NAS ($NAS_DIR)..."
|
||||||
cp "$BACKUP_DIR/$FINAL_ARCHIVE_NAME" "$NAS_DIR/"
|
if cp "$BACKUP_DIR/$FINAL_ARCHIVE_NAME" "$NAS_DIR/"; then
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
log "${GREEN}NAS Copy Successful.${NC}"
|
log "${GREEN}NAS Copy Successful.${NC}"
|
||||||
else
|
else
|
||||||
log "${RED}NAS Copy Failed. Check permissions on $NAS_DIR${NC}"
|
log "${RED}NAS Copy Failed. Check permissions on $NAS_DIR${NC}"
|
||||||
@@ -170,6 +181,14 @@ perform_backup() {
|
|||||||
log "Cleanup of old local backups complete."
|
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
|
# Function to generate the restore script
|
||||||
create_restore_script() {
|
create_restore_script() {
|
||||||
local TARGET_DIR=$1
|
local TARGET_DIR=$1
|
||||||
@@ -211,6 +230,7 @@ check_dependencies
|
|||||||
# Parse Arguments
|
# Parse Arguments
|
||||||
if [ $# -eq 0 ]; then
|
if [ $# -eq 0 ]; then
|
||||||
perform_backup "false"
|
perform_backup "false"
|
||||||
|
copy_logs_to_nas
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -218,7 +238,7 @@ while [[ "$#" -gt 0 ]]; do
|
|||||||
case $1 in
|
case $1 in
|
||||||
-h|--help) usage; exit 0 ;;
|
-h|--help) usage; exit 0 ;;
|
||||||
-l|--list) list_backups; 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 ;;
|
*) echo "Unknown parameter: $1"; usage; exit 1 ;;
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
|
|||||||
Reference in New Issue
Block a user