diff --git a/Docker_backup/README.md b/Docker_backup/README.md new file mode 100644 index 0000000..49275c7 --- /dev/null +++ b/Docker_backup/README.md @@ -0,0 +1,19 @@ +# Docker_backup + +## Overview +This directory contains the Docker backup scripts for this repository. + +## Files +- `docker-backup.sh` backs up Docker data to the NAS share. +- `setup_docker-backup.sh` installs the backup script and cron job on a new system. +- `correct_docker-backup.sh` repairs older installs and rewrites the cron job. + +## Usage +- Review the scripts before running them. +- Run the install or correction scripts as `root`. +- Adjust paths if the target system uses different Docker or NAS mount locations. + +## Notes +- Backups are written to `/mnt/naspublic/docker-backup/{hostname}/{month}/{date}/`. +- The backup job runs every 3 hours. +- Backups older than 14 days are removed automatically. diff --git a/batch_files/docker-backup_files/correct_docker-backup.sh b/Docker_backup/correct_docker-backup.sh similarity index 84% rename from batch_files/docker-backup_files/correct_docker-backup.sh rename to Docker_backup/correct_docker-backup.sh index 030160f..5fabafd 100755 --- a/batch_files/docker-backup_files/correct_docker-backup.sh +++ b/Docker_backup/correct_docker-backup.sh @@ -23,10 +23,10 @@ cat > "${CRON_JOB_FILE}" <> ${LOG_FILE} 2>&1 +# Run every 3 hours +0 */3 * * * root ${DEST_SCRIPT} >> ${LOG_FILE} 2>&1 EOF chmod 644 "${CRON_JOB_FILE}" echo "Installed ${CRON_JOB_FILE}" -echo "Docker backups will run every 12 hours." +echo "Docker backups will run every 3 hours." diff --git a/batch_files/docker-backup_files/docker-backup.sh b/Docker_backup/docker-backup.sh old mode 100644 new mode 100755 similarity index 71% rename from batch_files/docker-backup_files/docker-backup.sh rename to Docker_backup/docker-backup.sh index a19cb8f..367bbfb --- a/batch_files/docker-backup_files/docker-backup.sh +++ b/Docker_backup/docker-backup.sh @@ -6,18 +6,19 @@ set -euo pipefail # ========================= # Backup destination +NAS_ROOT="/mnt/naspublic" HOSTNAME="$(hostname -s)" -BACKUP_BASE="/mnt/naspublic/docker-backups/${HOSTNAME}" +BACKUP_BASE="${NAS_ROOT}/docker-backup/${HOSTNAME}" # What to back up SOURCE_PATHS=( "/root/docker" - "/home/traver/docker" "/var/lib/docker/volumes" + "/home/traver/docker" ) # Retention in days -RETENTION_DAYS=7 +RETENTION_DAYS=14 # Log file LOG_FILE="/var/log/docker_backup.log" @@ -32,9 +33,10 @@ COMPOSE_DIRS=( ) # Date/time foldering +MONTH_STAMP="$(date '+%Y-%m')" DATE_STAMP="$(date '+%Y-%m-%d')" TIME_STAMP="$(date '+%H-%M-%S')" -BACKUP_DIR="${BACKUP_BASE}/${DATE_STAMP}" +BACKUP_DIR="${BACKUP_BASE}/${MONTH_STAMP}/${DATE_STAMP}" log() { echo "[$(date '+%F %T')] $*" | tee -a "$LOG_FILE" @@ -45,6 +47,29 @@ archive_name_for_path() { echo "${path#/}" | tr '/' '_' } +require_backup_target() { + if command -v mountpoint >/dev/null 2>&1; then + mountpoint -q "$NAS_ROOT" || { + log "Error: ${NAS_ROOT} is not mounted. Aborting backup." + exit 1 + } + return + fi + + if command -v findmnt >/dev/null 2>&1; then + findmnt -rn "$NAS_ROOT" >/dev/null 2>&1 || { + log "Error: ${NAS_ROOT} is not mounted. Aborting backup." + exit 1 + } + return + fi + + if [[ ! -d "$NAS_ROOT" ]]; then + log "Error: ${NAS_ROOT} is not mounted. Aborting backup." + exit 1 + fi +} + start_containers() { log "Starting Docker Compose stacks..." for dir in "${COMPOSE_DIRS[@]}"; do @@ -73,7 +98,8 @@ stop_containers() { cleanup_old_backups() { log "Removing backups older than ${RETENTION_DAYS} days from ${BACKUP_BASE}" - find "$BACKUP_BASE" -mindepth 1 -maxdepth 1 -type d -mtime +"$RETENTION_DAYS" -exec rm -rf {} \; >>"$LOG_FILE" 2>&1 || true + find "$BACKUP_BASE" -mindepth 2 -maxdepth 2 -type d -mtime +"$RETENTION_DAYS" -exec rm -rf {} + >>"$LOG_FILE" 2>&1 || true + find "$BACKUP_BASE" -mindepth 1 -maxdepth 1 -type d -empty -delete >>"$LOG_FILE" 2>&1 || true } create_archive() { @@ -94,13 +120,18 @@ create_archive() { base_name="$(basename "$path")" log "Creating archive ${archive_path} from ${path}" - tar --xattrs --acls --numeric-owner -czpf "$archive_path" -C "$parent_dir" "$base_name" >>"$LOG_FILE" 2>&1 + if command -v pigz >/dev/null 2>&1; then + tar --xattrs --acls --numeric-owner -cpf - -C "$parent_dir" "$base_name" 2>>"$LOG_FILE" | pigz 2>>"$LOG_FILE" > "$archive_path" + else + tar --xattrs --acls --numeric-owner -czpf "$archive_path" -C "$parent_dir" "$base_name" >>"$LOG_FILE" 2>&1 + fi } main() { log "========================================" log "Starting Docker backup" + require_backup_target mkdir -p "$BACKUP_BASE" mkdir -p "$BACKUP_DIR" diff --git a/batch_files/docker-backup_files/setup_docker-backup.sh b/Docker_backup/setup_docker-backup.sh similarity index 79% rename from batch_files/docker-backup_files/setup_docker-backup.sh rename to Docker_backup/setup_docker-backup.sh index 99ef7dd..b7ac711 100755 --- a/batch_files/docker-backup_files/setup_docker-backup.sh +++ b/Docker_backup/setup_docker-backup.sh @@ -10,6 +10,11 @@ DEST_SCRIPT="${DEST_DIR}/docker-backup.sh" CRON_JOB_FILE="/etc/cron.d/docker-backup" LOG_FILE="/var/log/docker_backup.log" +if [[ "${EUID}" -ne 0 ]]; then + echo "Run this script as root." + exit 1 +fi + mkdir -p "${DEST_DIR}" install -m 755 "${SRC_SCRIPT}" "${DEST_SCRIPT}" @@ -19,8 +24,8 @@ cat > "${CRON_JOB_FILE}" <> ${LOG_FILE} 2>&1 +# Run every 3 hours +0 */3 * * * root ${DEST_SCRIPT} >> ${LOG_FILE} 2>&1 EOF chmod 644 "${CRON_JOB_FILE}" diff --git a/batch_files/docker-backup_files/README.md b/batch_files/docker-backup_files/README.md deleted file mode 100644 index 7b98617..0000000 --- a/batch_files/docker-backup_files/README.md +++ /dev/null @@ -1,43 +0,0 @@ -<<<<<<< HEAD - # docker-backup_files - - ## Purpose - This directory contains files related to **docker backup files**. - - ## Contents - - `docker-backup.sh` -- `setup_docker-backup.sh` - - ## Usage - - Review each script or configuration file before running - - Some scripts may require **root or sudo** - - Paths and variables may need adjustment for your environment - - ## Notes - - This folder is part of the **public-setupfiles** repository - - Files are provided as-is for reference or automation - - ## Safety - Always back up data before running scripts that modify system state. -======= -# docker-backup_files - -## Overview -This directory is part of the **public-setupfiles** repository. -It contains scripts, configuration files, or resources related to **docker-backup_files**. - -## Usage -- Review scripts before execution -- Some files may require **root or sudo privileges** -- Paths, variables, or credentials may need customization - -## Logging & Output -Scripts may generate logs or output files in the same directory or system locations. - -## Safety Notes -- Always back up important data before running scripts -- Test in a non-production environment when possible -- Use at your own risk - ---- ->>>>>>> main