---
# System prepended metadata

title: Backupninja & Borgbackup

---

# Backupninja & Borgbackup
## Install backupninja and borg
```
apt install backupninja
apt install borgbackup
```

We add the configuration of the borg creating the file with the appropriate permissions so that the backup can be work correctly: 

This configuration makes the backup at 04:32 of /opt, /etc and /root of that machine, with the borg prune we reduce the number of backups in order not to run out of backup space.


**Remote options**

We have to change the destination directory 'directory' for the one we want to make the backup, which in this case would be the one of test since we created the user test to make the backup of the armeria server. Scheme: /mnt/servers/(user)/backup
The 'host' is the backup machine that with the ssh configuration should get the correct address and finally the corresponding user 'user'.

```
nvim /etc/backup.d/90.borg
```

```
## for more options see
## - example.borg
## - http://borgbackup.readthedocs.io/en/stable

when = everyday at 04:32

[source]
include = /opt
include = /etc
include = /root

## for more info see : borg prune -h
prune = yes
prune_options = --keep-daily=1 --keep-weekly=24 --keep-monthly=12 --keep-within="14d" --save-space

[dest]
directory = /mnt/servers/test/backup
host = backup
user = test
archive = {now:%Y-%m-%dT%H:%M:%S}
compression = lz4
encryption = none
passphrase = 

```
```
chmod 600 /etc/backup.d/90.borg
```


# DD backup
```
mkdir /opt/digitaldemocratic/backup/databases -p
```
## Dump isard-apps-mariadb databases
```
rm /opt/digitaldemocratic/backup/databases/isard-apps-mariadb* 

docker exec isard-apps-mariadb mysqldump --user=root --password=synodSevastopolsConfucian --all-databases | gzip -c > "/opt/digitaldemocratic/backup/databases/isard-apps-mariadb_$(date +"%Y%m%d-%H%M").gz"
```
## Dump isard-apps-postgresql databases
```
rm /opt/digitaldemocratic/backup/databases/isard-apps-postgresql*

docker exec -t isard-apps-postgresql pg_dumpall -c -U admin | gzip -c > "/opt/digitaldemocratic/backup/databases/isard-apps-postgresql_$(date +"%Y%m%d-%H%M").gz"
```
## Backup everything with borg

All the data that needs backup is at /opt/digitaldemocratic

## Create and run script for extract dump
```
nvim /etc/backup.d/80.sh
```

example: 
```
#!/bin/bash
declare -r TODAY=$(date +%Y%m%d%H%M%S)        # YYYYMMDDhhmmss
mkdir /opt/digitaldemocratic/backup/databases -p
chown root /opt/digitaldemocratic/backup
chmod 700 /opt/digitaldemocratic/backup
rm /opt/digitaldemocratic/backup/databases/isard-apps-mariadb* 
docker exec isard-apps-mariadb mysqldump --user=root --password=synodSevastopolsConfucian --all-databases > "/opt/src/digitaldemocratic/backup/databases/isard-apps-mariadb_$(date +"%Y%m%d-%H%M").sql"
rm /opt/digitaldemocratic/backup/databases/isard-apps-postgresql*
docker exec -t isard-apps-postgresql pg_dumpall -c -U admin > "/opt/src/digitaldemocratic/backup/databases/isard-apps-postgresql_$(date +"%Y%m%d-%H%M").sql"
chmod 600 /opt/digitaldemocratic/backup/*
```
```
chmod 600 /etc/backup.d/80.sh
```

## Run backupninja with borg

```
ninjahelper
```

## Commands borg 
```
borg info /directory-backup/
borg list /directory-backup/

#Note

Currently, extract always writes into the current working directory (“.”), so make sure you cd to the right place before calling borg extract.

When parent directories are not extracted (because of using file/directory selection or any other reason), borg can not restore parent directories’ metadata, e.g. owner, group, permission, etc.
Examples

# Extract entire archive
borg extract /path/to/repo::my-files

# Extract entire archive and list files while processing
borg extract --list /path/to/repo::my-files

# Verify whether an archive could be successfully extracted, but do not write files to disk
borg extract --dry-run /path/to/repo::my-files

# Extract the "src" directory
borg extract /path/to/repo::my-files home/USERNAME/src

# Extract the "src" directory but exclude object files
borg extract /path/to/repo::my-files home/USERNAME/src --exclude '*.o'

# Restore a raw device (must not be active/in use/mounted at that time)
borg extract --stdout /path/to/repo::my-sdx | dd of=/dev/sdx bs=10M


example extract one file comand:
borg extract -v --list /mnt/servers/test/backup::2022-08-01T02:00:03 opt/isard/certs/default/ca-cert.pem
```
More Borg:
https://borgbackup.readthedocs.io/en/stable/usage/general.html


## Restoring isard-apps-mariadb
```
zcat /opt/src/digitaldemocratic/backup/databases/isard-apps-mariadb_20221104-1110.gz | docker exec -i isard-apps-mariadb mysql --user root --password=PASSWORD
```

## Restoring isard-apps-postgresql
```
gunzip < /opt/src/digitaldemocratic/backup/databases/isard-apps-postgresql_20221104-1110.gz | docker exec -i isard-apps-postgresql psql -U admin
```
