-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
91 lines (64 loc) · 1.94 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
#!/bin/bash
PATH=$PATH:/usr/local/bin/
DATE_MORE=$(date +"%Y-%m-%d %H:%M:%S")
DATE_SIMPLE=$(date +"%Y-%m-%d")
# Log function.
log() {
if [[ "$BACKUP_VERBOSE" -ge "$1" ]]; then
echo "[$1][$DATE_MORE] $2"
# Check for log file.
if [[ -n "$BACKUP_LOG_DIR" ]]; then
LOG_FILE="$BACKUP_LOG_DIR/$DATE_SIMPLE.log"
echo "[$1][$DATE_MORE] $2" >> $LOG_FILE
fi
fi
}
log 2 "Starting backup on '$DATE_MORE'..."
log 3 "Using B2 key: $BACKUP_B2_APP_KEY..."
log 3 "Using B2 ID: $BACKUP_B2_ID..."
log 3 "Using B2 bucket: $BACKUP_B2_BUCKET..."
log 3 "Using B2 bucket directory: $BACKUP_B2_DIR..."
log 3 "Using PSQL User: $POSTGRES_USER..."
log 3 "Using PSQL DB: $POSTGRES_DB..."
# Make sure our environmental variables are set.
if [[ -z "$POSTGRES_USER" ]]; then
log 0 "FATAL: PostgreSQL user not set!"
exit 1
fi
if [[ -z "$POSTGRES_DB" ]]; then
log 0 "FATAL: PostgreSQL database not set!"
exit 1
fi
if [[ -z "$BACKUP_B2_ID" ]]; then
log 0 "FATAL: B2 ID not set!"
exit 1
fi
if [[ -z "$BACKUP_B2_APP_KEY" ]]; then
log 0 "FATAL: B2 App Key not set!"
exit 1
fi
if [[ -z "$BACKUP_B2_BUCKET" ]]; then
log 0 "FATAL: B2 bucket not set!"
exit 1
fi
# Dump database.
DUMP_FILE_NAME="${POSTGRES_DB}.pgsql"
FULL_DUMP_PATH="/tmp/${DUMP_FILE_NAME}"
log 3 "Backing up to file '$FULL_DUMP_PATH'..."
pg_dump -U $POSTGRES_USER -d $POSTGRES_DB > $FULL_DUMP_PATH
log 3 "Uploading to Backblaze..."
DUP_CMD=""
if [[ "$BACKUP_DUP_FORCE_INC" -ge 1 ]]; then
DUP_CMD="inc"
elif [[ "BACKUP_DUP_FORCE_FULL" -ge 1 ]]; then
DUP_CMD="full"
fi
DIR=
if [[ -n "$BACKUP_B2_DIR" ]]; then
DIR="/${BACKUP_B2_DIR}"
fi
env PASSPHRASE="$BACKUP_DUP_PASS" duplicity $DUP_CMD --allow-source-mismatch "$FULL_DUMP_PATH" "b2://${BACKUP_B2_ID}:${BACKUP_B2_APP_KEY}@${BACKUP_B2_BUCKET}${DIR}"
# Remove local backup.
log 4 "Removing local backup file '$FULL_DUMP_PATH'..."
rm -f "$FULL_DUMP_PATH"
log 2 "Finished..."