]> git.somenet.org - root/pub/somesible.git/blob - roles/base/backup/files/default/backup.sh
roles/service/nextcloud/defaults
[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-2024 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 BORG_PASSPHRASE=${5} # borg key passphrase
22
23     BKPPATH_ESCAPED=$(echo -n "$BKPPATH"|sed -e 's#/#-#g') # contains first "/"
24
25     if [[ -z "$BORG_PASSPHRASE" ]]; then
26         borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e none --make-parent-dirs "$BKPREPO"
27     else
28         borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e repokey-blake2 --make-parent-dirs "$BKPREPO"
29     fi
30
31     echo "# Merged on: $(date -Isec)" > "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED"
32     for file in /bkp/local/exclude.conf.d/$BKPHOST-$BKPPATH_ESCAPED*; do
33         echo -e "\n\n# $file" >> "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED"
34         cat "$file" >> "/bkp/local/exclude.conf.d/.merged.$BKPHOST-$BKPPATH_ESCAPED"
35     done
36
37     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"
38         exit_status=$?;
39         if [ $exit_status -ne 0 ]; then
40             export ERRCODE="$exit_status";
41             echo "** backup.sh (backup:create): non-zero exitcode: $exit_status"
42         fi
43
44     backup_prune "$BKPREPO" "$BKPKEEP" "$BORG_PASSPHRASE"
45 }
46
47 function backup_cmd {
48     export BKPREPO=${1} # backup repo (should be: "/bkp/storage-local/$host/$path" or for ssh: "user@server:$path")
49     export BKPNAME=${2} # backup name
50     export BKPCMD=${3}  # command to run
51     export BKPKEEP=${4} # pruning-settings
52     export BORG_PASSPHRASE=${5} # borg key passphrase
53
54     if [[ -z "$BORG_PASSPHRASE" ]]; then
55         borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e none --make-parent-dirs "$BKPREPO"
56     else
57         borg info "$BKPREPO" >/dev/null 2>&1 || borg init --umask 0077 -e repokey-blake2 --make-parent-dirs "$BKPREPO"
58     fi
59
60     borg create --umask 0077 --info --stats --noctime --nobirthtime --compression zstd --files-cache disabled --content-from-command -- "$BKPREPO::$BKPNAME--{now}" $BKPCMD
61         exit_status=$?;
62         if [ $exit_status -ne 0 ]; then
63             export ERRCODE="$exit_status";
64             echo "** backup.sh (backup_cmd:create): non-zero exitcode: $exit_status"
65         fi
66
67     backup_prune "$BKPREPO" "$BKPKEEP" "$BORG_PASSPHRASE"
68 }
69
70 function backup_prune {
71     export BKPREPO=${1} # backup repo (should be: "/bkp/storage-local/$host/$path" or for ssh: "user@server:$path")
72     export BKPKEEP=${2} # pruning-settings
73     export BORG_PASSPHRASE=${3} # borg key passphrase
74
75     if [[ -z "$BKPKEEP" ]]; then
76         echo "** backup.sh(backup_prune): No prune settings, skipping"
77     else
78         borg prune --umask 0077 --list --stats --save-space --keep-last 1 $BKPKEEP "$BKPREPO"
79             exit_status=$?;
80             if [ $exit_status -ne 0 ]; then
81                 export ERRCODE="$exit_status";
82                 echo "** backup.sh (backup_prune:prune): non-zero exitcode: $exit_status"
83             fi
84        borg compact -v --cleanup-commits "$BKPREPO"
85             exit_status=$?;
86             if [ $exit_status -ne 0 ]; then
87                 export ERRCODE="$exit_status";
88                 echo "** backup.sh (backup_prune:compact): non-zero exitcode: $exit_status"
89             fi
90     fi
91 }
92
93 # run managed
94 echo "** backup.sh: running /bkp/local/backup.conf.managed"
95 source /bkp/local/backup.conf.managed
96
97 # run local additions
98 if [ -e "/bkp/local/backup.conf.local" ]; then
99     echo "** backup.sh: running /bkp/local/backup.conf.local"
100     source /bkp/local/backup.conf.local
101 fi
102
103 echo "** backup.sh: DONE"
104
105 unset BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK
106 unset BORG_RELOCATED_REPO_ACCESS_IS_OK
107
108 exit $ERRCODE