Add various scripts for MinIO and Restic backup management
This commit is contained in:
127
scripts/restic-backup-new.sh
Executable file
127
scripts/restic-backup-new.sh
Executable file
@@ -0,0 +1,127 @@
|
||||
#!/bin/bash
|
||||
|
||||
# --- CONFIGURATION ---
|
||||
# The bucket quotas check requires the 'mc' (MinIO client) command.
|
||||
# Download it from https://min.io/docs/minio/linux/reference/minio-client/mc.html
|
||||
# and make sure it's in your PATH.
|
||||
|
||||
# MinIO Server Details
|
||||
MINIO_ALIAS="minio-k0s"
|
||||
MINIO_URL="http://192.168.153.245:9000"
|
||||
MINIO_ACCESS_KEY="Tg4UTucrnhJ8FPaPsqra"
|
||||
MINIO_SECRET_KEY="mGEJ6CSYWfSYdaQ90yAY2hfvNtMXV6at9T34o3Kc"
|
||||
|
||||
# Restic Backup Details
|
||||
RESTIC_REPO="s3:http://192.168.153.245:9000/backup"
|
||||
BACKUP_SOURCE="/home/$(whoami)"
|
||||
RESTIC_EXCLUDE_FILE="~/.resticignore" # optional, create this file to list folders/files to exclude
|
||||
|
||||
# Restic Environment Variables (for security, use these instead of passing keys directly in the command)
|
||||
export RESTIC_PASSWORD="frack666"
|
||||
export AWS_ACCESS_KEY_ID="${MINIO_ACCESS_KEY}"
|
||||
export AWS_SECRET_ACCESS_KEY="${MINIO_SECRET_KEY}"
|
||||
|
||||
# Quota Check Configuration
|
||||
BUCKET_NAME="backup"
|
||||
MIN_QUOTA_REQUIRED_MB=1024 # Minimum required space in megabytes (1GB)
|
||||
|
||||
# Notification System Configuration
|
||||
# EMAIL_RECIPIENT="william.valentin.info@gmail.com"
|
||||
# EMAIL_SUBJECT="Restic Backup Report"
|
||||
|
||||
# --- FUNCTIONS ---
|
||||
send_notification() {
|
||||
local type="$1"
|
||||
local message="$2"
|
||||
local summary="Restic Backup Report"
|
||||
|
||||
# Send GNOME notification
|
||||
if command -v notify-send &> /dev/null; then
|
||||
notify-send -t 5000 -i "dialog-${type}" "${summary}" "${message}"
|
||||
fi
|
||||
|
||||
# Send email notification
|
||||
if [ -n "${EMAIL_RECIPIENT}" ]; then
|
||||
echo -e "${message}" | mail -s "${summary} - ${type^^}" "${EMAIL_RECIPIENT}"
|
||||
fi
|
||||
}
|
||||
|
||||
# --- SCRIPT START ---
|
||||
echo "Starting Restic backup process for user folder..."
|
||||
echo "------------------------------------------------"
|
||||
|
||||
# --- 1. MinIO Client Setup & Quota Check ---
|
||||
# Check if MinIO client (mc) is installed
|
||||
if ! command -v mc &> /dev/null; then
|
||||
send_notification "error" "MinIO client 'mc' not found. Please install it to use the quota check."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Set up MinIO alias if it doesn't exist
|
||||
if ! mc alias list | grep -q "${MINIO_ALIAS}"; then
|
||||
echo "Setting up MinIO alias '${MINIO_ALIAS}'..."
|
||||
mc alias set "${MINIO_ALIAS}" "${MINIO_URL}" "${MINIO_ACCESS_KEY}" "${MINIO_SECRET_KEY}"
|
||||
if [ $? -ne 0 ]; then
|
||||
send_notification "error" "Failed to set up MinIO alias. Check your credentials and URL."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Get bucket usage and quota
|
||||
BUCKET_USAGE_MB=$(mc du "${MINIO_ALIAS}/${BUCKET_NAME}" | awk '{print $1}')
|
||||
BUCKET_QUOTA_MB=$(mc quota info "${MINIO_ALIAS}/${BUCKET_NAME}" | grep 'Hard Quota' | awk '{print $3}' | sed 's/MB//')
|
||||
|
||||
if [ -z "${BUCKET_QUOTA_MB}" ]; then
|
||||
echo "NOTICE: No hard quota set for bucket '${BUCKET_NAME}'. Skipping quota check."
|
||||
else
|
||||
# Calculate remaining space
|
||||
REMAINING_SPACE_MB=$((BUCKET_QUOTA_MB - BUCKET_USAGE_MB))
|
||||
|
||||
echo "Bucket '${BUCKET_NAME}' usage: ${BUCKET_USAGE_MB} MB"
|
||||
echo "Bucket '${BUCKET_NAME}' quota: ${BUCKET_QUOTA_MB} MB"
|
||||
echo "Remaining space: ${REMAINING_SPACE_MB} MB"
|
||||
|
||||
if [ "${REMAINING_SPACE_MB}" -lt "${MIN_QUOTA_REQUIRED_MB}" ]; then
|
||||
send_notification "error" "Insufficient space in the bucket. Only ${REMAINING_SPACE_MB} MB remaining, but ${MIN_QUOTA_REQUIRED_MB} MB required."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- 2. Restic Repository Check ---
|
||||
echo "Checking Restic repository existence..."
|
||||
if ! restic snapshots --json &> /dev/null; then
|
||||
echo "Restic repository not found. Initializing a new one..."
|
||||
restic init
|
||||
if [ $? -ne 0 ]; then
|
||||
send_notification "error" "Failed to initialize Restic repository. Check your RESTIC_REPO and credentials."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- 3. Backup Execution ---
|
||||
echo "Starting backup of ${BACKUP_SOURCE}..."
|
||||
|
||||
if [ -f "${RESTIC_EXCLUDE_FILE}" ]; then
|
||||
echo "Excluding files listed in ${RESTIC_EXCLUDE_FILE}..."
|
||||
restic backup "${BACKUP_SOURCE}" --exclude-file="${RESTIC_EXCLUDE_FILE}"
|
||||
else
|
||||
echo "No exclude file found, backing up all files in the source directory."
|
||||
restic backup "${BACKUP_SOURCE}"
|
||||
fi
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
send_notification "error" "Restic backup failed."
|
||||
exit 1
|
||||
else
|
||||
send_notification "info" "Restic backup completed successfully."
|
||||
fi
|
||||
|
||||
# --- 4. Cleanup and Maintenance ---
|
||||
echo "Running repository maintenance..."
|
||||
restic forget --prune --keep-daily 7 --keep-weekly 4 --keep-monthly 6
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
send_notification "warning" "Restic maintenance failed. Check the logs for details."
|
||||
fi
|
||||
|
||||
echo "Backup script finished. ✨"
|
||||
Reference in New Issue
Block a user