You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.2 KiB
Markdown

4 years ago
# BorgBackup in docker
###### guide by example
4 years ago
![logo](https://i.imgur.com/dR50bkP.png)
4 years ago
4 years ago
# Purpose & Overview
4 years ago
Backups.
4 years ago
* [Official site](https://www.borgbackup.org/)
* [Github](https://github.com/borgbackup/borg)
4 years ago
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>
4 years ago
A script is created that backs up the entire docker directory and /etc locally.</br>
Cronjob is set to execute this script daily.
4 years ago
4 years ago
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
4 years ago
4 years ago
```
4 years ago
/home/
└── ~/
├── borg/
│ ├── docker_backup/
│ ├── borg_backup.sh
│ └── borg_backup.log
4 years ago
4 years ago
└── docker/
4 years ago
├── container-setup #1
├── container-setup #2
4 years ago
├── ...
4 years ago
```
4 years ago
4 years ago
* `docker_backup/` - borg repository directory containg the backups
4 years ago
* `borg_backup.sh` - the backup script that adds new archive in to the repository
4 years ago
* `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.
4 years ago
4 years ago
# The setup
4 years ago
4 years ago
#### Install BorgBackup
4 years ago
4 years ago
Borg is likely in your linux repositories.
4 years ago
4 years ago
#### Create a new borg repo
4 years ago
4 years ago
`mkdir ~/borg`</br>
4 years ago
`sudo borg init --encryption=none ~/borg/docker_backup`
Note the sudo. Borg commands should be run as root, so it can access everything.
4 years ago
4 years ago
#### The backup script
4 years ago
4 years ago
`borg_backup.sh`
```bash
#!/bin/bash
4 years ago
4 years ago
# INITIALIZE THE REPO WITH THE COMMAND:
# borg init --encryption=none ~/borg/my_backup
# THEN RUN THIS SCRIPT
4 years ago
4 years ago
# -----------------------------------------------
4 years ago
4 years ago
BACKUP_THIS='/home/bastard/docker /etc'
REPOSITORY='/home/bastard/borg/docker_backup'
LOGFILE='/home/bastard/borg/borg_backup.log'
4 years ago
4 years ago
# -----------------------------------------------
4 years ago
4 years ago
NOW=$(date +"%Y-%m-%d | %H:%M | ")
echo "$NOW Starting Backup and Prune" >> $LOGFILE
4 years ago
4 years ago
# CREATES NEW ARCHIVE IN PRESET REPOSITORY
4 years ago
4 years ago
borg create \
$REPOSITORY::'{now:%s}' \
$BACKUP_THIS \
\
--compression zstd \
--one-file-system \
--exclude-caches \
--exclude-if-present '.nobackup' \
--exclude '/home/*/Downloads/' \
4 years ago
4 years ago
# DELETES ARCHIVES NOT FITTING KEEP-RULES
4 years ago
4 years ago
borg prune -v --list $REPOSITORY \
--keep-daily=7 \
--keep-weekly=4 \
--keep-monthly=6 \
--keep-yearly=0 \
4 years ago
4 years ago
echo "$NOW Done" >> $LOGFILE
echo '------------------------------' >> $LOGFILE
4 years ago
4 years ago
# --- USEFULL SHIT ---
4 years ago
4 years ago
# setup above ignores directories containing '.nobackup' file
# make '.nobackup' imutable using chattr to prevent accidental removal
# touch .nobackup
# chattr +i .nobackup
4 years ago
4 years ago
# 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
```
4 years ago
4 years ago
The script must be **executabe** - `chmod +x borg_backup.sh`
4 years ago
### Manual run
`sudo ./borg_backup.sh`
4 years ago
It could ask about
*Attempting to access a previously unknown unencrypted repository*</br>
Answer yes, this could be important as the automatic backup would stop at
this question otherwise.
4 years ago
4 years ago
### Automatic execution
Using [cron](https://wiki.archlinux.org/index.php/cron).
**Make sure cron is installed and the service is running**</br>
`sudo systemctl status cronie`
4 years ago
4 years ago
Create a cron job that executes the script
[at 03:00](https://crontab.guru/#0_03_*_*_*)
4 years ago
4 years ago
* switch to root</br>
`su`
* add new cron job</br>
`crontab -e`</br>
`0 3 * * * /home/bastard/docker/nextcloud/nextcloud-backup-script.sh`
4 years ago
`crontab -l` - list current cronjobs</br>
`journalctl | grep cron` - cron history
4 years ago
# Accessing the backup files
4 years ago
* go in to the borg repo</br>
`cd /home/bastard/borg/docker_backup/`
* list the archives</br>
`sudo borg list .`
4 years ago
* choose one by the date, copy its identifier which is epoch time, e.g. 1588986941
4 years ago
* mount it to some folder</br>
`sudo borg mount .::1588986941 /mnt/temp`
4 years ago
* browse the directory where mounted and do whatever is needed
4 years ago
* umount the backup</br>
`sudo borg umount /mnt/temp`
4 years ago
4 years ago
# Extra info
Test your backups, test your recovery procedure.
4 years ago
# Remote backup
4 years ago
4 years ago
Backing up borg repo to a network share or cloud using rclone
4 years ago
4 years ago
*To be continued*