From ff4a9a14e75462b0b61204934527c2384f9c777f Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Thu, 17 Apr 2025 12:24:00 -0400 Subject: [PATCH] feat: Cleanup and updates readme. --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++------ docker-backup.sh | 18 +++++++---------- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index db6c150..f57e66b 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,52 @@ # backup -A script template to backup docker volumes on the servers. +A script to backup docker volumes and stack configuration on the servers. -## Usage +## What it does + +1. Stops all docker services located in `/etc/komodo/stacks` +1. Generates a temporary directory. +1. Copies all stack configuration to temporary directory. +1. Copies all docker volumes `/var/lib/docker/volumes` to temporary directory. +1. Copies all data in `/opt` (where host bind mounts should be) to temporary directory. +1. Compresses the temporary directory. +1. Copies the result to the `/backups` folder. +1. Cleans up / removes temporary directory that was created. +1. Restarts all the services. + +## Setup + +Before using the backup script, you must mount a network share for backups to be +stored in. This is done by editing the `/etc/fstab` file, and adding the +following line. + +```bash +///docker/backups/ /backups cifs credentials=/home/michael/smbcredentials,uid=0,gid=0 0 0 +``` + +Once that is done: + +1. Generate the `/backups` directory `sudo mkdir /backups` +1. Reload the daemon `sudo systemctl daemon-reload` +1. Mount the folder `sudo mount -a` + +### Systemd Setup + +Setup the systemd service and timer, so that backups are ran once a day @ 3 a.m. + +```bash +cd /etc/systemd/system +sudo wget https://git.housh.dev/homelab/backup/raw/branch/main/backup.service +sudo wget https://git.housh.dev/homelab/backup/raw/branch/main/backup.timer +sudo systemctl daemon-reload +sudo systemctl start backup.timer +sudo systemctl start backup.service + +``` + +### Setup the backup script 1. Download the script `wget https://git.housh.dev/homelab/backup/raw/branch/main/docker-backup.sh` -1. Edit the script to include services that need to be shut down, prior to - backup. 1. Add any extra volumes to backup. 1. Make script executable `chmod +x docker-backup.sh` -1. Run the script `sudo ./docker-backup.sh` - +1. Manually run the script `sudo ./docker-backup.sh` diff --git a/docker-backup.sh b/docker-backup.sh index ee6ed35..0a8c09e 100644 --- a/docker-backup.sh +++ b/docker-backup.sh @@ -1,18 +1,14 @@ #!/bin/bash today=$(date +'%F') -volumesdir="/var/lib/docker/volumes" stackdir="/etc/komodo/stacks" backupdir="/backups" - -# Add services here to stop and backup. -services=( - "$stackdir/caddy_housh_dev" -) +services=($(find "$stackdir" -maxdepth 1 -mindepth 1 -type d)) # Add volumes here to backup. volumes=( - "$volumesdir" + "/var/lib/docker/volumes" + "/opt" ) function stopServices() { @@ -49,11 +45,11 @@ function copyVolumes() { function backup() { echo "Creating backup..." - tar -cvf "/tmp/$today.tar" "$1" - gzip "/tmp/$today.tar" - cp -R "/tmp/$today.tar.gz" "$backupdir" + tar -cvf "/$today.tar" "$1" + gzip "/$today.tar" + cp -R "/$today.tar.gz" "$backupdir" # Cleanup temporary files. - rm -rf "$temp" "/tmp/$today.tar" "/tmp/$today.tar.gz" + rm -rf "$1" "/$today.tar" "/$today.tar.gz" } function main() {