Skip to content

Instantly share code, notes, and snippets.

@jsgarvin
Created November 28, 2013 05:50
Show Gist options
  • Save jsgarvin/7687773 to your computer and use it in GitHub Desktop.
Save jsgarvin/7687773 to your computer and use it in GitHub Desktop.
Automatically establish ssh tunnel to remote postgresql db server on boot, and auto re-establish if connection is lost (eg reboot of remote machine or network failure). Requires autossh.
# Modified version of technique borrowed from
# http://theantway.com/2012/08/how-to-create-ssh-tunnel-at-boot-time-under-ubuntu/
# Place in /etc/init/
# autossh
description "autossh daemon"
start on net-device-up IFACE=eth1
stop on runlevel [01S6]
respawn
respawn limit 5 60 # respawn max 5 times in 60 seconds
script
export AUTOSSH_PIDFILE=/var/run/autossh.pid
export AUTOSSH_POLL=60
export AUTOSSH_FIRST_POLL=30
export AUTOSSH_GATETIME=0
export AUTOSSH_DEBUG=1
autossh -M 0 -2 -N -L 5432:localhost:5432 USERNAME@REMOTE.int -p REMOTE_PORT -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -o BatchMode=yes -o StrictHostKeyChecking=no -i /path/to/.ssh/id_rsa
end script
@ROBERT-MCDOWELL
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment