backup.in in backup

at master

1#!/bin/sh
2
3BACKUP_CONFIG="{{ config_path }}"
4
5while [ -n "$1" ]
6do
7 [ "$1" = "-c" ] && shift && BACKUP_CONFIG=$1 && shift
8 [ "$1" = "-i" ] && shift && BACKUP_INIT=1
9done
10
11[ -f "$BACKUP_CONFIG" ] && . "$BACKUP_CONFIG"
12
13[ -n "$BACKUP_OUTPUT" ] && exec > $BACKUP_OUTPUT 2>&1
14if [ $(id -u) -ne 0 ]
15then
16 echo "`date`: Must be run as root" >&2
17 exit 1
18fi
19
20__cleanup()
21{
22 if [ -n "$BACKUP_MOUNTED" ]
23 then
24 umount $BACKUP_MOUNTPOINT
25 echo "Backup location unmounted"
26 unset BACKUP_MOUNTED
27 fi
28}
29
30
31echo "Beginning backup: `date`"
32
33# Only do the following if backing up to a server
34if [ -n "$BACKUP_SERVER" ]
35then
36 starttime=`date '+%s'`
37 runningtime=0
38 while [ $runningtime -le $BACKUP_SERVER_WAIT ]
39 do
40 if ping -c 1 $BACKUP_SERVER > /dev/null 2>&1
41 then
42 connected=1
43 break
44 fi
45 sleep 1
46 runningtime=$((`date '+%s'` - $starttime))
47 done
48
49 if [ -n "$connected" ]
50 then
51 echo "$BACKUP_SERVER reachable. Proceeding."
52 else
53 echo "$BACKUP_SERVER NOT reachable. Exiting." >&2
54 echo "Ending backup: `date`"
55 exit -1
56 fi
57fi
58
59if [ -n "$BACKUP_MOUNTPOINT" ]
60then
61 if mount "$BACKUP_MOUNTPOINT" 2>&1
62 then
63 echo "Backup location successfully mounted"
64 BACKUP_MOUNTED=1
65 else
66 echo "Backup location mount FAILED. Confirm entry in /etc/fstab." >&2
67 echo "Quitting."
68 echo "Ending backup: `date`"
69 exit -2
70 fi
71fi
72
73if [ -n "$BACKUP_INIT" ]
74then
75 BORG_PASSPHRASE=$BACKUP_PASSWORD \
76 borg init --encryption=authenticated "$BACKUP_DEST"
77fi
78
79BORG_PASSPHRASE=$BACKUP_PASSWORD borg create --stats \
80 $BACKUP_DEST::`date '+%Y%m%d'` \
81 $BACKUP_FOLDERS 2>&1
82
83__cleanup
84echo "Ending backup: `date`"