mirror of
https://github.com/wickedyoda/public-setupfiles.git
synced 2026-11-16 13:31:56 -06:00
Relocate Docker backup scripts and schedule 3-hour NAS backups
This commit is contained in:
1 parent
160cabc734
commit
1b5c15948c
5 files changed
+66
-54
No files matched your search
@@ -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
|
||||
@@ -1,32 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
SRC_SCRIPT="${SCRIPT_DIR}/docker-backup.sh"
|
||||
DEST_DIR="/root/docker"
|
||||
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}"
|
||||
echo "Installed ${DEST_SCRIPT}"
|
||||
|
||||
cat > "${CRON_JOB_FILE}" <<EOF
|
||||
SHELL=/bin/bash
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
||||
|
||||
# Run every 12 hours
|
||||
0 */12 * * * 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."
|
||||
@@ -1,129 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# =========================
|
||||
# Docker Backup Script
|
||||
# =========================
|
||||
|
||||
# Backup destination
|
||||
HOSTNAME="$(hostname -s)"
|
||||
BACKUP_BASE="/mnt/naspublic/docker-backups/${HOSTNAME}"
|
||||
|
||||
# What to back up
|
||||
SOURCE_PATHS=(
|
||||
"/root/docker"
|
||||
"/home/traver/docker"
|
||||
"/var/lib/docker/volumes"
|
||||
)
|
||||
|
||||
# Retention in days
|
||||
RETENTION_DAYS=7
|
||||
|
||||
# Log file
|
||||
LOG_FILE="/var/log/docker_backup.log"
|
||||
|
||||
# Stop containers during backup? true/false
|
||||
STOP_CONTAINERS="false"
|
||||
|
||||
# Docker compose search roots for optional stop/start
|
||||
COMPOSE_DIRS=(
|
||||
"/root/docker"
|
||||
"/home/traver/docker"
|
||||
)
|
||||
|
||||
# Date/time foldering
|
||||
DATE_STAMP="$(date '+%Y-%m-%d')"
|
||||
TIME_STAMP="$(date '+%H-%M-%S')"
|
||||
BACKUP_DIR="${BACKUP_BASE}/${DATE_STAMP}"
|
||||
|
||||
log() {
|
||||
echo "[$(date '+%F %T')] $*" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
archive_name_for_path() {
|
||||
local path="$1"
|
||||
echo "${path#/}" | tr '/' '_'
|
||||
}
|
||||
|
||||
start_containers() {
|
||||
log "Starting Docker Compose stacks..."
|
||||
for dir in "${COMPOSE_DIRS[@]}"; do
|
||||
if [[ -d "$dir" ]]; then
|
||||
while IFS= read -r -d '' compose_file; do
|
||||
compose_path="$(dirname "$compose_file")"
|
||||
log "Starting stack in $compose_path"
|
||||
docker compose -f "$compose_file" up -d >>"$LOG_FILE" 2>&1 || log "Warning: failed to start stack in $compose_path"
|
||||
done < <(find "$dir" -type f \( -name "docker-compose.yml" -o -name "compose.yml" -o -name "docker-compose.yaml" -o -name "compose.yaml" \) -print0)
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
stop_containers() {
|
||||
log "Stopping Docker Compose stacks..."
|
||||
for dir in "${COMPOSE_DIRS[@]}"; do
|
||||
if [[ -d "$dir" ]]; then
|
||||
while IFS= read -r -d '' compose_file; do
|
||||
compose_path="$(dirname "$compose_file")"
|
||||
log "Stopping stack in $compose_path"
|
||||
docker compose -f "$compose_file" down >>"$LOG_FILE" 2>&1 || log "Warning: failed to stop stack in $compose_path"
|
||||
done < <(find "$dir" -type f \( -name "docker-compose.yml" -o -name "compose.yml" -o -name "docker-compose.yaml" -o -name "compose.yaml" \) -print0)
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
create_archive() {
|
||||
local path="$1"
|
||||
local archive_name
|
||||
local archive_path
|
||||
local parent_dir
|
||||
local base_name
|
||||
|
||||
if [[ ! -e "$path" ]]; then
|
||||
log "Warning: source path missing: $path"
|
||||
return 0
|
||||
fi
|
||||
|
||||
archive_name="$(archive_name_for_path "$path")"
|
||||
archive_path="${BACKUP_DIR}/${TIME_STAMP}_${archive_name}.tar.gz"
|
||||
parent_dir="$(dirname "$path")"
|
||||
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
|
||||
}
|
||||
|
||||
main() {
|
||||
log "========================================"
|
||||
log "Starting Docker backup"
|
||||
|
||||
mkdir -p "$BACKUP_BASE"
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
|
||||
if [[ "$STOP_CONTAINERS" == "true" ]]; then
|
||||
stop_containers
|
||||
fi
|
||||
|
||||
log "Creating separate compressed backups in $BACKUP_DIR"
|
||||
for path in "${SOURCE_PATHS[@]}"; do
|
||||
create_archive "$path"
|
||||
done
|
||||
|
||||
log "Backup sizes:"
|
||||
du -sh "$BACKUP_DIR" >>"$LOG_FILE" 2>&1 || true
|
||||
|
||||
if [[ "$STOP_CONTAINERS" == "true" ]]; then
|
||||
start_containers
|
||||
fi
|
||||
|
||||
cleanup_old_backups
|
||||
|
||||
log "Docker backup completed successfully"
|
||||
log "========================================"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
@@ -1,27 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# --- CONFIG ---
|
||||
SRC_SCRIPT="${SCRIPT_DIR}/docker-backup.sh"
|
||||
DEST_DIR="/root/docker"
|
||||
DEST_SCRIPT="${DEST_DIR}/docker-backup.sh"
|
||||
CRON_JOB_FILE="/etc/cron.d/docker-backup"
|
||||
LOG_FILE="/var/log/docker_backup.log"
|
||||
|
||||
mkdir -p "${DEST_DIR}"
|
||||
|
||||
install -m 755 "${SRC_SCRIPT}" "${DEST_SCRIPT}"
|
||||
echo "Installed ${DEST_SCRIPT}"
|
||||
|
||||
cat > "${CRON_JOB_FILE}" <<EOF
|
||||
SHELL=/bin/bash
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
||||
|
||||
# Run every 12 hours
|
||||
0 */12 * * * root ${DEST_SCRIPT} >> ${LOG_FILE} 2>&1
|
||||
EOF
|
||||
|
||||
chmod 644 "${CRON_JOB_FILE}"
|
||||
echo "Installed ${CRON_JOB_FILE}"
|
||||
Reference in new issue
Block a user