#!/bin/bash
# 设置变量
CONTAINER_NAME="ems-tdengine"
BACKUP_ROOT="/opt/ems-platform/backups/tdengine"
DATE=$(date +%Y%m%d_%H%M)
TARGET_DIR="$BACKUP_ROOT/$DATE"

mkdir -p $TARGET_DIR

# 1. 调用容器内的 taosdump 工具导出元数据和数据
echo "Starting TDengine backup..."
docker exec $CONTAINER_NAME taosdump -o /tmp/dump_out -D power_db

# 2. 将备份文件从容器复制到宿主机
docker cp $CONTAINER_NAME:/tmp/dump_out/. $TARGET_DIR/

# 3. 清理容器内临时文件
docker exec $CONTAINER_NAME rm -rf /tmp/dump_out

# 4. 压缩 (时序数据通常很大，建议压缩)
cd $BACKUP_ROOT
tar -czf $DATE.tar.gz $DATE
rm -rf $DATE

# 5. 清理超过 30 天的备份
find $BACKUP_ROOT -name "*.tar.gz" -mtime +30 -delete

echo "Backup completed: $TARGET_DIR"

