@@ -11,6 +11,8 @@ LOG_FILE="/var/log/unifi-on-boot.log"
1111SERVICE_NAME=" unifi-on-boot"
1212SHADOW_CONF=" /data/unifi-on-boot/shadow.conf"
1313SKIP_SHADOW=false
14+ SSH_OPTS=" -o ConnectTimeout=5 -o StrictHostKeyChecking=no -o BatchMode=yes"
15+ SYNC_TIMEOUT=120 # hard deadline for each remote operation (seconds)
1416
1517# Parse command line arguments
1618while [[ $# -gt 0 ]]; do
@@ -61,6 +63,12 @@ sync_shadow_gateway() {
6163 return 0
6264 fi
6365
66+ # Verify SSH connectivity before committing to a full sync
67+ if ! timeout 10 ssh $SSH_OPTS " ${shadow_user} @${shadow_ip} " true > /dev/null 2>&1 ; then
68+ log " WARNING: Shadow gateway at ${shadow_ip} is reachable but SSH failed (skipping sync)"
69+ return 0
70+ fi
71+
6472 log " Shadow gateway detected at ${shadow_ip} , starting sync..."
6573
6674 # Ensure rsync is installed locally
@@ -73,9 +81,9 @@ sync_shadow_gateway() {
7381 fi
7482
7583 # Ensure rsync is installed on shadow
76- if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no " ${shadow_user} @${shadow_ip} " " command -v rsync" > /dev/null 2>&1 ; then
84+ if ! timeout " $SYNC_TIMEOUT " ssh $SSH_OPTS " ${shadow_user} @${shadow_ip} " " command -v rsync" > /dev/null 2>&1 ; then
7785 log " Installing rsync on shadow gateway..."
78- if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no " ${shadow_user} @${shadow_ip} " \
86+ if ! timeout " $SYNC_TIMEOUT " ssh $SSH_OPTS " ${shadow_user} @${shadow_ip} " \
7987 " DEBIAN_FRONTEND=noninteractive apt-get update >/dev/null 2>&1 && apt-get install -y rsync >/dev/null 2>&1" ; then
8088 log " ERROR: Failed to install rsync on shadow gateway, cannot sync"
8189 return 1
@@ -84,7 +92,7 @@ sync_shadow_gateway() {
8492
8593 # Sync /data/on_boot.d/ to shadow (--delete ensures exact mirror)
8694 log " Syncing /data/on_boot.d/ to shadow gateway..."
87- if rsync -avz --delete -e " ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no " \
95+ if timeout " $SYNC_TIMEOUT " rsync -avz --delete -e " ssh $SSH_OPTS " \
8896 " ${BOOT_DIR} /" " ${shadow_user} @${shadow_ip} :${BOOT_DIR} /" 2>&1 | tee -a " $LOG_FILE " ; then
8997 log " Successfully synced /data/on_boot.d/ to shadow gateway"
9098 else
@@ -93,15 +101,15 @@ sync_shadow_gateway() {
93101 fi
94102
95103 # Ensure unifi-on-boot is installed on shadow
96- if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no " ${shadow_user} @${shadow_ip} " \
104+ if ! timeout " $SYNC_TIMEOUT " ssh $SSH_OPTS " ${shadow_user} @${shadow_ip} " \
97105 " dpkg -l unifi-on-boot 2>/dev/null | grep -q '^ii'" 2> /dev/null; then
98106 log " Installing unifi-on-boot on shadow gateway..."
99107
100108 local deb_path=" /data/unifi-on-boot/unifi-on-boot.deb"
101109 if [ -f " $deb_path " ]; then
102- if scp -o ConnectTimeout=5 -o StrictHostKeyChecking=no \
110+ if timeout " $SYNC_TIMEOUT " scp $SSH_OPTS \
103111 " $deb_path " " ${shadow_user} @${shadow_ip} :/tmp/unifi-on-boot.deb" 2> /dev/null && \
104- ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no " ${shadow_user} @${shadow_ip} " \
112+ timeout " $SYNC_TIMEOUT " ssh $SSH_OPTS " ${shadow_user} @${shadow_ip} " \
105113 " dpkg -i /tmp/unifi-on-boot.deb && rm -f /tmp/unifi-on-boot.deb" 2>&1 | tee -a " $LOG_FILE " ; then
106114 log " Successfully installed unifi-on-boot on shadow gateway"
107115 else
@@ -116,7 +124,7 @@ sync_shadow_gateway() {
116124 # Run unifi-on-boot on shadow with --skip-shadow to prevent recursion
117125 # Note: we invoke the script directly (not via systemctl) so we can pass --skip-shadow
118126 log " Triggering unifi-on-boot on shadow gateway..."
119- if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no " ${shadow_user} @${shadow_ip} " \
127+ if timeout " $SYNC_TIMEOUT " ssh $SSH_OPTS " ${shadow_user} @${shadow_ip} " \
120128 " /usr/sbin/unifi-on-boot --skip-shadow" 2>&1 | tee -a " $LOG_FILE " ; then
121129 log " Shadow gateway sync completed successfully"
122130 else
@@ -183,7 +191,8 @@ if [ "$fail_count" -gt 0 ]; then
183191 log " WARNING: ${fail_count} script(s) failed. Check log for details."
184192fi
185193
186- # Sync to shadow gateway after all scripts have run
187- sync_shadow_gateway || log " WARNING: Shadow gateway sync encountered errors"
194+ # Sync to shadow gateway in the background — not boot-critical
195+ (sync_shadow_gateway || log " WARNING: Shadow gateway sync encountered errors" ) &
196+ disown
188197
189198exit 0
0 commit comments