138 lines
3.9 KiB
Bash
Executable File
138 lines
3.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Script to synchronize the current directory to the same path on another Mac
|
|
# and optionally to an external NVME drive
|
|
# Author: Cline
|
|
# Date: $(date +"%Y-%m-%d")
|
|
|
|
# Define IP addresses
|
|
IP_MAC1="192.168.7.1"
|
|
IP_MAC2="192.168.7.4"
|
|
NVME_PATH="/Volumes/d4to"
|
|
|
|
# Detect local IP address on Thunderbolt interface
|
|
get_local_ip() {
|
|
local thunderbolt_ip=$(ifconfig | grep -A 4 "bridge0\|en5\|en6" | grep "inet " | awk '{print $2}' | grep "192.168.7.")
|
|
echo "$thunderbolt_ip"
|
|
}
|
|
|
|
# Determine destination IP based on local IP
|
|
get_destination_ip() {
|
|
local local_ip=$(get_local_ip)
|
|
|
|
if [ "$local_ip" = "$IP_MAC1" ]; then
|
|
echo "$IP_MAC2"
|
|
elif [ "$local_ip" = "$IP_MAC2" ]; then
|
|
echo "$IP_MAC1"
|
|
else
|
|
echo ""
|
|
fi
|
|
}
|
|
|
|
# Source and destination paths
|
|
SOURCE_DIR=$(pwd)
|
|
DEST_IP=$(get_destination_ip)
|
|
|
|
if [ -z "$DEST_IP" ]; then
|
|
echo "Error: Could not determine destination IP. Make sure you're on a Mac with IP $IP_MAC1 or $IP_MAC2"
|
|
exit 1
|
|
fi
|
|
|
|
DESTINATION="pierre@$DEST_IP:$SOURCE_DIR"
|
|
|
|
# Log file
|
|
LOG_FILE="$SOURCE_DIR/sync_log_$(date +"%Y%m%d_%H%M%S").log"
|
|
|
|
# Colors for output
|
|
GREEN='\033[0;32m'
|
|
RED='\033[0;31m'
|
|
YELLOW='\033[0;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Function to log messages
|
|
log_message() {
|
|
local message="$1"
|
|
local type="$2"
|
|
local color="$NC"
|
|
|
|
case "$type" in
|
|
"INFO") color="$GREEN" ;;
|
|
"ERROR") color="$RED" ;;
|
|
"WARNING") color="$YELLOW" ;;
|
|
esac
|
|
|
|
echo -e "${color}[$(date +"%Y-%m-%d %H:%M:%S")] [$type] $message${NC}"
|
|
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [$type] $message" >> "$LOG_FILE"
|
|
}
|
|
|
|
# Check if rsync is installed
|
|
if ! command -v rsync &> /dev/null; then
|
|
log_message "rsync is not installed. Please install it using 'brew install rsync'" "ERROR"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if source directory exists
|
|
if [ ! -d "$SOURCE_DIR" ]; then
|
|
log_message "Source directory $SOURCE_DIR does not exist" "ERROR"
|
|
exit 1
|
|
fi
|
|
|
|
# Create log file
|
|
touch "$LOG_FILE"
|
|
log_message "Starting synchronization from $SOURCE_DIR to $DESTINATION" "INFO"
|
|
|
|
# Ping the destination to check if it's reachable
|
|
if ! ping -c 1 "$DEST_IP" &> /dev/null; then
|
|
log_message "Destination $DEST_IP is not reachable. Check your network connection." "ERROR"
|
|
exit 1
|
|
fi
|
|
|
|
# Run rsync with archive mode, compression, and verbose output
|
|
log_message "Running rsync..." "INFO"
|
|
rsync -avz --exclude=".git" \
|
|
"$SOURCE_DIR/" "$DESTINATION/" 2>&1 | tee -a "$LOG_FILE"
|
|
|
|
# Check rsync exit status
|
|
if [ ${PIPESTATUS[0]} -eq 0 ]; then
|
|
log_message "Synchronization completed successfully" "INFO"
|
|
else
|
|
log_message "Synchronization failed with exit code ${PIPESTATUS[0]}" "ERROR"
|
|
exit 1
|
|
fi
|
|
|
|
# Get local IP
|
|
LOCAL_IP=$(get_local_ip)
|
|
|
|
# If we're on MAC2 and NVME is mounted, also backup to NVME
|
|
if [ "$LOCAL_IP" = "$IP_MAC2" ] && [ -d "$NVME_PATH" ]; then
|
|
# Get parent directory name
|
|
PARENT_DIR=$(basename "$(dirname "$SOURCE_DIR")")
|
|
PROJECT_DIR=$(basename "$SOURCE_DIR")
|
|
BACKUP_DATE=$(date +"%Y-%m-%d")
|
|
NVME_BACKUP_DIR="$NVME_PATH/dev/$PARENT_DIR-$PROJECT_DIR/Y-w-$BACKUP_DATE"
|
|
|
|
log_message "Also backing up to NVME at $NVME_BACKUP_DIR" "INFO"
|
|
|
|
# Create backup directory if it doesn't exist
|
|
mkdir -p "$NVME_BACKUP_DIR"
|
|
|
|
# Backup to NVME
|
|
rsync -avz --exclude=".git" "$SOURCE_DIR/" "$NVME_BACKUP_DIR/" 2>&1 | tee -a "$LOG_FILE"
|
|
|
|
# Check rsync exit status
|
|
if [ ${PIPESTATUS[0]} -eq 0 ]; then
|
|
log_message "NVME backup completed successfully" "INFO"
|
|
else
|
|
log_message "NVME backup failed with exit code ${PIPESTATUS[0]}" "WARNING"
|
|
fi
|
|
|
|
# Purge backups older than 3 weeks
|
|
log_message "Purging backups older than 3 weeks" "INFO"
|
|
find "$NVME_PATH/dev/$PARENT_DIR-$PROJECT_DIR" -type d -name "Y-w-*" -mtime +21 -exec rm -rf {} \; 2>/dev/null || true
|
|
fi
|
|
|
|
log_message "Synchronization process finished" "INFO"
|
|
echo -e "${GREEN}Synchronization log saved to: $LOG_FILE${NC}"
|
|
|
|
exit 0
|