Project

General

Profile

Backup Setup when we don't have root » History » Version 3

Jon Goldberg, 08/30/2021 09:37 PM

1 1 Jon Goldberg
# Backup Setup when we don't have root
2
3
We normally rely on a modified version `backupninja` for backup, which requires root access.  When we don't have root access but we do have `borgbackup`, we have a modified procedure for backing up to rsync.net using borgbackup.
4
5
In the example below, `1234` should be your rsync.net user number, `yourrsyncserver.rsync.net` should be your rsync.net server (e.g. `usw-s008.rsync.net`). `clientname` and `servershortname` can be anything you want as long as they're consistent between `borg init` and the shell script. `server.longname.org` should be the name of the server as Icinga knows it. `/path/to/back/up` should be the path you want to back up.  You can have more than one, separated by spaces.
6
7
Run all the following on a local computer that has its public key on the rsync.net server.
8
9
```shell
10
# Get the public key for the SSH user who will be running the backup on the client's server.
11
# Add it to the rsync.net account:
12
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1SlLpapSVuMm5O35LN/FWNa8DmRgxYRiaIhcrlFetmso3/+6s2ZGaMHtheTmfZuCm9VNHHp3YHXYqsDPPLuxFg4iKw7ccDc6nRRV32rl5doQE6708fZc6dMp4hFIGNLA1RBFvK/7bGBdQw+ryG/rKFuW7o1X2DrhWRYPVwavjqci2NiHEB5SwjLzetXKF6VLnoZxERZJcBG0d8uQ/EgbcMLN4xXbDDsNsofM4wok0aMULucogSvayHezu5HDt6Dta8iDaWbqQtJwtpcgWrqy+HTPZTxJ5PtZuOfG4KrVR0R+TQSdYe6CFFevGPp3TujO03h885zyXPiLqjEmrVLkt " | ssh 1234@yourrsyncserver.rsync.net 'dd of=.ssh/authorized_keys oflag=append conv=notrunc'
13
# Create a borg repo.
14
borg init 1234@yourrsyncserver.rsync.net:clientname-servershortname -e repokey --remote-path=/usr/local/bin/borg1/borg1
15
```
16
17
Then, drop this script on the server (e.g. in `$HOME/bin`):
18
```shell
19
#!/bin/bash
20
# borg backupninja backup script
21
REPOSITORY="1234@yourrsyncserver.rsync.net:clientname-servershortname"
22
export BORG_PASSPHRASE='StrongPassphrase'
23
# On rsync.net: Specify /usr/local/bin/borg/borg for borg 0.29; /usr/local/bin/borg1 for 1.x
24
REMOTE_PATH=/usr/local/bin/borg1/borg1
25
26
ICINGA2_API_USER=backupninja
27
ICINGA2_SERVER_ADDRESS=icinga.megaphonetech.com
28
ICINGA2_API_PORT=5665
29
ICINGA2_API_PASSWORD=<Icinga2 API password>
30
ICINGA2_HOSTNAME=server.longname.org
31
# Set level to 0, "OK", and we'll change it if anything goes wrong.
32
LEVEL=0
33
# Run the backup.
34
OUTPUT=$( (
35
/usr/bin/borg create --warning --filter=AME --stats  --list --compression lz4         \
36
--remote-path $REMOTE_PATH \
37
$REPOSITORY::'{hostname}-{now:%Y-%m-%d}' \
38
/path/to/back/up \
39 3 Jon Goldberg
--exclude '*/templates_c' \
40
--exclude '*/Maildir' \
41
--exclude '.config/borg' \
42
--exclude '*/civicrm/upload/cache'
43 1 Jon Goldberg
) 2>&1)
44
if [ $? -ne 0 ]
45
then
46
  LEVEL=2
47
fi
48
49
# Remove old backups.
50 3 Jon Goldberg
OUTPUT=${OUTPUT}\\n$( (
51 1 Jon Goldberg
/usr/bin/borg prune -v $REPOSITORY --prefix '{hostname}-' --keep-daily=15 --keep-weekly=9 --keep-monthly=6 --remote-path $REMOTE_PATH
52
) 2>&1)
53
if [ $? -ne 0 ]
54
  then
55
  LEVEL=2
56
fi
57
58
# Check the integrity of the backup.
59 3 Jon Goldberg
OUTPUT=${OUTPUT}\\n$( (
60 1 Jon Goldberg
/usr/bin/borg check $REPOSITORY --remote-path $REMOTE_PATH
61
) 2>&1)
62
if [ $? -ne 0 ]
63
  then
64
  LEVEL=2
65
fi
66
unset BORG_PASSPHRASE
67
# Send to Icinga
68 3 Jon Goldberg
DATA="{ \"exit_status\": $LEVEL, \"plugin_output\": \"${OUTPUT}\" }"
69
/usr/bin/curl -k -s -u $ICINGA2_API_USER:$ICINGA2_API_PASSWORD -H 'Accept: application/json' -X POST "https://$ICINGA2_SERVER_ADDRESS:$ICINGA2_API_PORT/v1/actions/process-check-result?service=${ICINGA2_HOSTNAME}!backupninja" --data "${DATA}" > /dev/null
70 1 Jon Goldberg
# Reschedule the next dummy backupninja check
71
RESCHEDULE_TIME=$(/bin/date -d "tomorrow 6am" "+%s")
72 3 Jon Goldberg
RESCHEDULE_JSON="{ \"next_check\": \"${RESCHEDULE_TIME}\" }"
73
/usr/bin/curl -k -s -u $ICINGA2_API_USER:$ICINGA2_API_PASSWORD -H 'Accept: application/json' -X POST "https://$ICINGA2_SERVER_ADDRESS:$ICINGA2_API_PORT/v1/actions/reschedule-check?service=${ICINGA2_HOSTNAME}!backupninja" --data "${RESCHEDULE_JSON}" > /dev/null
74 1 Jon Goldberg
```
75
76
`chmod +x` this file, then add it to the user's `crontab`.
77
Add `vars.has_backupninja = true` to the server's Icinga config and restart Icinga.