Backup Retention Policy
- Daily Backups: Keep 14 snapshots.
- Weekly Backups: Keep 2 snapshots.
- Backup from a remote system via SSH.
Key Backup Elements
- Database Dumps (MariaDB, PostgreSQL & OpenLDAP)
- Docker Volumes & Data Directories
- Configuration Files
- Ensuring Consistency (Before backup, ensure DB is dumped and volumes are accessible)
1. Create a Backup Directory on the Remote System
The backup server should have a dedicated directory:
mkdir -p /backup/pet/daily
mkdir -p /backup/pet/weekly
2. Install and Configure rsnapshot
On the backup server, install rsnapshot:
sudo apt install rsnapshot
Edit the configuration file /etc/rsnapshot.conf:
# Snapshot levels
retain daily 14
retain weekly 2
# Destination for backups
snapshot_root /backup/pet/
# Remote backup via SSH
rsync_short_args -az
rsync_long_args --delete --numeric-ids --relative
ssh_args -p 22
# Directories to backup
backup root@your-server:/home/ /pet/home/
backup root@your-server:/etc/ /pet/etc/
backup root@your-server:/var/lib/docker/volumes/ /pet/docker-volumes/
backup root@your-server:/opt/ /pet/opt/
backup root@your-server:/srv/ /pet/srv/
# Database dumps (Handled separately, stored in /var/backups)
backup root@your-server:/var/backups/ /pet/backups/
3. Ensure Database Dumps Before Backup
On your Docker host, create a script /root/db-backup.sh to dump MariaDB, PostgreSQL, and OpenLDAP before rsnapshot runs.
#!/bin/bash
BACKUP_DIR="/var/backups"
# MariaDB Dump
docker exec mariadb sh -c 'mysqldump -u root -p"$MYSQL_ROOT_PASSWORD" --all-databases' > "$BACKUP_DIR/mariadb.sql"
# PostgreSQL Dump
docker exec postgres pg_dumpall -U postgres > "$BACKUP_DIR/postgres.sql"
# OpenLDAP Backup
docker exec openldap slapcat -n 0 > "$BACKUP_DIR/openldap.ldif"
# Ensure correct permissions
chmod 600 "$BACKUP_DIR/mariadb.sql" "$BACKUP_DIR/postgres.sql" "$BACKUP_DIR/openldap.ldif"
Make it executable:
chmod +x /root/db-backup.sh
4. Automate Backups
On your Docker host, set up a cron job (crontab -e):
0 2 * * * /root/db-backup.sh
- Runs at 2 AM daily.
On your backup server, automate rsnapshot (crontab -e):
30 3 * * * /usr/bin/rsnapshot daily
0 4 * * 0 /usr/bin/rsnapshot weekly
- Runs daily at 3:30 AM and weekly on Sundays at 4 AM.
5. Verify and Test
- Run a manual test backup:
rsnapshot daily - Check snapshot contents:
ls -lah /backup/pet/daily/ - Restore a file (example for OpenLDAP LDIF dump):
cp /backup/pet/daily.0/pet/backups/openldap.ldif /root/
Restoring Data
1. Restore MariaDB
docker exec -i mariadb sh -c 'mysql -u root -p"$MYSQL_ROOT_PASSWORD"' < /root/mariadb.sql
2. Restore PostgreSQL
docker exec -i postgres psql -U postgres < /root/postgres.sql
3. Restore OpenLDAP
docker exec openldap ldapadd -x -D "cn=admin,dc=apps,dc=pet" -w "$LDAP_ADMIN_PASSWORD" -f /root/openldap.ldif