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