-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_tool_borg.sh
More file actions
executable file
·212 lines (181 loc) · 5.56 KB
/
Copy pathbackup_tool_borg.sh
File metadata and controls
executable file
·212 lines (181 loc) · 5.56 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash
set -euo pipefail
# set -x
# --- Configuration & Defaults ---
HOSTNAME="$(hostname)"
readonly HOSTNAME
readonly CONFIG_DIR="${HOME}/.config/borg"
readonly LOCK_FILE="/tmp/borg_backup_${HOSTNAME}.lock"
# Load local overrides
if [[ -f "${CONFIG_DIR}/config.conf" ]]; then
# shellcheck source=/dev/null
source "${CONFIG_DIR}/config.conf"
fi
export BORG_REPO="${BORG_REPO:-/backups/${HOSTNAME}-backup}"
readonly RETENTION_AGE="${RETENTION_AGE:-1m}"
# Disable encryption & suppress interactive prompts
export BORG_PASSPHRASE=""
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK="yes"
# --- Global dry-run flag ---
DRY_RUN_OPTS=()
# --- Functions ---
setup_environment() {
if ! command -v borg >/dev/null 2>&1; then
echo "Error: borg is not installed or not in PATH" >&2
exit 1
fi
mkdir -p "${CONFIG_DIR}"
touch "${CONFIG_DIR}/exclude.conf" "${CONFIG_DIR}/include.conf"
# Create target dir only for local paths
if [[ "${BORG_REPO}" == /* ]]; then
mkdir -p "${BORG_REPO}"
fi
# Initialize repository if it doesn't exist yet
if ! borg info >/dev/null 2>&1; then
echo "Initializing new borg repository at ${BORG_REPO}"
borg init --encryption=none
fi
}
usage() {
cat <<EOF
Universal Borg Backup Script
Usage: $(basename "$0") [OPTIONS] COMMAND
Commands:
backup Perform backup and prune old backups
status Show backup repository status
list List files in latest backup
verify Verify repository integrity
prune Remove backups older than ${RETENTION_AGE}
cleanup Release stale repository lock
help Show this help message
Options:
--dry-run Simulate operation without making changes
Configuration:
Override defaults by creating: ${CONFIG_DIR}/config.conf
Example config.conf:
BORG_REPO="ssh://backup_user@192.168.1.100/remote-backup"
RETENTION_AGE="3m"
Include paths: ${CONFIG_DIR}/include.conf (one path per line)
Exclude patterns: ${CONFIG_DIR}/exclude.conf (one glob pattern per line)
EOF
}
notify() {
local message="$1"
local title="${2:-Backup}"
command -v notify-send >/dev/null 2>&1 || return 0
notify-send \
--icon=drive-harddisk \
--app-name="System Backup" \
--urgency=low \
"${title}" "${message}" || true
}
remove_old_backups() {
echo "Pruning old backups"
borg prune \
--list \
--keep-within "${RETENTION_AGE}" \
--force \
--save-space \
"${DRY_RUN_OPTS[@]}"
if [[ ${#DRY_RUN_OPTS[@]} -eq 0 ]]; then
borg compact
fi
}
backup() {
# Ensure lock cleanup on script exit
trap 'rm -f "${LOCK_FILE}"' EXIT
exec 9>"${LOCK_FILE}"
if ! flock -n 9; then
echo "Error: Another backup is currently running." >&2
exit 1
fi
# Read source paths from include.conf
local -a source_paths=()
while IFS= read -r line || [[ -n "${line}" ]]; do
[[ -z "${line}" || "${line}" == \#* ]] && continue
source_paths+=("${line}")
done <"${CONFIG_DIR}/include.conf"
if [[ ${#source_paths[@]} -eq 0 ]]; then
echo "Error: ${CONFIG_DIR}/include.conf is empty. Add paths to back up." >&2
exit 1
fi
echo "Starting backup to ${BORG_REPO}..."
local output
output=$(
borg create \
--stats \
--compression auto,zstd \
--exclude-caches \
--json \
--exclude-from "${CONFIG_DIR}/exclude.conf" \
"${DRY_RUN_OPTS[@]}" \
"::${HOSTNAME}-$(date '+%Y-%m-%dT%H:%M:%S')" \
"${source_paths[@]}" 2>&1
) || {
echo "Backup failed! See details below:" >&2
echo "${output}" >&2
notify "Backup failed. Check logs for details." "Backup Failed"
exit 1
}
echo "Backup completed. Processing statistics..."
echo "${output}"
remove_old_backups
duration=$(echo "${output}" | jq -r '.archive.duration // 0' 2>/dev/null)
nfiles=$(echo "${output}" | jq -r '.archive.stats.nfiles // 0' 2>/dev/null)
orig_size=$(echo "${output}" | jq -r '.archive.stats.original_size // 0' 2>/dev/null)
dedup_size=$(echo "${output}" | jq -r '.archive.stats.deduplicated_size // 0' 2>/dev/null)
# Format sizes (e.g., 107437043447 -> 101GiB)
orig_human=$(numfmt --to=iec-i --suffix=B "${orig_size}" 2>/dev/null || echo "${orig_size}B")
dedup_human=$(numfmt --to=iec-i --suffix=B "${dedup_size}" 2>/dev/null || echo "${dedup_size}B")
dur_human=$(date -d@"${duration%.*}" -u +%H:%M:%S)
summary="Time: ${dur_human}\nFiles: ${nfiles}\nProcessed: ${orig_human}\nAdded to Repo: ${dedup_human}"
notify "${summary}" "Backup Successful"
}
# --- Main ---
if [[ ${1:-} == --dry-run ]]; then
DRY_RUN_OPTS=(--dry-run)
echo "DRY RUN MODE: No changes will be made"
shift
fi
cmd="${1:-}"
case "${cmd}" in
backup)
setup_environment
backup
;;
cleanup)
setup_environment
borg break-lock
echo "Repository lock released."
;;
prune)
setup_environment
remove_old_backups
;;
status)
setup_environment
borg info
;;
list)
setup_environment
latest=$(borg list --last 1 --format '{name}{NL}' 2>/dev/null | head -1 || true)
if [[ -n "${latest}" ]]; then
borg list "::${latest}"
else
echo "No archives found in repository." >&2
exit 1
fi
;;
verify)
setup_environment
borg check --verify-data
;;
help)
usage
;;
*)
echo "Error: unknown command '${cmd}'" >&2
usage
exit 1
;;
esac