-
Notifications
You must be signed in to change notification settings - Fork 1
/
uberspace-backup.sh
executable file
·122 lines (103 loc) · 3.74 KB
/
uberspace-backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env bash
# Copyright (c) 2013 Jakob Krigovsky
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Default settings
BACKUP_DIR=~/uberspace-backups/
SSH_CONFIG=~/.ssh/config
ARCHIVE=false
DATE=`date +%d-%m-%Y`
read_hosts() {
while read line; do
if [[ $line == Host\ * ]]; then
CURRENT_HOST="${line:5}"
fi
if [[ $line == HostName\ *.uberspace.de ]]; then
UNIQUE=true
for HOSTNAME in $HOSTNAMES; do
if [[ $HOSTNAME == ${line:9} ]]; then
UNIQUE=false
break
fi
done
# If there are multiple hosts with the same hostname we only backup the
# first one.
if $UNIQUE; then
HOSTNAMES=("${HOSTNAMES[@]} ${line:9}")
HOSTS=("${HOSTS[@]} $CURRENT_HOST")
fi
fi
done < "$SSH_CONFIG"
}
run_backup() {
for HOST in $HOSTS; do
UBERSPACE=$(ssh $HOST whoami)
# Remove trailing slash, if any.
BACKUP_DIR=${BACKUP_DIR%/}
echo "Uberspace: $UBERSPACE\n"
if $ARCHIVE; then
# Create a tar.bz2-archive
mkdir -p "${BACKUP_DIR}/archives/"
echo "• Backing up /home/$UBERSPACE"
ssh $HOST "tar -cjf - /home/$UBERSPACE" >${BACKUP_DIR}/archives/uberspace-$UBERSPACE-home-$DATE.tar.bz2 && echo "✔ Backed up /home/$UBERSPACE → uberspace-$UBERSPACE-home-$DATE.tar.bz2"
else
# Sync with rsync
echo "• Syncing /home/$UBERSPACE"
mkdir -p "${BACKUP_DIR}/home/$UBERSPACE/"
rsync -aPz --delete $HOST:/home/$UBERSPACE/ "${BACKUP_DIR}/home/$UBERSPACE/" && echo "✔ Synced /home/$UBERSPACE"
fi
if $ARCHIVE; then
# Create a tar.bz2-archive
mkdir -p "${BACKUP_DIR}/archives/"
echo "• Backing up /var/www/virtual/$UBERSPACE"
ssh $HOST "tar -cjf - /var/www/virtual/$UBERSPACE" >${BACKUP_DIR}/archives/uberspace-$UBERSPACE-var-$DATE.tar.bz2 && echo "✔ Backed up /var/www/virtual/$UBERSPACE → uberspace-$UBERSPACE-var-$DATE.tar.bz2"
else
# Sync with rsync
echo "• Syncing /var/www/virtual/$UBERSPACE"
mkdir -p "${BACKUP_DIR}/var/www/virtual/$UBERSPACE/"
rsync -aPz --delete $HOST:/var/www/virtual/$UBERSPACE/ "${BACKUP_DIR}/var/www/virtual/$UBERSPACE/" && echo "✔ Synced /var/www/virtual/$UBERSPACE"
fi
echo "• Dumping MySQL databases"
mkdir -p "${BACKUP_DIR}/mysqlbackup/$UBERSPACE/"
ssh $HOST "mysqldump --compact --comments --dump-date --single-transaction --quick --all-databases | xz" > "${BACKUP_DIR}/mysqlbackup/$UBERSPACE/all-databases.sql.xz" && echo "✔ Dumped MySQL databases"
echo
done
}
on_sigint() {
echo "Received SIGINT."
exit 1
}
while getopts ab:h:s: option; do
case "$option" in
a)
ARCHIVE=true ;;
b)
BACKUP_DIR=$OPTARG ;;
h)
HOSTS=("${HOSTS[@]} $OPTARG") ;;
s)
SSH_CONFIG=$OPTARG ;;
esac
done
trap on_sigint SIGINT
# Only read hosts from ssh’s config if none were specified using -h
if [ -z "$HOSTS" ]; then
read_hosts
fi
run_backup