-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-docker-volumes
executable file
·103 lines (85 loc) · 2.01 KB
/
backup-docker-volumes
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
#!/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")
# https://github.com/theobori/dockerv
#
# dockerv source points
#
# You can replace $@ by the sources point you want
# instead of passing CLI arguments
readonly SOURCE_POINTS=( $@ )
# Retention days amount
readonly RETENTION=${RETENTION:-3}
# Backup directory path
readonly BACKUP_DIR=${BACKUP_DIR:-"/backup"}
# dockerv destination point
readonly DESTINATION_POINT="${BACKUP_DIR}/docker-volumes-${DATE}.tar.gz"
# Help display function
print_help() {
cat <<USAGE
${FILENAME}
Version ${VERSION}
Optional arguments
-h Show this help message
-v Print the script version
Usage example
${FILENAME} src1 src2 src3
USAGE
}
# Parsing the CLI arguments with the built-in getopts command
parse() {
while getopts "hv" arg; do
case ${arg} in
h)
print_help
exit 0
;;
v)
echo ${FILENAME} -- ${VERSION}
exit 0
;;
esac
done
if [[ ${#SOURCE_POINTS[@]} -eq 0 ]]
then
echo "Missing sources, check the help message" >&2
return 1
fi
}
# Exporting and packing Docker volumes
# selected with the source points as a Tarball
export_docker_volumes() {
local args=""
for source_point in ${SOURCE_POINTS[@]}
do
args+="--src \"${source_point}\" "
done
if ! dockerv export \
${args} \
--dest ${DESTINATION_POINT} \
--force
then
return 1
fi
}
# 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}"
}
main() {
parse ${@} || exit 1
mkdir -p ${BACKUP_DIR}
echo "Starting Docker volumes export"
export_docker_volumes || exit 1
purge_backups
}
main ${@}