OpenWRT中备份多个docker容器的脚本
在OpenWrt 24.10.1上测试通过
#!/bin/sh
# OpenWrt Docker Container Backup Script
# Backup docker container and copy to network storage
echo "=========================================="
echo "Docker Container Backup Script Starting"
echo "=========================================="
# Configuration parameters
DOCKER_CONTAINERS=(my-postgres my-redis my-nginx) # 这里填写需要备份的容器名,可以是多个
BACKUP_DIR="/root/docker_backup"
NAS_PATH="//192.168.0.20/backup" # NAS共享路径
NAS_USERNAME="your_username" # NAS用户名
NAS_PASSWORD="your_password" # NAS密码
MOUNT_POINT="/mnt/post"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
RETENTION_DAYS=7 # Keep backups for 7 days, delete older ones
# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
echo "[FATAL] This script must be run as root!"
echo "Please run with: sudo $0"
exit 1
fi
# Check docker
if ! command -v docker >/dev/null 2>&1; then
echo "[FATAL] Docker command not found! Please install docker first."
exit 1
fi
# Check rsync
if ! command -v rsync >/dev/null 2>&1; then
echo "[FATAL] rsync command not found! Please install rsync first."
exit 1
fi
# Check mount command
if ! command -v mount >/dev/null 2>&1; then
echo "[FATAL] mount command not found!"
exit 1
fi
# Check gzip
if ! command -v gzip >/dev/null 2>&1; then
echo "[FATAL] gzip command not found! Please install gzip first."
exit 1
fi
# Check CIFS kernel module
echo "[INFO] Checking CIFS kernel module (kmod-fs-cifs)..."
if opkg list-installed | grep "kmod-fs-cifs"; then
echo "[SUCCESS] kmod-fs-cifs is installed."
elif opkg list | grep "kmod-fs-cifs"; then
echo "[INFO] Installing kmod-fs-cifs..."
opkg update
opkg install kmod-fs-cifs
if [ $? -eq 0 ]; then
echo "[SUCCESS] kmod-fs-cifs installed successfully."
else
echo "[FATAL] Failed to install kmod-fs-cifs. CIFS mounting will not work."
exit 1
fi
else
echo "[FATAL] kmod-fs-cifs package not found in repositories."
echo "CIFS mounting requires this kernel module. Please install it manually."
exit 1
fi
# 检查所有容器是否存在
for CONTAINER_NAME in "${DOCKER_CONTAINERS[@]}"; do
CONTAINER_ID=$(docker ps -qf "name=$CONTAINER_NAME")
if [ -z "$CONTAINER_ID" ]; then
echo "[FATAL] No Docker container named '$CONTAINER_NAME' is running!"
echo "Please start the container first or check the container name."
exit 1
fi
echo "[INFO] Docker container found: $CONTAINER_NAME"
done
# Function to check if mount point is mounted
check_mount() {
if grep -q "$MOUNT_POINT" /proc/mounts; then
return 0
fi
if mount | grep -q "$MOUNT_POINT"; then
return 0
fi
if [ -d "$MOUNT_POINT" ] && [ "$(ls -A "$MOUNT_POINT" 2>/dev/null)" ]; then
return 0
fi
return 1
}
# Check if mount is successful
if ! check_mount; then
echo "[INFO] NAS not mounted, attempting to mount..."
mkdir -p "$MOUNT_POINT"
mount -t cifs -o username="$NAS_USERNAME",password="$NAS_PASSWORD" "$NAS_PATH" "$MOUNT_POINT"
sleep 2
if ! check_mount; then
echo "[FATAL] NAS mount failed, aborting backup."
exit 1
fi
echo "[SUCCESS] NAS mounted successfully."
else
echo "[INFO] NAS is already mounted."
fi
# Create backup directory
mkdir -p "$BACKUP_DIR"
if [ $? -ne 0 ]; then
echo "[FATAL] Failed to create backup directory."
exit 1
fi
# 循环备份每个容器
for CONTAINER_NAME in "${DOCKER_CONTAINERS[@]}"; do
BACKUP_FILENAME="${CONTAINER_NAME}_bak_${TIMESTAMP}.tar.gz"
BACKUP_PATH="${BACKUP_DIR}/${BACKUP_FILENAME}"
echo "[INFO] Backing up container: $CONTAINER_NAME"
docker export "$(docker ps -qf "name=$CONTAINER_NAME")" | gzip > "$BACKUP_PATH"
if [ $? -ne 0 ]; then
echo "[ERROR] Backup failed for container: $CONTAINER_NAME"
exit 2
fi
echo "[SUCCESS] Backup completed: $BACKUP_FILENAME"
rsync -av "$BACKUP_PATH" "$MOUNT_POINT/"
if [ $? -ne 0 ]; then
echo "[ERROR] Failed to copy backup to NAS for container: $CONTAINER_NAME"
exit 3
fi
echo "[INFO] Cleaning up old backups for container: $CONTAINER_NAME"
find "$BACKUP_DIR" -name "${CONTAINER_NAME}_bak_*.tar.gz" -type f -mtime +$RETENTION_DAYS -exec rm -f {} \;
find "$MOUNT_POINT" -name "${CONTAINER_NAME}_bak_*.tar.gz" -type f -mtime +$RETENTION_DAYS -exec rm -f {} \;
echo "[SUCCESS] Cleanup completed for container: $CONTAINER_NAME"
done
echo "[SUCCESS] All backups and cleanups completed!"

浙公网安备 33010602011771号