home · contact · privacy
Improve web server setup.
[config] / all_new_2018 / letsencrypt_remote.sh
1 #!/bin/sh
2 # Install or copy LetsEncrypt certificates on/from server.
3 #
4 # First argument: server
5 # Second argument: "get" or "put"
6 #
7 # "get" copies the server's /etc/letsencrypt to a local letsencrypt.tar.
8 #
9 # "set" copies a local letsencrypt.tar to the server's /etc/letsencrypt.
10 set -e
11
12 # Ensure we have a server name as argument.
13 if [ $# -lt 2 ]; then
14     echo "Need server and action as arguments."
15     false
16 fi
17 server="$1"
18 action="$2"
19
20 # So we only get asked once for decrypting our key.
21 eval $(ssh-agent)
22 ssh-add ~/.ssh/id_rsa
23
24 if [ "${action}" = "get" ]; then
25     # Get /etc/letsencrypt/ as tar file.
26     ssh -t plom@${server} 'su -c "cd /etc/ && tar cf letsencrypt.tar letsencrypt && chown plom:plom letsencrypt.tar && mv letsencrypt.tar /home/plom/"'
27     scp plom@${server}:~/letsencrypt.tar .
28 elif [ "${action}" = "put" ]; then
29     # Expand letsencrypt.tar to /etc/letsencrypt/ on server.
30     scp letsencrypt.tar plom@${server}:~/
31     ssh -t plom@${server} 'su -c "apt -y install certbot && rmdir /etc/letsencrypt && mv letsencrypt.tar /etc/ && cd /etc/ && tar xf letsencrypt.tar && rm letsencrypt.tar"'
32 else
33     echo "Action must be 'get', or 'put'."
34     false
35 fi