-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-docker-db.sh
executable file
·186 lines (150 loc) · 3.6 KB
/
backup-docker-db.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
#!/usr/bin/env bash
set -e
# Script version
readonly VERSION=0.0.1
# Script filename
readonly FILENAME=$(basename ${0})
# Current date
readonly DATE=$(date "+%Y-%m-%d")
# Retention days amount
readonly RETENTION=${RETENTION:-3}
# Backup directory path
readonly BACKUP_DIR=${BACKUP_DIR:-"/backup"}
# For these variables, you can fill them inside the file
# or using CLI arguments.
# Database username
USERNAME=
# Database password
PASSWORD=
# Database name
DATABASE=
# Database engine
ENGINE=
# Docker container id / name
CONTAINER=
# Operation kind (export, import)
OPERATION=export
# File path to the import file
# (optional is ${OPERATION} is export)
DB_FILE=
# Destination filename without the extension
DESTINATION=
# Help display function
print_help() {
cat <<USAGE
${FILENAME}
Version ${VERSION}
Optional arguments
-h Show this help message
-u Database user
-p Databse password
-d Database name
-e Database engine (mysql, postgresql)
-c Docker container
-f Database file (import only)
-k Operation kind (import, export)
-v Print the script version
USAGE
}
# Parsing the CLI arguments with the built-in getopts command
parse() {
while getopts "k:f:c:e:u:p:d:hvn" arg; do
case ${arg} in
u) USERNAME=${USERNAME:-${OPTARG}} ;;
e) ENGINE=${ENGINE:-${OPTARG}} ;;
p) PASSWORD=${PASSWORD:-${OPTARG}} ;;
d) DATABASE=${DATABASE:-${OPTARG}} ;;
c) CONTAINER=${CONTAINER:-${OPTARG}} ;;
f) DB_FILE=${DB_FILE:-${OPTARG}} ;;
k) OPERATION=${OPTARG} ;;
h)
print_help
exit 0
;;
v)
echo ${FILENAME} -- ${VERSION}
exit 0
;;
esac
done
if [[ -z ${CONTAINER} ]]; then
echo "You must set a container id/name" >&2
return 1
fi
DESTINATION=${BACKUP_DIR}/${ENGINE}-${CONTAINER}-${DATE}
}
# Export a MySQL database inside a Docker container as a SQL file
export_mysl() {
docker exec ${CONTAINER} \
mysqldump \
-u ${USERNAME} \
--password=${PASSWORD} \
${DATABASE} >${DESTINATION}.sql
}
# Import a SQL file into a Docker container containing a MySQL database
import_mysql() {
cat ${DB_FILE} | docker exec -i ${CONTAINER} \
mysql -u ${USERNAME} \
--password=${PASSWORD} \
${DATABASE}
}
# Export a PostgreSQL database inside a Docker container as a SQL file
export_postgresql() {
docker exec ${CONTAINER} \
pg_dump \
-U ${USERNAME} \
${DATABASE} >${DESTINATION}.sql
}
# Import a SQL file into a Docker container containing a PostgreSQL database
import_postgresql() {
cat ${DB_FILE} | docker exec -i ${CONTAINER} \
psql \
-d ${DATABASE} \
-U ${USERNAME}
}
# Deleting files older than ${RETENTION} days
purge_backups() {
find ${BACKUP_DIR} \
-type f \
-mtime +${RETENTION} \
-delete
echo "Deleted files older than ${RETENTION} days in ${BACKUP_DIR}"
}
# Export manager
_export() {
case ${ENGINE} in
mysql) export_mysl ;;
postgresql) export_postgresql ;;
*) return 1 ;;
esac
echo "Exported the database from ${CONTAINER} as ${DESTINATION}"
}
# Import manager
_import() {
case ${ENGINE} in
mysql) import_mysql ;;
postgresql) import_postgresql ;;
*) return 1 ;;
esac
echo "Imported ${DB_FILE} into ${CONTAINER}"
}
# Operation controller
_exec() {
local call=
case ${OPERATION} in
export) call=_export ;;
import) call=_import ;;
esac
if ! ${call}; then
echo "Invalid database engine, check -h" >&2
return 1
fi
}
main() {
parse ${@} || exit 1
test -d ${BACKUP_DIR} || mkdir -p ${BACKUP_DIR}
echo "Starting database ${OPERATION}"
_exec || exit 1
purge_backups
}
main ${@}