initial commit

This commit is contained in:
Christian Ulrich 2019-03-09 16:46:25 +01:00
commit a638ff8392
1 changed files with 80 additions and 0 deletions

80
backup/backup.sh Normal file
View File

@ -0,0 +1,80 @@
#!/usr/bin/env bash
# backup.sh
#
# Copyright (C) 2019 Christian Ulrich
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
SERVER_MAC_ADDRESS=40:61:86:32:f8:c3
SERVER_STAGE1_IP=10.0.0.19
SERVER_STAGE1_PORT=2222
SERVER_STAGE1_USERNAME=root
SERVER_IP="$SERVER_STAGE1_IP"
SERVER_PORT=22
SERVER_USERNAME=luno
SSH_IDENTITY_FILE=~/.ssh/id_rsa
DIRECTORIES=("/data" "/etc/nixos" "/var/lib/matrix-synapse" "/var/lib/postgresql" "/var/lib/prosody")
function cleanup {
ssh-add -d $SSH_IDENTITY_FILE
kill $SSH_AGENT_PID
}
function wakeup_server {
wakelan $SERVER_MAC_ADDRESS
}
function wait_for_server {
local ip="$1"
local port="$2"
echo "Waiting for $1:$2 ..."
while true; do
nc -z $ip $port 2>/dev/null && break
done
}
function init_server {
eval $(ssh-agent) 1>/dev/null
ssh-add $SSH_IDENTITY_FILE
wait_for_server $SERVER_STAGE1_IP $SERVER_STAGE1_PORT || return 1
ssh -l $SERVER_STAGE1_USERNAME -p $SERVER_STAGE1_PORT $SERVER_STAGE1_IP -t 'cryptsetup-askpass'
wait_for_server $SERVER_IP $SERVER_PORT || return 2
}
function shutdown_server {
echo "Trying to shutdown server."
ssh -l $SERVER_USERNAME -p $SERVER_PORT $SERVER_IP 'sudo shutdown -h now'
}
function do_backup {
echo "Starting backup..."
rsync -avR --no-inc-recursive --info=progress2 --delete -e "ssh -p $SERVER_PORT" "${DIRECTORIES[@]}" $SERVER_USERNAME@$SERVER_IP:
if [ $? -eq 0 ]; then
echo "Backup successful!"
fi
}
function main {
wakeup_server
init_server
if [ $? -eq 0 ]; then
do_backup
fi
shutdown_server
cleanup
}
main