2 ################################################
3 ### Managed by someone's ansible provisioner ###
4 ################################################
5 # Part of: https://git.somenet.org/root/pub/somesible.git
6 # 2017-2026 by someone <someone@somenet.org>
13 export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK="yes"
14 export BORG_RELOCATED_REPO_ACCESS_IS_OK="yes"
17 export BKPREPO=${1} # backup repo (should be: "/bkp/storage-local/$host/$path" or for ssh: "user@server:$path")
18 export BKPHOST=${2} # src-host
19 export BKPPATH=${3} # abs-path on src-host
20 export BKPKEEP=${4} # pruning-settings
21 export BKPPASS=${5} # borg key passphrase
23 BKPPATH_ESCAPED=$(echo -n "$BKPPATH"|sed -e 's#/#-#g') # contains first "/"
25 # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
26 if [[ -n "$BKPPASS" ]]; then
27 export BORG_PASSPHRASE=${BKPPASS}
29 if [[ -n "$BORG_PASSPHRASE" ]]; then
30 borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e repokey-blake2 --make-parent-dirs "$BKPREPO"
32 borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e none --make-parent-dirs "$BKPREPO"
35 echo "# Merged on: $(date -Isec)" > "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED"
36 for file in /bkp/local/exclude.conf.d/$BKPHOST-$BKPPATH_ESCAPED*; do
37 echo -e "\n\n# $file" >> "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED"
38 cat "$file" >> "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED"
41 borg create --umask 0077 --info --list --stats --noctime --nobirthtime --exclude-caches --exclude-from "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED" "$BKPREPO::$BKPHOST-$BKPPATH_ESCAPED--{now}" "$BKPPATH"
43 if [ $exit_status -ne 0 ]; then
44 export ERRCODE="$exit_status";
45 echo "** backup.sh (backup:create): non-zero exitcode: $exit_status"
48 backup_prune "$BKPREPO" "$BKPKEEP" "$BKPPASS"
52 export BKPREPO=${1} # backup repo (should be: "/bkp/storage-local/$host/$path" or for ssh: "ssh://user@server/$path")
53 export BKPHOST=${2} # src-host
54 export BKPPATH=${3} # abs-path on src-host
55 export BKPKEEP=${4} # pruning-settings
56 export BKPPASS=${5} # borg key passphrase
58 BKPPATH_ESCAPED=$(echo -n "$BKPPATH"|sed -e 's#/#-#g') # contains first "/"
60 # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
61 if [[ -n "$BKPPASS" ]]; then
62 export BORG_PASSPHRASE=${BKPPASS}
64 if [[ -n "$BORG_PASSPHRASE" ]]; then
65 borg2 repo-info --repo "$BKPREPO" >/dev/null 2>&1 || borg2 repo-create --umask 0077 -e repokey-blake2-chacha20-poly1305 --repo "$BKPREPO"
67 borg2 repo-info --repo "$BKPREPO" >/dev/null 2>&1 || borg2 repo-create --umask 0077 -e none --repo "$BKPREPO"
70 echo "# Merged on: $(date -Isec)" > "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED"
71 for file in /bkp/local/exclude.conf.d/$BKPHOST-$BKPPATH_ESCAPED*; do
72 echo -e "\n\n# $file" >> "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED"
73 cat "$file" >> "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED"
76 echo "** backup.sh(backup2:create) ********************************************************************************************************************************"
77 borg2 create --umask 0077 --info --list --stats --nobirthtime --compression zstd --exclude-caches --exclude-from "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED" --repo "$BKPREPO" "$BKPHOST-$BKPPATH_ESCAPED" "$BKPPATH"
79 if [ $exit_status -ne 0 ]; then
80 export ERRCODE="$exit_status";
81 echo "** backup.sh (backup2:create): non-zero exitcode: $exit_status"
84 backup_prune2 "$BKPREPO" "$BKPKEEP" "$BKPPASS"
88 export BKPREPO=${1} # backup repo (should be: "/bkp/storage-local/$host/$path" or for ssh: "user@server:$path")
89 export BKPNAME=${2} # backup name
90 export BKPCMD=${3} # command to run
91 export BKPKEEP=${4} # pruning-settings
92 export BKPPASS=${5} # borg key passphrase
94 BKPPATH_ESCAPED=$(echo -n "$BKPPATH"|sed -e 's#/#-#g') # contains first "/"
96 # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
97 if [[ -n "$BKPPASS" ]]; then
98 export BORG_PASSPHRASE=${BKPPASS}
100 if [[ -n "$BORG_PASSPHRASE" ]]; then
101 borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e repokey-blake2 --make-parent-dirs "$BKPREPO"
103 borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e none --make-parent-dirs "$BKPREPO"
106 borg create --umask 0077 --info --stats --noctime --nobirthtime --compression zstd --files-cache disabled --content-from-command -- "$BKPREPO::$BKPNAME--{now}" $BKPCMD
108 if [ $exit_status -ne 0 ]; then
109 export ERRCODE="$exit_status";
110 echo "** backup.sh (backup_cmd:create): non-zero exitcode: $exit_status"
113 backup_prune "$BKPREPO" "$BKPKEEP" "$BKPPASS"
116 function backup_cmd2 {
117 export BKPREPO=${1} # backup repo (should be: "/bkp/storage-local/$host/$path" or for ssh: "ssh://user@server/$path")
118 export BKPNAME=${2} # backup name
119 export BKPCMD=${3} # command to run
120 export BKPKEEP=${4} # pruning-settings
121 export BKPPASS=${5} # borg key passphrase
123 BKPPATH_ESCAPED=$(echo -n "$BKPPATH"|sed -e 's#/#-#g') # contains first "/"
125 # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
126 if [[ -n "$BKPPASS" ]]; then
127 export BORG_PASSPHRASE=${BKPPASS}
129 if [[ -n "$BORG_PASSPHRASE" ]]; then
130 borg2 repo-info --repo "$BKPREPO" >/dev/null 2>&1 || borg2 repo-create --umask 0077 -e repokey-blake2-chacha20-poly1305 --repo "$BKPREPO"
132 borg2 repo-info --repo "$BKPREPO" >/dev/null 2>&1 || borg2 repo-create --umask 0077 -e none --repo "$BKPREPO"
135 echo "** backup.sh(backup_cmd2:create) ********************************************************************************************************************************"
136 borg2 create --umask 0077 --info --stats --nobirthtime --compression zstd --files-cache disabled --content-from-command --repo "$BKPREPO" "$BKPNAME" -- $BKPCMD
138 if [ $exit_status -ne 0 ]; then
139 export ERRCODE="$exit_status";
140 echo "** backup.sh (backup_cmd2:create): non-zero exitcode: $exit_status"
143 backup_prune2 "$BKPREPO" "$BKPKEEP" "$BKPPASS"
146 function backup_prune {
147 export BKPREPO=${1} # backup repo (should be: "/bkp/storage-local/$host/$path" or for ssh: "user@server:$path")
148 export BKPKEEP=${2} # pruning-settings
149 export BKPPASS=${3} # borg key passphrase
151 # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
152 if [[ -n "$BKPPASS" ]]; then
153 export BORG_PASSPHRASE=${BKPPASS}
156 if [[ -z "$BKPKEEP" ]]; then
157 echo "** backup.sh(backup_prune): No prune settings, skipping"
159 borg prune --umask 0077 --list --stats --save-space --keep-last 1 $BKPKEEP "$BKPREPO"
161 if [ $exit_status -ne 0 ]; then
162 export ERRCODE="$exit_status";
163 echo "** backup.sh (backup_prune:prune): non-zero exitcode: $exit_status"
165 borg compact --umask 0077 -v --cleanup-commits "$BKPREPO"
167 if [ $exit_status -ne 0 ]; then
168 export ERRCODE="$exit_status";
169 echo "** backup.sh (backup_prune:compact): non-zero exitcode: $exit_status"
174 function backup_prune2 {
175 export BKPREPO=${1} # backup repo (should be: "/bkp/storage-local/$host/$path" or for ssh: "ssh://user@server/$path")
176 export BKPKEEP=${2} # pruning-settings
177 export BKPPASS=${3} # borg key passphrase
179 # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
180 if [[ -n "$BKPPASS" ]]; then
181 export BORG_PASSPHRASE=${BKPPASS}
184 if [[ -z "$BKPKEEP" ]]; then
185 echo "** backup.sh(backup_prune2): No prune settings, skipping"
187 echo "** backup.sh(backup_prune2:prune) ********************************************************************************************************************************"
188 time borg2 prune --umask 0077 --list --keep-last 1 $BKPKEEP --repo "$BKPREPO"
190 if [ $exit_status -ne 0 ]; then
191 export ERRCODE="$exit_status";
192 echo "** backup.sh (backup_prune2:prune): non-zero exitcode: $exit_status"
194 echo "** backup.sh(backup_prune2:compact) ********************************************************************************************************************************"
195 time borg2 compact --umask 0077 -v --stats --repo "$BKPREPO"
197 if [ $exit_status -ne 0 ]; then
198 export ERRCODE="$exit_status";
199 echo "** backup.sh (backup_prune2:compact): non-zero exitcode: $exit_status"
205 # run local pre managed
206 if [ -e "/bkp/local/backup.conf.local-pre" ]; then
207 echo "** backup.sh: running /bkp/local/backup.conf.local-pre"
208 source /bkp/local/backup.conf.local-pre
212 echo "** backup.sh: BEGIN: $(date -Isec)"
213 echo "** backup.sh: running /bkp/local/backup.conf.managed"
214 source /bkp/local/backup.conf.managed
216 # run local additions
217 if [ -e "/bkp/local/backup.conf.local" ]; then
218 echo "** backup.sh: running /bkp/local/backup.conf.local"
219 source /bkp/local/backup.conf.local
221 echo "** backup.sh: DONE: $(date -Isec)"
224 unset BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK
225 unset BORG_RELOCATED_REPO_ACCESS_IS_OK