]> git.somenet.org - root/pub/somesible.git/blob - roles/base/backup/files/default/backup.sh
[roles/server/mail/milter-dmarc] setup dmarc milter
[root/pub/somesible.git] / roles / base / backup / files / default / backup.sh
1 #!/bin/bash
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>
7 #
8
9 umask 0077
10 cd /tmp
11 export ERRCODE=0
12
13 export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK="yes"
14 export BORG_RELOCATED_REPO_ACCESS_IS_OK="yes"
15
16 function backup {
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
22
23     BKPPATH_ESCAPED=$(echo -n "$BKPPATH"|sed -e 's#/#-#g') # contains first "/"
24
25     # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
26     if [[ -n "$BKPPASS" ]]; then
27         export BORG_PASSPHRASE=${BKPPASS}
28     fi
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"
31     else
32         borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e none --make-parent-dirs "$BKPREPO"
33     fi
34
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"
39     done
40
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"
42         exit_status=$?;
43         if [ $exit_status -ne 0 ]; then
44             export ERRCODE="$exit_status";
45             echo "** backup.sh (backup:create): non-zero exitcode: $exit_status"
46         fi
47
48     backup_prune "$BKPREPO" "$BKPKEEP" "$BKPPASS"
49 }
50
51 function backup2 {
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
57
58     BKPPATH_ESCAPED=$(echo -n "$BKPPATH"|sed -e 's#/#-#g') # contains first "/"
59
60     # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
61     if [[ -n "$BKPPASS" ]]; then
62         export BORG_PASSPHRASE=${BKPPASS}
63     fi
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"
66     else
67         borg2 repo-info --repo "$BKPREPO" >/dev/null 2>&1 || borg2 repo-create --umask 0077 -e none --repo "$BKPREPO"
68     fi
69
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"
74     done
75
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"
78         exit_status=$?;
79         if [ $exit_status -ne 0 ]; then
80             export ERRCODE="$exit_status";
81             echo "** backup.sh (backup2:create): non-zero exitcode: $exit_status"
82         fi
83
84     backup_prune2 "$BKPREPO" "$BKPKEEP" "$BKPPASS"
85 }
86
87 function backup_cmd {
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
93
94     BKPPATH_ESCAPED=$(echo -n "$BKPPATH"|sed -e 's#/#-#g') # contains first "/"
95
96     # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
97     if [[ -n "$BKPPASS" ]]; then
98         export BORG_PASSPHRASE=${BKPPASS}
99     fi
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"
102     else
103         borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e none --make-parent-dirs "$BKPREPO"
104     fi
105
106     borg create --umask 0077 --info --stats --noctime --nobirthtime --compression zstd --files-cache disabled --content-from-command -- "$BKPREPO::$BKPNAME--{now}" $BKPCMD
107         exit_status=$?;
108         if [ $exit_status -ne 0 ]; then
109             export ERRCODE="$exit_status";
110             echo "** backup.sh (backup_cmd:create): non-zero exitcode: $exit_status"
111         fi
112
113     backup_prune "$BKPREPO" "$BKPKEEP" "$BKPPASS"
114 }
115
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
122
123     BKPPATH_ESCAPED=$(echo -n "$BKPPATH"|sed -e 's#/#-#g') # contains first "/"
124
125     # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
126     if [[ -n "$BKPPASS" ]]; then
127         export BORG_PASSPHRASE=${BKPPASS}
128     fi
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"
131     else
132         borg2 repo-info --repo "$BKPREPO" >/dev/null 2>&1 || borg2 repo-create --umask 0077 -e none --repo "$BKPREPO"
133     fi
134
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
137         exit_status=$?;
138         if [ $exit_status -ne 0 ]; then
139             export ERRCODE="$exit_status";
140             echo "** backup.sh (backup_cmd2:create): non-zero exitcode: $exit_status"
141         fi
142
143     backup_prune2 "$BKPREPO" "$BKPKEEP" "$BKPPASS"
144 }
145
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
150
151     # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
152     if [[ -n "$BKPPASS" ]]; then
153         export BORG_PASSPHRASE=${BKPPASS}
154     fi
155
156     if [[ -z "$BKPKEEP" ]]; then
157         echo "** backup.sh(backup_prune): No prune settings, skipping"
158     else
159         borg prune --umask 0077 --list --stats --save-space --keep-last 1 $BKPKEEP "$BKPREPO"
160             exit_status=$?;
161             if [ $exit_status -ne 0 ]; then
162                 export ERRCODE="$exit_status";
163                 echo "** backup.sh (backup_prune:prune): non-zero exitcode: $exit_status"
164             fi
165        borg compact --umask 0077 -v --cleanup-commits "$BKPREPO"
166             exit_status=$?;
167             if [ $exit_status -ne 0 ]; then
168                 export ERRCODE="$exit_status";
169                 echo "** backup.sh (backup_prune:compact): non-zero exitcode: $exit_status"
170             fi
171     fi
172 }
173
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
178
179     # Override if exists, but also allow to have BORG_PASSPHRASE set elsewhere.
180     if [[ -n "$BKPPASS" ]]; then
181         export BORG_PASSPHRASE=${BKPPASS}
182     fi
183
184     if [[ -z "$BKPKEEP" ]]; then
185         echo "** backup.sh(backup_prune2): No prune settings, skipping"
186     else
187         echo "** backup.sh(backup_prune2:prune) ********************************************************************************************************************************"
188         time borg2 prune --umask 0077 --list --keep-last 1 $BKPKEEP --repo "$BKPREPO"
189             exit_status=$?;
190             if [ $exit_status -ne 0 ]; then
191                 export ERRCODE="$exit_status";
192                 echo "** backup.sh (backup_prune2:prune): non-zero exitcode: $exit_status"
193             fi
194         echo "** backup.sh(backup_prune2:compact) ********************************************************************************************************************************"
195         time borg2 compact --umask 0077 -v --stats --repo "$BKPREPO"
196             exit_status=$?;
197             if [ $exit_status -ne 0 ]; then
198                 export ERRCODE="$exit_status";
199                 echo "** backup.sh (backup_prune2:compact): non-zero exitcode: $exit_status"
200             fi
201     fi
202 }
203
204
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
209 fi
210
211 # run managed
212 echo "** backup.sh: BEGIN: $(date -Isec)"
213 echo "** backup.sh: running /bkp/local/backup.conf.managed"
214 source /bkp/local/backup.conf.managed
215
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
220 fi
221 echo "** backup.sh: DONE: $(date -Isec)"
222
223
224 unset BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK
225 unset BORG_RELOCATED_REPO_ACCESS_IS_OK
226
227 exit $ERRCODE