-
Notifications
You must be signed in to change notification settings - Fork 5
/
entrypoint.sh
86 lines (76 loc) · 3.31 KB
/
entrypoint.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
#!/usr/bin/env bash
if [ "$1" == "bash" ] || [ "$1" == "sh" ]; then
exec "${@}"
fi
if [ -z "$BORG_PASSPHRASE" ]; then
echo "[ERR] BORG_PASSPHRASE env variable not set. Exiting"
exit 1
fi
if [ -z "$FOLDERS_TO_BACKUP_PATH" ]; then
echo "[ERR] FOLDERS_TO_BACKUP_PATH env variable not set. Exiting"
exit 1
fi
if [ -z "$BACKUP_PATH" ]; then
echo "[ERR] BACKUP_PATH env variable not set. Exiting"
exit 1
fi
if [ -z "$MYSQL_HOST" ] || [ -z "$MYSQL_USER" ] || [ -z "$MYSQL_PASSWORD" ]; then
echo "[INFO] MYSQL not fully set, MYSQL Backup disable"
else
echo "[INFO] MYSQL configurated"
echo "[INFO] Checking MYSQL Databases"
res=$(MYSQL_PWD="$MYSQL_PASSWORD" mysql -u "$MYSQL_USER" -h "$MYSQL_HOST" -e "SHOW DATABASES;")
retVal=$?
if [ $retVal -ne 0 ]; then
echo "[ERR] An issue appear during the MYSQL test connection; please check the MYSQL_USER/MYSQL_PASSWORD/MYSQL_HOST environment variables."
exit 1
fi
echo "[INFO] The following tables will be backup"
echo "$res"
echo "[INFO] MYSQL backup enabled"
fi
if [ -z "$POSTGRES_HOST" ] || [ -z "$POSTGRES_USER" ] || [ -z "$POSTGRES_PASSWORD" ]; then
echo "[INFO] POSTGRES not fully set, POSTGRES Backup disable"
else
POSTGRES_CURRENT_VERSION=$(pg_dumpall --version | cut -f 3 -d ' ' | cut -f 1 -d '.')
echo "[INFO] POSTGRES configurated"
if [ -n "$POSTGRES_VERSION" ] && [ "$POSTGRES_VERSION" -ne "$POSTGRES_CURRENT_VERSION" ]; then
echo "[INFO] Removing the default postgres client version $POSTGRES_CURRENT_VERSION provided by Debian $(lsb_release -cs)"
apt-get -qq update && apt-get -qq remove -y postgresql-client*
echo "[INFO] Installing the requested postgres client version $POSTGRES_VERSION"
apt-get -qq install -y postgresql-client-${POSTGRES_VERSION}
retVal=$?
if [ $retVal -ne 0 ]; then
echo "[ERR] An issue appear during the postgresql-client-${POSTGRES_VERSION} install"
echo "[ERR] maybe this version isn't available for $(lsb_release -cs)"
echo "[ERR] some 32bits CPU aren't supported by postgres repository (like armhf/armv7), so only the Debian stable version is available"
echo "[ERR] Exiting !"
exit 1
fi
fi
res=$(PGPASSWORD="$POSTGRES_PASSWORD" psql -U "$POSTGRES_USER" -h "$POSTGRES_HOST" -l)
retVal=$?
if [ $retVal -ne 0 ]; then
echo "[ERR] An issue appear during the POSTGRES test connection; please check the POSTGRES_USER/POSTGRES_PASSWORD/POSTGRES_HOST environment variables."
exit 1
fi
echo "[INFO] The following tables will be backup"
echo "$res"
echo "[INFO] POSTGRES backup enabled"
fi
if [ ! -f "$BACKUP_PATH"/config ]; then
echo "[INFO] No backup folder/config found, initiating..."
borgbackup init --encryption=repokey "$BACKUP_PATH"
echo "[INFO] The initialisation use the encryption=repokey, so config and keys are into the backup folder (encrypted with the passphrase, don't loose it)"
fi
if [ -n "$BACKUP_CRON" ]; then
sed -i "s/0 3 \* \* \*/$BACKUP_CRON/" /etc/cron.d/backup
fi
if [ -n "$ONESHOT" ] && [ "$ONESHOT" == "true" ]; then
exec /script_backup.sh
else
# Save env variable for the cron
printenv | sed 's/^\(.*\)$/export \1/g' > /tmp/project_env.sh
chmod 700 /tmp/project_env.sh
exec "$@"
fi