-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
194 lines (171 loc) · 4.96 KB
/
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
THIS=$(realpath -s "$0")
ON_THE_SERVER=false
if [[ $# -gt 0 ]]; then
if [[ "$1" == "-s" ]]; then
ON_THE_SERVER=true
shift
fi
fi
SERVER=boulangerie
POSTGRES_ROOT_PASSWORD=REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME
MYSQL_ROOT_PASSWORD=REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME
MARIADB_ROOT_PASSWORD=REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME-REPLACE-ME
REMOTE_BACKUP_DIR=boulangerie-backup
mariadb_backup () {
DB=$1
DB_PASSWORD=$MARIADB_ROOT_PASSWORD
OPTIONS=$2
echo "--- MariaDB $DB ---"
sudo podman exec -it mariadb-server mysqldump $OPTIONS -uroot -p$DB_PASSWORD $DB | gzip > $REMOTE_BACKUP_DIR/backup-$DB.sql.gz
echo "Created $REMOTE_BACKUP_DIR/backup-$DB.sql.gz"
}
mysql_backup () {
DB=$1
DB_PASSWORD=$MYSQL_ROOT_PASSWORD
OPTIONS=$2
echo "--- MySQL $DB ---"
sudo podman exec -it mysql-server mysqldump $OPTIONS -uroot -p$DB_PASSWORD $DB | gzip > $REMOTE_BACKUP_DIR/backup-$DB.sql.gz
echo "Created $REMOTE_BACKUP_DIR/backup-$DB.sql.gz"
}
postgres_backup () {
DB=$1
OPTIONS=$2
echo "--- PostgreSQL $DB ---"
sudo PGPASSWORD=$POSTGRES_ROOT_PASSWORD podman exec -it postgres-server pg_dump -w $OPTIONS -Upostgres $DB | gzip > $REMOTE_BACKUP_DIR/backup-$DB.sql.gz
echo "Created $REMOTE_BACKUP_DIR/backup-$DB.sql.gz"
}
files_backup () {
SHORTNAME=$1
DIR=$2
FILES=("${@:3}")
echo "--- tar $DIR ($FILES) ---"
sudo tar -czf $REMOTE_BACKUP_DIR/backup-$SHORTNAME.tar.gz -C $DIR "${FILES[@]}" ; sudo chown "$(id -un):" $REMOTE_BACKUP_DIR/backup-$SHORTNAME.tar.gz
echo "Created $REMOTE_BACKUP_DIR/backup-$SHORTNAME.tar.gz"
}
files_backup_stream () {
SHORTNAME=$1
DIR=$2
FILES=("${@:3}")
echo "--- streaming tar $DIR ($FILES) ---"
ssh boulangerie "sudo -S tar -czf - -C $DIR '${FILES[@]}'" | gzip > tmp/backup-$SHORTNAME.tar.gz
echo "Created tmp/backup-$SHORTNAME.tar.gz (locally)"
}
move_files () {
echo "--- Copying files ---"
TIMESTAMP=$(date +"%Y-%m-%d-%H%M%S")
if [[ -d tmp ]]; then
for file in tmp/*; do
basename=$(basename $file)
filename=$(basename $file | cut -d. -f1)
ext=$(basename $file | cut -d. -f2-)
mv "$file" "${filename}-${TIMESTAMP}.${ext}"
echo "Done: ${filename}-${TIMESTAMP}.${ext}"
done
rmdir tmp
else
echo "tmp is not a directory or does not exist"
exit 1
fi
}
notify () {
notify-send -i drive-harddisk "Backup performed"
}
if [[ "$ON_THE_SERVER" == "true" ]]; then
rm -rf $REMOTE_BACKUP_DIR
mkdir $REMOTE_BACKUP_DIR
while [[ $# -gt 0 ]]; do
case $1 in
bookstack_db)
mariadb_backup "bookstack_db" ""
shift
;;
tarallo_db)
mariadb_backup "tarallo_db" "--ignore-table=tarallo_db.ProductItemFeature --ignore-table=tarallo_db.ProductItemFeatureUnified"
shift
;;
nextcloud)
postgres_backup "nextcloud" "-C"
shift
;;
keycloak)
postgres_backup "keycloak" "-C"
shift
;;
weeebsite)
mysql_backup "weeebsite" ""
shift
;;
bookstack-dir)
files_backup "bookstack-dir" "/data/containers/volumes/websites/bookstack" "public/uploads" "storage/uploads" ".env"
shift
;;
weeebsite-dir)
files_backup "weeebsite-dir" "/data/containers/volumes/websites/wordpress" "wp-content/uploads" "wp-content/gallery" "wp-config.php"
shift
;;
weeehire)
files_backup "weeehire" "/data/containers/volumes/websites/weeehire" "database/weeehire.db"
shift
;;
containers)
files_backup "containers" "/data/containers/sources" "."
shift
;;
nginx)
files_backup "nginx" "/data/containers/volumes/nginx" "configuration"
shift
;;
# nextcloud-dir)
# files_backup "nextcloud-dir" "/data/containers/volumes/websites/nextcloud" "data" "config"
# shift
# ;;
*)
echo "Unknown backup $1"
shift
;;
esac
done
else
if [[ $# -lt 1 ]]; then
# https://stackoverflow.com/a/1970254
WHAT_TO_BACKUP=$(whiptail --title "Select backups" --checklist \
"Select backups" 0 0 0 \
"containers" "Container sources" OFF \
"nginx" "Nginx config" OFF \
"bookstack_db" "Bookstack DB" OFF \
"bookstack-dir" "Bookstack files" OFF \
"tarallo_db" "Tarallo DB" OFF \
"nextcloud" "Nextcloud DB" OFF \
"keycloak" "Keycloak DB" OFF \
"weeebsite" "Weeebsite DB" OFF \
"weeebsite-dir" "Weeebsite files" OFF \
"weeehire" "WEEEHire DB" OFF \
"nextcloud-dir" "Nextcloud files (streaming)" OFF \
3>&1 1>&2 2>&3)
fi
if [[ "x" == "x$WHAT_TO_BACKUP" ]]; then
echo "Nothing to do"
exit 1
fi
scp $THIS $SERVER:boulangerie-backup-script.sh
ssh -t $SERVER "bash -s <<EOF
#!/bin/bash
set -e
chmod ug+x boulangerie-backup-script.sh
chmod o-r boulangerie-backup-script.sh
./boulangerie-backup-script.sh -s $WHAT_TO_BACKUP
EOF"
echo "--- rsync ---"
rsync -raHAX --info=progress2 boulangerie:$REMOTE_BACKUP_DIR/ tmp/
while [[ $# -gt 0 ]]; do
case $1 in
nextcloud-dir)
files_backup_stream "nextcloud-dir" "/data/containers/volumes/websites/nextcloud"
shift
;;
esac
done
move_files
notify
fi