mirror-sync.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/bin/bash
  2. # apt-mirror2 sync script
  3. # This script handles the synchronization of apt repositories using the Python version (apt-mirror2)
  4. MIRROR_CONFIG="/etc/apt/mirror.list"
  5. MIRROR_LOG="/var/log/apt-mirror/apt-mirror.log"
  6. SYNC_FREQUENCY="${SYNC_FREQUENCY:-3600}" # Default: 1 hour
  7. LOCK_FILE="/var/run/apt-mirror.lock"
  8. # Function to log messages
  9. log() {
  10. # Ensure log directory exists
  11. mkdir -p "$(dirname "$MIRROR_LOG")"
  12. echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$MIRROR_LOG"
  13. }
  14. # Function to check if sync is already running
  15. check_lock() {
  16. if [ -f "$LOCK_FILE" ]; then
  17. local pid=$(cat "$LOCK_FILE" 2>/dev/null)
  18. if kill -0 "$pid" 2>/dev/null; then
  19. # Additional check: verify the process is actually apt-mirror
  20. if ps -p "$pid" -o comm= 2>/dev/null | grep -q "apt-mirror\|python.*apt-mirror"; then
  21. log "Sync already running (PID: $pid)"
  22. return 1
  23. else
  24. log "Removing stale lock file (PID $pid is not apt-mirror process)"
  25. rm -f "$LOCK_FILE"
  26. fi
  27. else
  28. log "Removing stale lock file"
  29. rm -f "$LOCK_FILE"
  30. fi
  31. fi
  32. return 0
  33. }
  34. # Function to create lock file
  35. create_lock() {
  36. echo $$ > "$LOCK_FILE"
  37. }
  38. # Function to remove lock file
  39. remove_lock() {
  40. rm -f "$LOCK_FILE"
  41. }
  42. # Function to perform sync
  43. do_sync() {
  44. log "Starting apt-mirror2 sync..."
  45. if [ ! -f "$MIRROR_CONFIG" ]; then
  46. log "ERROR: Mirror configuration not found at $MIRROR_CONFIG"
  47. return 1
  48. fi
  49. # Create lock file
  50. create_lock
  51. # Set environment variables for better performance
  52. export PYTHONUNBUFFERED=1
  53. export PYTHONIOENCODING=utf-8
  54. # Run apt-mirror2 using Python version with timeout
  55. if timeout 36000 apt-mirror "$MIRROR_CONFIG" 2>&1 | tee -a "$MIRROR_LOG"; then
  56. log "Sync completed successfully"
  57. # Update symlink to ensure web server sees latest data
  58. if [ -d "/var/spool/apt-mirror/mirror" ]; then
  59. ln -sf /var/spool/apt-mirror/mirror /var/www/mirror.intra/mirror
  60. log "Updated web symlink"
  61. fi
  62. # Update last sync timestamp
  63. date > /var/spool/apt-mirror/last-sync.txt
  64. # Log sync completion statistics
  65. log "Sync completed at $(date)"
  66. if [ -d "/var/spool/apt-mirror/mirror" ]; then
  67. local total_size=$(du -sh /var/spool/apt-mirror/mirror 2>/dev/null | cut -f1)
  68. log "Total mirror size: $total_size"
  69. fi
  70. else
  71. local exit_code=$?
  72. if [ $exit_code -eq 124 ]; then
  73. log "ERROR: Sync timed out after 2 hours"
  74. else
  75. log "ERROR: Sync failed with exit code $exit_code"
  76. fi
  77. remove_lock
  78. return 1
  79. fi
  80. remove_lock
  81. return 0
  82. }
  83. # Function to run continuous sync
  84. run_continuous() {
  85. log "Starting continuous sync with frequency: ${SYNC_FREQUENCY}s"
  86. while true; do
  87. if check_lock; then
  88. do_sync
  89. fi
  90. log "Waiting ${SYNC_FREQUENCY} seconds until next sync..."
  91. sleep "$SYNC_FREQUENCY"
  92. done
  93. }
  94. # Function to run single sync
  95. run_once() {
  96. if check_lock; then
  97. do_sync
  98. else
  99. exit 1
  100. fi
  101. }
  102. # Main execution
  103. case "${1:-continuous}" in
  104. "once")
  105. run_once
  106. ;;
  107. "continuous")
  108. run_continuous
  109. ;;
  110. "status")
  111. if [ -f "$LOCK_FILE" ]; then
  112. local pid=$(cat "$LOCK_FILE")
  113. if kill -0 "$pid" 2>/dev/null; then
  114. echo "Sync running (PID: $pid)"
  115. else
  116. echo "Stale lock file found"
  117. fi
  118. else
  119. echo "No sync running"
  120. fi
  121. ;;
  122. *)
  123. echo "Usage: $0 {once|continuous|status}"
  124. echo " once - Run sync once and exit"
  125. echo " continuous - Run sync continuously (default)"
  126. echo " status - Check sync status"
  127. exit 1
  128. ;;
  129. esac