add restoration possiblity
continuous-integration/drone/tag Build is passing

This commit is contained in:
victor.kolomin committed 2025-10-02 16:18:32 +02:00
1 parent 8e9b639ee5
commit e242e36fe4
4 files changed
+135 -20

No files matched your search

+2 -1
View File
@@ -32,7 +32,8 @@ RUN curl -o /usr/local/bin/mc https://dl.min.io/client/mc/release/linux-amd64/mc
COPY --from=builder /usr/local/cargo/bin/rustic /usr/local/bin/rustic
COPY backup.sh /usr/local/bin/backup.sh
COPY restore.sh /usr/local/bin/restore.sh
COPY start.sh /start.sh
RUN chmod +x /usr/local/bin/backup.sh /start.sh
RUN chmod +x /usr/local/bin/backup.sh /usr/local/bin/restore.sh /start.sh
CMD ["/start.sh"]
+39 -10
View File
@@ -1,7 +1,7 @@
services:
rustic_daemon:
# IMPORTANT: Replace with the name of your custom built Rustic image
image: your_registry/rustic-backup:latest
image: git.kolspace.cc/victor.kolomin/docker-rustic-backup:latest
# Use the Ansible variable for hostname
hostname: "{{ inventory_hostname }}"
container_name: rustic_backup_daemon
@@ -10,9 +10,13 @@ services:
restart: "unless-stopped"
environment:
# --- 1. CRON SCHEDULING ---
# --- 0. CONTAINER ROLE ---
# "backup": Runs the cron daemon for scheduled backups (default)
# "restore": Keeps the container alive to perform manual restores
- CONTAINER_ROLE="backup"
# --- 1. CRON SCHEDULING (for backup role) ---
# Defines WHEN the entire backup script (backup, prune, check) will run.
# Example: "0 0,12 * * *" runs at 00:00 and 12:00 daily
- CRON_SCHEDULE="0 */12 * * *"
# --- 2. RUSTIC CORE SETTINGS ---
@@ -27,32 +31,57 @@ services:
- AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY"
- AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
# --- 4. EXECUTOR SETTINGS (Used by backup.sh) ---
# --- 4. EXECUTOR SETTINGS (for backup role) ---
# Optional: Max random delay in seconds to wait before starting the backup.
# This helps distribute the load if many containers start at the same time.
# Example: 3600 will make the script wait 0-59 minutes before running.
- RANDOM_DELAY_SECONDS=3600
# Paths to backup (space-separated list). Must match the volume mount source.
- BACKUP_PATHS="/data/volumes/"
# Pruning policy arguments
- PRUNE_POLICY="--keep-daily 7 --keep-weekly 5 --keep-monthly 12"
- PRUNE_POLICY="--keep-last 3 --keep-daily 7 --keep-weekly 4 --keep-monthly 6"
# Check arguments
- CHECK_ARGS="--read-data-subset 10%"
- CHECK_ARGS="--read-data --read-data-subset 10%"
# --- 5. MOUNTED DATA ---
volumes:
# Mount the Docker data path from the host (read-only for safety)
# /opt/docker (Host) -> /data/volumes/ (Container)
- /opt/docker:/data/volumes/:ro
# Persistent Volume for the Rustic Cache (crucial for performance)
- rustic_cache:/root/.cache/rustic
# Include the .env file if it contains S3 secrets or other shared variables
# Note: Variables in this file will be overridden by the 'environment' section above.
env_file:
- .env
rustic_restore:
# This is a dedicated service for running the container in restore mode.
image: git.kolspace.cc/victor.kolomin/docker-rustic-backup:latest
hostname: rustic-restore-shell
container_name: rustic_restore_shell
# No restart policy, as this is a manual, one-off task.
environment:
# --- 0. CONTAINER ROLE ---
- CONTAINER_ROLE="restore"
# --- RUSTIC & S3 SETTINGS (must match the backup container) ---
- RUSTIC_PASSWORD="YOUR_SUPER_SECRET_REPOSITORY_PASSWORD_HERE"
- S3_ENDPOINT="http://192.168.110.135:9000"
- S3_BUCKET="mybucket"
- S3_REGION="us-east-1"
- AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY"
- AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
volumes:
# Mount a local directory on the host to receive the restored files.
- ./restore_output:/restore
# Mount the same cache volume for performance
- rustic_cache:/root/.cache/rustic
env_file:
- .env
+61
View File
@@ -0,0 +1,61 @@
#!/bin/bash
# restore.sh: Interactive script to restore a snapshot.
# Exit immediately if a command exits with a non-zero status.
set -e
echo "--- Rustic Interactive Restore ---"
# --- 1. Filter by Host ---
read -p "Enter hostname to filter snapshots (or leave blank for all): " HOST_FILTER
echo "Fetching available snapshots..."
# --- 2. Display Snapshots ---
if [ -n "$HOST_FILTER" ]; then
echo "Displaying snapshots for host: ${HOST_FILTER}"
rustic snapshots --host "${HOST_FILTER}"
else
echo "Displaying all snapshots..."
rustic snapshots
fi
echo "----------------------------------"
# --- 3. Select Snapshot ---
read -p "Please enter the full ID of the snapshot you want to restore: " SNAPSHOT_ID
# Exit if the user didn't provide an ID
if [ -z "$SNAPSHOT_ID" ]; then
echo "No snapshot ID entered. Aborting."
exit 1
fi
# --- 4. Select Destination ---
read -p "Enter the absolute path inside the container to restore to [default: /restore]: " RESTORE_PATH
# Use default value if user input is empty
RESTORE_PATH=${RESTORE_PATH:-/restore}
echo "--- Summary ---"
echo "Snapshot to restore: ${SNAPSHOT_ID}"
echo "Restore destination: ${RESTORE_PATH}"
echo "-----------------"
# --- 5. Confirm and Restore ---
read -p "Are you sure you want to proceed? (y/N): " CONFIRMATION
if [[ ! "$CONFIRMATION" =~ ^[yY](es)?$ ]]; then
echo "Restore cancelled."
exit 0
fi
# Create the target directory if it doesn't exist
mkdir -p "${RESTORE_PATH}"
echo "Starting restore process..."
# Execute the restore command
rustic restore "${SNAPSHOT_ID}" "${RESTORE_PATH}"
echo "----------------------------------"
echo "✅ Restore completed successfully!"
echo "Your files are available in the container at: ${RESTORE_PATH}"
echo "Check the volume mounts on your host to access them."
+33 -9
View File
@@ -4,6 +4,10 @@
# Exit immediately if any command fails
set -e
# --- 0. Determine Container Role ---
# Default to "backup" role if not specified
CONTAINER_ROLE=${CONTAINER_ROLE:-"backup"}
# --- 1. Validate Core Variables ---
if [ -z "$CRON_SCHEDULE" ]; then
echo "ERROR: CRON_SCHEDULE environment variable is not set."
@@ -59,13 +63,33 @@ else
fi
# --- 3. Create Dynamic Crontab ---
CRONTAB_FILE="/var/spool/cron/crontabs/root"
echo "Creating crontab entry: ${CRON_SCHEDULE} /usr/local/bin/backup.sh"
echo "${CRON_SCHEDULE} /usr/local/bin/backup.sh" > ${CRONTAB_FILE}
chmod 600 ${CRONTAB_FILE}
# --- 3. Role-based Execution ---
if [ "$CONTAINER_ROLE" = "backup" ]; then
echo "--- Starting in Backup Mode ---"
if [ -z "$CRON_SCHEDULE" ]; then
echo "ERROR: CRON_SCHEDULE environment variable is not set for backup mode."
exit 1
fi
# Create Dynamic Crontab
CRONTAB_FILE="/var/spool/cron/crontabs/root"
echo "Creating crontab entry: ${CRON_SCHEDULE} /usr/local/bin/backup.sh"
echo "${CRON_SCHEDULE} /usr/local/bin/backup.sh" > ${CRONTAB_FILE}
chmod 600 ${CRONTAB_FILE}
# --- 4. Start Daemon ---
echo "Starting cron daemon in foreground..."
exec /usr/sbin/cron -f -L 8
echo "Cron daemon is running. Container is now in daemon mode."
# Start Cron Daemon
echo "Starting cron daemon in foreground..."
exec /usr/sbin/cron -f -L 8
elif [ "$CONTAINER_ROLE" = "restore" ]; then
echo "--- Starting in Restore Mode ---"
echo "The container is now running and ready for restore operations."
echo "To restore a backup, run the following command:"
echo " docker exec -it ${HOSTNAME} /usr/local/bin/restore.sh"
echo "--------------------------------------------------------"
# Keep container alive
exec tail -f /dev/null
else
echo "ERROR: Invalid CONTAINER_ROLE specified: '${CONTAINER_ROLE}'. Use 'backup' or 'restore'."
exit 1
fi