-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-backup
executable file
·96 lines (75 loc) · 2.85 KB
/
create-backup
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
#!/bin/sh
set -e
LOGFILE="/var/log/borg/borgbackup.log"
function log () {
echo "$(date "+%m-%d-%Y %T"): $1" 2>1 | tee -a $LOGFILE
}
function send_error_email () {
echo "Subject: Failed creating foodcoops backup on: $(date)" > /tmp/error.txt
echo "From: [email protected]" >> /tmp/error.txt
echo "" >> /tmp/error.txt
echo "Failed creating backup on: $(date)" >> /tmp/error.txt
sendmail -v -S mail.foodcoops.at $RECIPIENT_ADDRESS_ERROR -f [email protected] < /tmp/error.txt
}
trap send_error_email ERR
destination=/backup_storage
remote_destination=/backup_storage_remote
export PGPASSWORD="$(cat /run/secrets/postgres_password)"
export MYSQLDUMP_PASSWORD="$(cat /run/secrets/mariadb_root_password)"
log "Starting backup --------------------"
log "Starting to dump postgresql databases"
mkdir -p /backup/postgresql_dbs
pg_dumpall --globals-only -Oxf /backup/postgresql_dbs/globals.sql
databases=$(psql -U postgres -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d')
for db_name in $databases; do
if [ "$db_name" != "template0" ] && [ "$db_name" != "template1" ] && [ "$db_name" != "template_postgis" ]; then
log "Starting DUMP for db ${db_name}"
pg_dump -U postgres -Fc -Ox ${db_name} > /backup/postgresql_dbs/pg_dump_${db_name}.pgc
dump_single_exit=$?
log "DUMP db ${db_name} finished with ${dump_single_exit}"
fi
done
log "Starting to dump mariadb databases"
mkdir -p /backup/mysql_dbs
for db_name in $(mysql -h"$MYSQLDUMP_HOST" -u"$MYSQLDUMP_USER" -p"$MYSQLDUMP_PASSWORD" -e "show databases;" | tail -n +2);
do
log "Starting DUMP for db ${db_name}"
mysqldump \
-h"$MYSQLDUMP_HOST" \
-u"$MYSQLDUMP_USER" \
-p"$MYSQLDUMP_PASSWORD" \
--single-transaction \
--order-by-primary \
${db_name} > /backup/mysql_dbs/mysql_${db_name}_dump.sql
dump_single_exit=$?
log "DUMP db ${db_name} finished with ${dump_single_exit}"
done
log "Creating redis dump"
redis-cli -h redis save
mkdir -p /backup/redis
cp /redis_data/dump.rdb /backup/redis/
log "Done creating redis dump"
log "Create borg backup"
export BORG_PASSPHRASE="$(cat /run/secrets/borg_passphrase)"
export BORG_REPO=/backup_storage/borgrepo
borg create --compression lz4 ::'{hostname}-{now}' /backup \
--exclude /backup/nextcloud_data/nextcloud.log
log "Prune old borg backup"
borg prune \
--glob-archives '{hostname}-*' \
--keep-daily 6 \
--keep-weekly 3 \
--keep-monthly 5
borg compact
log "Create remote borg backup"
export BORG_REPO=/backup_storage_remote/borgrepo
borg create --compression lz4 ::'{hostname}-{now}' /backup \
--exclude /backup/nextcloud_data/nextcloud.log
log "Prune remote old borg backup"
borg prune \
--glob-archives '{hostname}-*' \
--keep-daily 6 \
--keep-weekly 3 \
--keep-monthly 5 \
borg compact
log "Backup finished ----------------------"