Skip to content

Instantly share code, notes, and snippets.

@aamnah
Last active April 19, 2018 14:48
Show Gist options
  • Select an option

  • Save aamnah/fd3db0ede0fb402f7df0bf420ac68826 to your computer and use it in GitHub Desktop.

Select an option

Save aamnah/fd3db0ede0fb402f7df0bf420ac68826 to your computer and use it in GitHub Desktop.
Import Apache websites along with their conf files and Let's Encrypt SSL certs from another server
#!/bin/bash
# Purpose: Transfer /var/www files from another server
# This PULLS the changes, i.e. it'll fetch stuff from another server
# and write on the one you're running the command from
# Pull: sync remote with local
# Include a config.sh file that has variables
# scriptPath="${BASH_SOURCE%/*}"
# source "${scriptPath}/config.sh"
REMOTE_SSH_HOST='123.123.123.123' # Server IP or FQDN of the remote server you're importing from
REMOTE_SSH_USER='root'
REMOTE_SSH_KEY="~/.ssh/id_rsa"
REMOTE_SSH_PORT='22'
WWW_DIR='/var/www' # /var/www is the default sites directory for your websites
REMOTE_WWW_DIR='/var/www'
SITES_AVAILABLE_DIR='/etc/apache2/sites-available' # /etc/apache2/sites-available is where vhost.conf files for all domains are by default
REMOTE_SITES_AVAILABLE_DIR='/etc/apache2/sites-available'
REMOTE_SITES_ENABLED_DIR='/etc/apache2/sites-enabled'
SITES_ENABLED_DIR='/etc/apache2/sites-enabled' # /etc/apache2/sites-enabled is where vhost.conf files for enabled domains is (symlinks to files in sites-available)
import_sites() {
# apt update && apt install -y rsync
#Access via remote shell:
# Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
# Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
# -q: quiet
# -a: archive (equivalent of -rlptgoD)
# -z: compress
# -e: the shell you want to use
# /var/www
echo -e "\n\n Transferring ${WWW_DIR}"
rsync -qaze "ssh -p ${REMOTE_SSH_PORT} -i ${REMOTE_SSH_KEY}" ${REMOTE_SSH_USER}@${REMOTE_SSH_HOST}:${REMOTE_WWW_DIR}/* ${WWW_DIR}/
# /etc/apache2/sites-available
echo -e "\n\n Transferring ${SITES_AVAILABLE_DIR}"
rsync -qaze "ssh -p ${REMOTE_SSH_PORT} -i ${REMOTE_SSH_KEY}" ${REMOTE_SSH_USER}@${REMOTE_SSH_HOST}:${REMOTE_SITES_AVAILABLE_DIR}/* ${SITES_AVAILABLE_DIR}/
# /etc/apache2/sites-enabled
echo -e "\n\n Transferring ${SITES_ENABLED_DIR}"
rsync -qaze "ssh -p ${REMOTE_SSH_PORT} -i ${REMOTE_SSH_KEY}" ${REMOTE_SSH_USER}@${REMOTE_SSH_HOST}:${REMOTE_SITES_ENABLED_DIR}/* ${SITES_ENABLED_DIR}/
# SSL Cert files
echo -e "\n\n Transferring Let's Encrypt SSL Certificates"
rsync -qaze "ssh -p ${REMOTE_SSH_PORT} -i ${REMOTE_SSH_KEY}" ${REMOTE_SSH_USER}@${REMOTE_SSH_HOST}:/etc/letsencrypt/archive :/etc/letsencrypt/live :/etc/letsencrypt/renewal /etc/letsencrypt/
# This works but asks for SSH key passphrase every time the rsync command runs
# LINKS
# - https://explainshell.com/explain?cmd=rsync+-avzhe+ssh+tecmint%40webserver.example.com%3A%2Fvar%2Fwww%2F+%2Fvar%2Fwww
# - https://community.letsencrypt.org/t/moving-and-merging-certs-from-server-a-to-b/19015/3
# - https://unix.stackexchange.com/questions/368210/how-to-rsync-multiple-source-folders
}
import_sites
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment