selfhosted-apps-docker/borg_backup/readme.md

176 lines
5.0 KiB
Markdown
Raw Normal View History

2020-04-09 22:52:11 +00:00
# BorgBackup in docker
2020-05-18 22:49:18 +00:00
###### guide-by-example
2020-04-09 22:52:11 +00:00
2020-04-25 09:09:46 +00:00
![logo](https://i.imgur.com/dR50bkP.png)
2020-04-09 22:52:11 +00:00
2020-05-06 23:49:16 +00:00
# Purpose & Overview
2020-04-25 09:09:46 +00:00
Backups.
2020-04-09 22:52:11 +00:00
* [Official site](https://www.borgbackup.org/)
* [Github](https://github.com/borgbackup/borg)
2020-05-06 23:49:16 +00:00
Borg is an open source deduplicating archiver with compression and encryption.</br>
Written in python with performance critical code implemented in C/Cython.
Highlight of borg is the deduplication, where files are cut in to variable size
chunks, and only new chunks are stored.
This allows to keep snapshots from several days, weeks and months,
while not wasting disk space.
In this setup borg is installed directly on the host system.</br>
2020-05-07 00:14:43 +00:00
A script is created that backs up the entire docker directory and /etc locally.</br>
Cronjob is set to execute this script daily.
2020-05-06 23:49:16 +00:00
2020-05-07 00:14:43 +00:00
The repository is also pruned on each run of the script -
old archives are deleted while keeping the ones fitting the retention rules
in the script.</br>
One backup per day for last 7 days, last 4 weeks, last 6 months are kept.
# Files and directory structure
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
```
2020-05-01 09:38:43 +00:00
/home/
└── ~/
├── borg/
│ ├── docker_backup/
│ ├── borg_backup.sh
│ └── borg_backup.log
2020-04-25 22:57:10 +00:00
2020-05-01 09:38:43 +00:00
└── docker/
2020-04-25 22:57:10 +00:00
├── container-setup #1
├── container-setup #2
2020-04-25 22:59:40 +00:00
├── ...
2020-04-25 22:57:10 +00:00
```
2020-04-09 22:52:11 +00:00
2020-05-10 01:26:52 +00:00
* `docker_backup/` - borg repository directory containg the backups
2020-05-07 00:02:19 +00:00
* `borg_backup.sh` - the backup script that adds new archive in to the repository
2020-05-07 00:14:43 +00:00
* `borg_backup.log` - log file with the dates of backups
Only `borg_backup.sh` has to be provided.</br>
Repo directory is created by `borg init` command
and the log file is created on the first run.
2020-05-06 23:49:16 +00:00
2020-04-25 09:09:46 +00:00
# The setup
2020-04-16 22:05:54 +00:00
2020-05-06 23:49:16 +00:00
#### Install BorgBackup
2020-04-16 22:05:54 +00:00
2020-05-06 23:49:16 +00:00
Borg is likely in your linux repositories.
2020-04-16 22:05:54 +00:00
2020-05-06 23:49:16 +00:00
#### Create a new borg repo
2020-05-09 15:13:49 +00:00
2020-04-25 22:57:10 +00:00
`mkdir ~/borg`</br>
2020-05-21 11:34:42 +00:00
`borg init --encryption=none ~/borg/docker_backup`
2020-05-09 15:13:49 +00:00
2022-04-03 08:50:12 +00:00
Note the lack of sudo for initialization, this allows regular user to browse it.
But the script should be run as root to have full access.
2020-04-16 22:05:54 +00:00
2020-05-06 23:49:16 +00:00
#### The backup script
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
`borg_backup.sh`
```bash
#!/bin/bash
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
# INITIALIZE THE REPO WITH THE COMMAND:
# borg init --encryption=none ~/borg/my_backup
# THEN RUN THIS SCRIPT
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
# -----------------------------------------------
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
BACKUP_THIS='/home/bastard/docker /etc'
REPOSITORY='/home/bastard/borg/docker_backup'
LOGFILE='/home/bastard/borg/borg_backup.log'
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
# -----------------------------------------------
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
NOW=$(date +"%Y-%m-%d | %H:%M | ")
echo "$NOW Starting Backup and Prune" >> $LOGFILE
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
# CREATES NEW ARCHIVE IN PRESET REPOSITORY
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
borg create \
$REPOSITORY::'{now:%s}' \
$BACKUP_THIS \
\
--compression zstd \
--one-file-system \
--exclude-caches \
--exclude-if-present '.nobackup' \
--exclude '/home/*/Downloads/' \
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
# DELETES ARCHIVES NOT FITTING KEEP-RULES
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
borg prune -v --list $REPOSITORY \
--keep-daily=7 \
--keep-weekly=4 \
--keep-monthly=6 \
--keep-yearly=0 \
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
echo "$NOW Done" >> $LOGFILE
echo '------------------------------' >> $LOGFILE
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
# --- USEFULL SHIT ---
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
# setup above ignores directories containing '.nobackup' file
# make '.nobackup' imutable using chattr to prevent accidental removal
# touch .nobackup
# chattr +i .nobackup
2020-04-09 22:52:11 +00:00
2020-04-25 22:57:10 +00:00
# in the repo folder, to list available backups:
# borg list .
# to mount one of them:
# borg mount .::1584472836 ~/temp
# to umount:
# borg umount ~/temp
# to delete single backup in a repo:
# borg delete .::1584472836
```
2020-04-09 22:52:11 +00:00
2020-05-09 15:13:49 +00:00
The script must be **executabe** - `chmod +x borg_backup.sh`
2020-05-10 01:26:52 +00:00
### Manual run
`sudo ./borg_backup.sh`
2020-05-09 15:13:49 +00:00
It could ask about
*Attempting to access a previously unknown unencrypted repository*</br>
2020-05-21 11:34:42 +00:00
Answer yes.</br>
If we would initialize the repo with sudo then it would be no issue,
but then non root user would not be able to enter the repo directory.
2020-04-09 22:52:11 +00:00
2020-05-10 01:26:52 +00:00
### Automatic execution
2022-04-03 08:50:12 +00:00
Previously cron has been used, but decided to try systemd timers.
2020-05-10 01:26:52 +00:00
2022-04-03 08:50:12 +00:00
[This](https://blog.andrewkeech.com/posts/170719_borg.html) is the source.
2020-04-25 22:57:10 +00:00
2020-05-09 15:13:49 +00:00
2020-05-10 01:32:48 +00:00
2020-05-09 15:13:49 +00:00
# Accessing the backup files
2020-05-10 01:26:52 +00:00
* go in to the borg repo</br>
`cd /home/bastard/borg/docker_backup/`
* list the archives</br>
`sudo borg list .`
2020-05-09 15:13:49 +00:00
* choose one by the date, copy its identifier which is epoch time, e.g. 1588986941
2020-05-10 01:26:52 +00:00
* mount it to some folder</br>
`sudo borg mount .::1588986941 /mnt/temp`
2020-05-21 11:34:42 +00:00
* browse the directory where the archive is mounted and do whatever is needed
2020-05-10 01:26:52 +00:00
* umount the backup</br>
`sudo borg umount /mnt/temp`
2020-04-25 09:09:46 +00:00
2020-05-07 00:02:19 +00:00
# Extra info
Test your backups, test your recovery procedure.
2020-04-25 09:09:46 +00:00
# Remote backup
2020-04-09 22:52:11 +00:00
2020-04-25 09:09:46 +00:00
Backing up borg repo to a network share or cloud using rclone
2020-04-09 22:52:11 +00:00
2020-04-25 09:09:46 +00:00
*To be continued*