Skip to content

Commit 2ef5465

Browse files
committed
introduce logging
1 parent 5dd5cec commit 2ef5465

File tree

5 files changed

+85
-44
lines changed

5 files changed

+85
-44
lines changed

backup.sh

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#
33
# backs up mysql dump and/or other data to local and/or remote borg repository
44

5+
readonly SELF="${0##*/}"
6+
readonly LOG="/var/log/${SELF}.log"
57

68
readonly usage="
7-
usage: ${0##*/} [-h] [-d MYSQL_DBS] [-n NODES_TO_BACKUP] [-c CONTAINERS]
9+
usage: $SELF [-h] [-d MYSQL_DBS] [-n NODES_TO_BACKUP] [-c CONTAINERS]
810
[-r] [-l] [-P BORG_PRUNE_OPTS] [-N BORG_LOCAL_REPO_NAME] -p PREFIX
911
1012
Create new archive
@@ -63,13 +65,44 @@ dump_db() {
6365
}
6466

6567

68+
backup_local() {
69+
borg create -v --stats \
70+
$BORG_EXTRA_OPTS \
71+
$BORG_LOCAL_EXTRA_OPTS \
72+
"$BORG_LOCAL_REPO"::"$ARCHIVE_NAME" \
73+
"${NODES_TO_BACK_UP[@]}" || err "local borg create failed with [$?]"
74+
75+
borg prune -v --list \
76+
"$BORG_LOCAL_REPO" \
77+
--prefix "$PREFIX_WITH_HOSTNAME" \
78+
$BORG_PRUNE_OPTS || err "local borg prune failed with [$?]"
79+
}
80+
81+
82+
backup_remote() {
83+
# duplicate to remote location: (http://borgbackup.readthedocs.io/en/latest/faq.html#can-i-copy-or-synchronize-my-repo-to-another-location)
84+
borg create -v --stats \
85+
$BORG_EXTRA_OPTS \
86+
$BORG_REMOTE_EXTRA_OPTS \
87+
"$REMOTE"::"$ARCHIVE_NAME" \
88+
"${NODES_TO_BACK_UP[@]}" || err "remote borg create failed with [$?]"
89+
90+
borg prune -v --list \
91+
"$REMOTE" \
92+
--prefix "$PREFIX_WITH_HOSTNAME" \
93+
$BORG_PRUNE_OPTS || err "remote borg prune failed with [$?]"
94+
}
95+
96+
6697
# backup selected data
98+
# note the borg processes are executed in a sub-shell, so local & remote backup could be
99+
# run in parallel
67100
do_backup() {
68-
local start_time end_time
101+
local started_pids
69102

70-
readonly start_time="$(date +%H:%M:%S)"
103+
declare -a started_pids=()
71104

72-
echo "=> Backup started at [$start_time]"
105+
log "=> Backup started"
73106

74107
dump_db || fail "db dump failed with [$?]"
75108
expand_nodes_to_back_up
@@ -78,35 +111,19 @@ do_backup() {
78111
pushd -- "$TMP" || fail "unable to pushd into [$TMP]" # cd there because files in $TMP are added without full path (to avoid "$TMP_ROOT" prefix in borg repo)
79112

80113
if [[ "$REMOTE_ONLY" -ne 1 ]]; then
81-
borg create -v --stats \
82-
$BORG_EXTRA_OPTS \
83-
$BORG_LOCAL_EXTRA_OPTS \
84-
"$BORG_LOCAL_REPO"::"$ARCHIVE_NAME" \
85-
"${NODES_TO_BACK_UP[@]}"
86-
87-
borg prune -v --list \
88-
"$BORG_LOCAL_REPO" \
89-
--prefix "$PREFIX_WITH_HOSTNAME" \
90-
$BORG_PRUNE_OPTS
114+
backup_local &
115+
started_pids+=("$!")
91116
fi
92117

93118
if [[ "$LOCAL_ONLY" -ne 1 ]]; then
94-
# duplicate to remote location: (http://borgbackup.readthedocs.io/en/latest/faq.html#can-i-copy-or-synchronize-my-repo-to-another-location)
95-
borg create -v --stats \
96-
$BORG_EXTRA_OPTS \
97-
$BORG_REMOTE_EXTRA_OPTS \
98-
"$REMOTE"::"$ARCHIVE_NAME" \
99-
"${NODES_TO_BACK_UP[@]}"
100-
101-
borg prune -v --list \
102-
"$REMOTE" \
103-
--prefix "$PREFIX_WITH_HOSTNAME" \
104-
$BORG_PRUNE_OPTS
119+
backup_remote &
120+
started_pids+=("$!")
105121
fi
106122

123+
wait "${started_pids[@]}"
124+
107125
popd &> /dev/null
108-
readonly end_time="$(date +%H:%M:%S)"
109-
echo "=> Backup finished at [$end_time]"
126+
log "=> Backup finished"
110127

111128
return 0
112129
}
@@ -184,7 +201,7 @@ cleanup() {
184201
# Entry
185202
# ================
186203
trap -- 'cleanup; exit' EXIT HUP INT QUIT PIPE TERM
187-
source /scripts_common.sh || { echo -e "failed to import /scripts_common.sh"; exit 1; }
204+
source /scripts_common.sh || { echo -e " ERROR: failed to import /scripts_common.sh" | tee "$LOG"; exit 1; }
188205
source /env_vars.sh || fail "failed to import /env_vars.sh"
189206
REMOTE_OR_LOCAL_OPT_COUNTER=0
190207

list.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#
33
# lists contents of local or remote archive
44

5+
readonly SELF="${0##*/}"
6+
readonly LOG="/var/log/${SELF}.log"
57

68
readonly usage="
7-
usage: ${0##*/} [-h] [-r] [-l] [-N BORG_LOCAL_REPO_NAME]
9+
usage: $SELF [-h] [-r] [-l] [-N BORG_LOCAL_REPO_NAME]
810
911
List archives in a borg repository
1012
@@ -54,7 +56,7 @@ validate_config() {
5456
# Entry
5557
# ================
5658
trap -- 'cleanup; exit' EXIT HUP INT QUIT PIPE TERM
57-
source /scripts_common.sh || { echo -e "failed to import /scripts_common.sh"; exit 1; }
59+
source /scripts_common.sh || { echo -e " ERROR: failed to import /scripts_common.sh" | tee "$LOG"; exit 1; }
5860
source /env_vars.sh || fail "failed to import /env_vars.sh"
5961
REMOTE_OR_LOCAL_OPT_COUNTER=0
6062

restore.sh

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#
33
# restores selected borg archive from either local or remote repo to $BACKUP_ROOT
44

5+
readonly SELF="${0##*/}"
6+
readonly LOG="/var/log/${SELF}.log"
57

68
readonly usage="
7-
usage: ${0##*/} [-h] [-d] [-c CONTAINERS] [-r] [-l]
9+
usage: $SELF [-h] [-d] [-c CONTAINERS] [-r] [-l]
810
[-N BORG_LOCAL_REPO_NAME] -a ARCHIVE_NAME
911
1012
Restore data from borg archive
@@ -46,38 +48,37 @@ restore_db() {
4648
[[ "${#sql_files[@]}" -ne 1 ]] && fail "expected to find 1 .sql file in the root of [$RESTORE_DIR], but found ${#sql_files[@]}"
4749
confirm "restore db from mysql dump [${sql_files[*]}]?" || return
4850

49-
if mysql \
51+
mysql \
5052
-h${MYSQL_HOST} \
5153
-P${MYSQL_PORT} \
5254
-u${MYSQL_USER} \
53-
-p${MYSQL_PASS} < "${sql_files[@]}"; then
55+
-p${MYSQL_PASS} < "${sql_files[@]}"
5456

55-
echo " Restore succeeded"
56-
else
57-
echo " Restore failed"
58-
fi
57+
return $?
5958
}
6059

6160

6261
do_restore() {
6362

63+
log "=> Restore started"
6464
pushd -- "$RESTORE_DIR" || fail "unable to pushd into [$RESTORE_DIR]"
6565

6666
if [[ "$LOCAL_REPO" -eq 1 ]]; then
6767
borg extract -v --list \
6868
$BORG_EXTRA_OPTS \
6969
$BORG_LOCAL_EXTRA_OPTS \
70-
"$BORG_LOCAL_REPO"::"$ARCHIVE_NAME" || fail "restoring [$BORG_LOCAL_REPO::$ARCHIVE_NAME] failed"
70+
"$BORG_LOCAL_REPO"::"$ARCHIVE_NAME" || fail "restoring [$BORG_LOCAL_REPO::$ARCHIVE_NAME] failed with [$?]"
7171
elif [[ "$REMOTE_REPO" -eq 1 ]]; then
7272
borg extract -v --list \
7373
$BORG_EXTRA_OPTS \
7474
$BORG_REMOTE_EXTRA_OPTS \
75-
"$REMOTE"::"$ARCHIVE_NAME" || fail "restoring [$REMOTE::$ARCHIVE_NAME] failed"
75+
"$REMOTE"::"$ARCHIVE_NAME" || fail "restoring [$REMOTE::$ARCHIVE_NAME] failed with [$?]"
7676
fi
7777

7878
popd &> /dev/null
7979
KEEP_DIR=1 # from this point onward, we should not delete $RESTORE_DIR on failure
80-
restore_db
80+
restore_db || fail "db restore failed"
81+
log "=> Restore finished"
8182
}
8283

8384

@@ -113,15 +114,15 @@ create_dirs() {
113114

114115
cleanup() {
115116
[[ "$KEEP_DIR" -ne 1 && -d "$RESTORE_DIR" ]] && rm -r -- "$RESTORE_DIR"
116-
[[ -d "$RESTORE_DIR" ]] && echo -e "\n\n -> restored files are in [$RESTORE_DIR]"
117+
[[ -d "$RESTORE_DIR" ]] && log "\n\n -> restored files are in [$RESTORE_DIR]"
117118
}
118119

119120

120121
# ================
121122
# Entry
122123
# ================
123124
trap -- 'cleanup; exit' EXIT HUP INT QUIT PIPE TERM
124-
source /scripts_common.sh || { echo -e "failed to import /scripts_common.sh"; exit 1; }
125+
source /scripts_common.sh || { echo -e " ERROR: failed to import /scripts_common.sh" | tee "$LOG"; exit 1; }
125126
source /env_vars.sh || fail "failed to import /env_vars.sh"
126127
REMOTE_OR_LOCAL_OPT_COUNTER=0
127128

scripts_common.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
readonly BACKUP_ROOT='/backup'
66
readonly CRON_FILE='/config/crontab'
77
readonly SSH_KEY='/config/id_rsa'
8+
readonly LOG_TIMESTAMP_FORMAT='+%Y-%m-%d %H:%M'
9+
readonly ID="id-$$" # for logging
810

911

1012
check_dependencies() {
@@ -65,8 +67,24 @@ confirm() {
6567

6668

6769
fail() {
70+
err "$@"
71+
exit 1
72+
}
73+
74+
75+
# info lvl logging
76+
log() {
77+
local msg
78+
readonly msg="$1"
79+
echo -e "[$(date "$LOG_TIMESTAMP_FORMAT")] [$ID]\tINFO $msg" | tee --append "$LOG"
80+
return 0
81+
}
82+
83+
84+
err() {
6885
local msg
6986
readonly msg="$1"
7087
echo -e "\n\n ERROR: $msg\n\n"
71-
exit 1
88+
echo -e "[$(date "$LOG_TIMESTAMP_FORMAT")] [$ID]\t ERROR $msg" >> "$LOG"
7289
}
90+

setup.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# writes down env vars so they can be sourced by the scripts executed by cron.
66
# also initialises cron & sets ssh key, if available.
77

8+
readonly SELF="${0##*/}"
9+
readonly LOG="/var/log/${SELF}.log"
10+
811
install_crontab() {
912
local cron_dir
1013

@@ -38,7 +41,7 @@ add_remote_to_known_hosts_if_missing() {
3841
}
3942

4043

41-
source /scripts_common.sh || { echo -e "failed to import /scripts_common.sh"; exit 1; }
44+
source /scripts_common.sh || { echo -e " ERROR: failed to import /scripts_common.sh" | tee "$LOG"; exit 1; }
4245
printenv | sed 's/^\(\w\+\)=\(.*\)$/export \1="\2"/g' > /env_vars.sh
4346

4447
install_crontab

0 commit comments

Comments
 (0)