-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
executable file
·170 lines (134 loc) · 3.95 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
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
#!/usr/bin/env bash
#
# server backup script
# 2015, 2016 Michał Słomkowski
#
export TEST=0
export VERBOSE=0
SIDE_SCRIPTS_ONLY=0
function show_help {
echo "Usage: todo" # todo write usage
}
while getopts "h?vts" opt; do
case "${opt}" in
h|\?)
show_help
exit 0
;;
v) export VERBOSE=1
;;
t) export TEST=1
export ECHO=echo
;;
s) SIDE_SCRIPTS_ONLY=1
;;
esac
done
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
export CONFIG_DIR="${SCRIPT_DIR}/conf.d"
# load all config files
for confFile in `find "${CONFIG_DIR}" -name '*.sh' ! -name 'example-*'`
do
. "${confFile}"
done
function full_date {
eval date +'%Y-%m-%d.%H.%M'
}
function run_sidescripts {
[[ ${VERBOSE} -ne 0 ]] && echo "** running side scripts in phase ${1}"
for scriptFile in `find "${SCRIPT_DIR}" -path '*/'"${1}.d/"'*.sh' | sort -n`
do
if [[ `basename ${scriptFile}` =~ ([0-9]+?)([a-z_]+)(\.sh) ]]
then
scriptName=${BASH_REMATCH[2]}
scriptEnabledVariableName=`echo ${1}_${scriptName} | tr '[a-z]' '[A-Z]'`_ENABLED
if [[ ${!scriptEnabledVariableName} -ne 1 ]]
then
continue
fi
export SCRIPT_TMP_DIR=`mktemp -d --suffix=_${1}_${scriptName}`
currentDir=`pwd`
cd ${SCRIPT_TMP_DIR}
[[ ${VERBOSE} -ne 0 ]] && echo "* running ${scriptName}"
bash ${scriptFile}
cd ${currentDir}
rm -r ${SCRIPT_TMP_DIR}
fi
done
}
export -f full_date
# put lock
PID_FILE="/tmp/backup_scripts.pid"
if [ -s ${PID_FILE} ]; then
PID=$(cat ${PID_FILE})
ps ${PID} > /dev/null
if [ ${?} -eq 0 ]; then
echo "Backup already running (PID ${PID}), aborting."
exit 1
fi
fi
[[ ${ECHO} != '' ]] && echo "echo $$ > ${PID_FILE}" || echo $$ > ${PID_FILE}
run_sidescripts premount
BORG_OPTIONS=""
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
if [[ ${SIDE_SCRIPTS_ONLY} -eq 0 ]]
then
if [ -n "${DST_REMOTE_LOCATION}" ]
then
${ECHO} mkdir -p ${DST_LOCAL_LOCATION}
${ECHO} sshfs -p ${DST_REMOTE_PORT} ${DST_REMOTE_USER}@${DST_REMOTE_HOST}:${DST_REMOTE_LOCATION} ${DST_LOCAL_LOCATION}
fi
run_sidescripts predump
out="${DST_LOCAL_LOCATION}/${HOSTNAME}"
${ECHO} mkdir -p ${out}
out+='/repository.borg'
export BORG_REPOSITORY_PATH="${out}"
if [[ "${VERBOSE}" -ne 0 ]]
then
BORG_OPTIONS+=" --stats"
echo "** starting Borg backup"
fi
export BORG_PASSPHRASE='backup'
if [ ! -d ${BORG_REPOSITORY_PATH} ]
then
${ECHO} borg init --encryption=${ENCRYPTION_MODE} ${BORG_REPOSITORY_PATH}
fi
for directory in ${SRC_DIRECTORIES}
do
if [[ `basename ${directory}` == '/' ]]
then
prefix="root_dir"
else
prefix=`basename ${directory} | tr '/' '__' `
fi
# construct exclude list
EXCLUDE_LIST="--exclude ${DST_LOCAL_LOCATION} "
for entry in `cat ${CONFIG_DIR}/excludes.txt`
do
EXCLUDE_LIST+=" --exclude ${entry}"
done
for entry in ${SRC_DIRECTORIES}
do
if [ ${entry} != ${directory} ]
then
if [ "${directory##$entry}" == "${directory}" ]
then
EXCLUDE_LIST+=" --exclude ${entry}"
fi
fi
done
${ECHO} ionice -c3 -t borg create ${BORG_OPTIONS} --one-file-system ${BORG_REPOSITORY_PATH}::$(full_date)-${prefix} ${directory} ${EXCLUDE_LIST}
done
${ECHO} ionice -c3 -t borg prune ${BORG_OPTIONS} ${BORG_REPOSITORY_PATH} ${BORG_PRUNE_AGES}
run_sidescripts postdump
sleep 3
if [ -n "${DST_REMOTE_LOCATION}" ]
then
${ECHO} umount ${DST_LOCAL_LOCATION}
${ECHO} rmdir ${DST_LOCAL_LOCATION}
fi
run_sidescripts postumount
fi
# remove lock
${ECHO} rm ${PID_FILE}