Backup Setup when we don't have root » History » Version 4
Jon Goldberg, 09/07/2021 04:56 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 | #!/bin/bash |
||
11 | 4 | Jon Goldberg | # This script requires jq. Bail if not found. You can download a static binary from the jq homepage. |
12 | if ! command -v ./jq &> /dev/null |
||
13 | then |
||
14 | echo "jq could not be found. Please install and try again" |
||
15 | exit |
||
16 | fi |
||
17 | |||
18 | 1 | Jon Goldberg | # borg backupninja backup script |
19 | REPOSITORY="1234@yourrsyncserver.rsync.net:clientname-servershortname" |
||
20 | export BORG_PASSPHRASE='StrongPassphrase' |
||
21 | # On rsync.net: Specify /usr/local/bin/borg/borg for borg 0.29; /usr/local/bin/borg1 for 1.x |
||
22 | REMOTE_PATH=/usr/local/bin/borg1/borg1 |
||
23 | |||
24 | ICINGA2_API_USER=backupninja |
||
25 | ICINGA2_SERVER_ADDRESS=icinga.megaphonetech.com |
||
26 | ICINGA2_API_PORT=5665 |
||
27 | ICINGA2_API_PASSWORD=<Icinga2 API password> |
||
28 | ICINGA2_HOSTNAME=server.longname.org |
||
29 | # Set level to 0, "OK", and we'll change it if anything goes wrong. |
||
30 | LEVEL=0 |
||
31 | # Run the backup. |
||
32 | OUTPUT=$( ( |
||
33 | 4 | Jon Goldberg | /usr/bin/borg create --warning --filter=AME --stats --compression lz4 \ |
34 | 1 | Jon Goldberg | --remote-path $REMOTE_PATH \ |
35 | $REPOSITORY::'{hostname}-{now:%Y-%m-%d}' \ |
||
36 | /path/to/back/up \ |
||
37 | 3 | Jon Goldberg | --exclude '*/templates_c' \ |
38 | --exclude '*/Maildir' \ |
||
39 | --exclude '.config/borg' \ |
||
40 | 1 | Jon Goldberg | --exclude '*/civicrm/upload/cache' |
41 | ) 2>&1) |
||
42 | if [ $? -ne 0 ] |
||
43 | then |
||
44 | LEVEL=2 |
||
45 | fi |
||
46 | # Remove old backups. |
||
47 | 3 | Jon Goldberg | OUTPUT=${OUTPUT}\\n$( ( |
48 | 1 | Jon Goldberg | /usr/bin/borg prune -v $REPOSITORY --prefix '{hostname}-' --keep-daily=15 --keep-weekly=9 --keep-monthly=6 --remote-path $REMOTE_PATH |
49 | ) 2>&1) |
||
50 | if [ $? -ne 0 ] |
||
51 | then |
||
52 | LEVEL=2 |
||
53 | fi |
||
54 | |||
55 | # Check the integrity of the backup. |
||
56 | OUTPUT=${OUTPUT}\\n$( ( |
||
57 | /usr/bin/borg check $REPOSITORY --remote-path $REMOTE_PATH |
||
58 | ) 2>&1) |
||
59 | if [ $? -ne 0 ] |
||
60 | then |
||
61 | 3 | Jon Goldberg | LEVEL=2 |
62 | 1 | Jon Goldberg | fi |
63 | unset BORG_PASSPHRASE |
||
64 | 4 | Jon Goldberg | # Escape JSON characters |
65 | echo $OUTPUT |
||
66 | OUTPUT=$( echo $OUTPUT | ./jq --raw-input --slurp --ascii-output . ) |
||
67 | echo "after jq" |
||
68 | echo $OUTPUT |
||
69 | 1 | Jon Goldberg | # Send to Icinga |
70 | 4 | Jon Goldberg | DATA="{ \"exit_status\": $LEVEL, \"plugin_output\": ${OUTPUT} }" |
71 | 1 | Jon Goldberg | /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 |
72 | # Reschedule the next dummy backupninja check |
||
73 | RESCHEDULE_TIME=$(/bin/date -d "tomorrow 6am" "+%s") |
||
74 | 3 | Jon Goldberg | RESCHEDULE_JSON="{ \"next_check\": \"${RESCHEDULE_TIME}\" }" |
75 | /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 |
||
76 | 1 | Jon Goldberg | ``` |
77 | |||
78 | 4 | Jon Goldberg | `chmod +x` this file, then add it to the user's `crontab` looking something like this: |
79 | ``` |
||
80 | 0 2 * * * cd /home/members/example/sites/crm.example.org/users/example/bin && /home/members/example/sites/crm.example.org/users/example/bin/borgbackup.sh |
||
81 | ``` |
||
82 | |||
83 | 1 | Jon Goldberg | Add `vars.has_backupninja = true` to the server's Icinga config and restart Icinga. |