diff --git a/backup-golinks.sh b/backup-golinks.sh new file mode 100755 index 0000000..cf14da5 --- /dev/null +++ b/backup-golinks.sh @@ -0,0 +1,141 @@ +#!/bin/bash + +# backup-golinks.sh - Backup golinks export +# Enhanced for NAS support and consistency + +set -e + +# Colors for output +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +RED='\033[0;31m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# ========================================== +# 1. CONFIGURATION +# ========================================== +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +BACKUP_DIR="/home/acedanger/backups/golinks" +NAS_DIR="/mnt/share/media/backups/golinks" +GOLINKS_URL="http://go/.export" +LOG_FILE="$SCRIPT_DIR/logs/golinks-backup.log" +DATE=$(date +%Y%m%d_%H%M%S) +BACKUP_FILENAME="golinks_backup_${DATE}.json" + +# Ensure directories exist +mkdir -p "$(dirname "$LOG_FILE")" +mkdir -p "$BACKUP_DIR" + +# Logging function +log() { + # Print to console with colors (interpreting escapes with -e) + echo -e "$(date --iso-8601=seconds) - $1" + # Strip colors for the log file to keep it clean + echo -e "$(date --iso-8601=seconds) - $1" | sed 's/\x1b\[[0-9;]*m//g' >> "$LOG_FILE" +} + +# Display usage information +usage() { + echo "Usage: $0 [OPTIONS]" + echo "" + echo "Backup golinks export" + echo "Options:" + echo " -h, --help Show this help message" + echo " -l, --list List available local backups" + echo " -n, --no-nas Skip copying to NAS (Local only)" + echo "" +} + +# Check dependencies +check_dependencies() { + if ! command -v curl &> /dev/null; then + log "${RED}Error: curl is not installed.${NC}" + exit 1 + fi +} + +# List available backups +list_backups() { + echo -e "${BLUE}=== Available Golinks Backups (Local) ===${NC}" + ls -lh "$BACKUP_DIR"/* 2>/dev/null || echo "No backups found." +} + +# ========================================== +# 2. BACKUP LOGIC +# ========================================== +perform_backup() { + local SKIP_NAS=$1 + + log "Starting backup process..." + + LOCAL_FILE="$BACKUP_DIR/$BACKUP_FILENAME" + + # Download golinks export + log "${YELLOW}Downloading golinks export to: $LOCAL_FILE${NC}" + if curl -L -f "$GOLINKS_URL" -o "$LOCAL_FILE"; then + if [ ! -s "$LOCAL_FILE" ]; then + log "${RED}Error: Downloaded file is empty${NC}" + rm -f "$LOCAL_FILE" + exit 1 + fi + log "${GREEN}Download completed successfully${NC}" + FILE_SIZE=$(du -h "$LOCAL_FILE" | cut -f1) + log "File size: $FILE_SIZE" + else + log "${RED}Error: Failed to download golinks export${NC}" + # Clean up empty file if it exists + [ -f "$LOCAL_FILE" ] && rm -f "$LOCAL_FILE" + exit 1 + fi + + # Copy to NAS if not skipped + if [ "$SKIP_NAS" = "false" ]; then + if [ ! -d "$NAS_DIR" ]; then + log "${YELLOW}NAS directory not found, creating: $NAS_DIR${NC}" + mkdir -p "$NAS_DIR" || log "${RED}Error: Failed to create NAS directory: $NAS_DIR${NC}" + fi + if [ -d "$NAS_DIR" ] && [ -w "$NAS_DIR" ]; then + log "${YELLOW}Copying backup to NAS: $NAS_DIR${NC}" + if cp "$LOCAL_FILE" "$NAS_DIR/"; then + log "${GREEN}Successfully copied to NAS${NC}" + else + log "${RED}Error: Failed to copy to NAS${NC}" + # Don't exit here, local backup is still good + fi + else + log "${YELLOW}Warning: NAS directory not accessible or writable: $NAS_DIR${NC}" + fi + else + log "${YELLOW}Skipping NAS copy as requested.${NC}" + fi + + # Cleanup old local backups (Keep 30 days) + log "Cleaning up local backups older than 30 days..." + find "$BACKUP_DIR" -type f -name "golinks_backup_*" -mtime +30 -delete + + log "${GREEN}Backup process finished.${NC}" +} + +# ========================================== +# 3. EXECUTION FLOW +# ========================================== + +check_dependencies + +# Parse Arguments +if [ $# -eq 0 ]; then + perform_backup "false" + exit 0 +fi + +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 ;; + *) echo "Unknown parameter: $1"; usage; exit 1 ;; + esac + shift +done