-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
executable file
·117 lines (108 loc) · 4.1 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
#!/bin/bash
set -e
# Define the status update URL
# shellcheck disable=SC2153
status_url=${STATUS_URL}
if [ -n "$status_url" ]; then
# shellcheck disable=SC2064
trap "curl -fs '$status_url?status=down&msg=FAILED'" ERR
fi
mkdir -p backup
# Check the backup source and run the appropriate backup client
#
# Multi: refers to backups that will either query for a list of databases or buckets
# Single: refers to backups that will only backup a single database or bucket
# Use file if you want to manually create your backup and just have it backuped. Mount it in the backup container and use the FILE option.
#
case $BACKUP_SOURCE in
POSTGRES_MULTI)
psql -tc "$PGDBSELECT" | while read -r database; do
echo "Backing up $database"
# database is not empty
if [ -n "$database" ]; then
# Use the pg_dump tool to create a backup of the database
pg_dump -Fd -v -d "$database" -j 4 -Z0 -f "/app/backup/$database"
fi
done
;;
MYSQL_MULTI)
# echo mysql result
mysql --host=$MYSQL_HOST --user=$MYSQL_USER --password="$MYSQL_PASSWORD" -s -N -e "$MYSQL_DBSELECT" | while read -r database; do
# Skip information_schema, performance_schema, and sys databases
if [ "$database" = "information_schema" ] || [ "$database" = "performance_schema" ] || [ "$database" = "sys" ]; then
continue
fi
echo "Backing up $database"
# database is not empty
if [ -n "$database" ]; then
# Use the mysqldump tool to create a backup of the database
mysqldump --host=$MYSQL_HOST --user=$MYSQL_USER --single-transaction --password="$MYSQL_PASSWORD" --quick --lock-tables=false "$database" > "/app/backup/$database.sql"
# Check that the file exists, otherwise throw error
if [ ! -f "/app/backup/$database.sql" ]; then
echo "Backup failed for $database"
exit 1
fi
fi
done
## if no database is found, exit with error
if [ ! "$(ls -A /app/backup)" ]; then
echo "No databases found"
exit 1
fi
;;
MYSQL_SINGLE)
echo "Backing up $MYSQL_DATABASE"
# Use the mysqldump tool to create a backup of the database
mysqldump --host=$MYSQL_HOST --user=$MYSQL_USER --single-transaction --password="$MYSQL_PASSWORD" --quick --lock-tables=false "$MYSQL_DATABASE" > "/app/backup/$MYSQL_DATABASE.sql"
# Check that the file exists, otherwise throw error
if [ ! -f "/app/backup/$MYSQL_DATABASE.sql" ]; then
echo "Backup failed for $MYSQL_DATABASE"
exit 1
fi
;;
MINIO_MULTI)
# Use the minio client (mc) to create a backup of the bucket
# shellcheck disable=SC2086
mc alias set s3backup $S3_ENDPOINT "$S3_ACCESS" $S3_SECRET --api S3v4
# mirror each s3 bucket to a local directory
mc ls s3backup | awk '{print $5}' | while read bucket; do
mc mirror --overwrite s3backup/$bucket /app/backup/$bucket
done
;;
MONGO_SINGLE)
# Use the mongodump tool to create a backup of the database
mongodump --uri "$MONGO_URL" --out=/app/backup
;;
FILE)
# Use the mongodump tool to create a backup of the database
echo "Performing file Backup"
;;
*)
echo "Invalid backup source: $BACKUP_SOURCE"
exit 1
;;
esac
if [ "$FOLDERS_SEPERATELY" = "true" ]; then
find /app/backup -mindepth 1 -maxdepth 1 -type d | while read folder; do
folder_name=$(basename "$folder")
proxmox-backup-client backup "$BACKUP_ID-$folder_name.pxar:/app/backup/$folder_name" \
--backup-id "$BACKUP_ID-$folder_name" \
--ns "$PBC_NAMESPACE" \
--change-detection-mode="${CHANGE_DETECTION_MODE:-legacy}" \
--skip-lost-and-found
done
else
proxmox-backup-client backup "$BACKUP_ID.pxar:/app/backup" \
--backup-id "$BACKUP_ID" \
--ns "$PBC_NAMESPACE" \
--change-detection-mode="${CHANGE_DETECTION_MODE:-legacy}" \
--skip-lost-and-found
fi
# Send a status update
if [ -n "$status_url" ]; then
curl -fs "$status_url?status=up&msg=OK"
fi
# Clean up the backup file if REMOVE_AFTER_BACKUP is set to TRUE
if [ "$REMOVE_AFTER_BACKUP" = "TRUE" ]; then
rm -rf /app/backup/*
fi