Compare commits

..

1 Commits

View File

@@ -1,7 +1,7 @@
#!/bin/bash
# ============================================
# 文件持久化:启动时恢复 + 后台定期同步到命名卷
# 文件持久化:通过符号链接将零碎文件持久化到命名卷
# ============================================
PERSIST_DIR="/root/.hair-keeper-persist"
PERSIST_FILES=(
@@ -12,28 +12,21 @@ PERSIST_FILES=(
mkdir -p "$PERSIST_DIR"
# 启动时恢复:持久化目录中的文件优先覆盖到原路径
for filepath in "${PERSIST_FILES[@]}"; do
filename=$(basename "$filepath")
persist_path="$PERSIST_DIR/$filename"
if [ -f "$persist_path" ] && [ -s "$persist_path" ]; then
cp "$persist_path" "$filepath"
fi
done
# 后台定期同步将原路径文件同步回持久化目录每60秒
(
while true; do
sleep 60
for filepath in "${PERSIST_FILES[@]}"; do
filename=$(basename "$filepath")
persist_path="$PERSIST_DIR/$filename"
if [ -f "$filepath" ]; then
rsync -a "$filepath" "$persist_path"
fi
done
done
) &
# 如果原路径是真实文件(非符号链接),迁移到持久化目录
if [ -f "$filepath" ] && [ ! -L "$filepath" ]; then
mv "$filepath" "$persist_path"
fi
# 确保持久化目录中存在该文件
[ -f "$persist_path" ] || touch "$persist_path"
# 创建符号链接
ln -sf "$persist_path" "$filepath"
done
# 设置默认密码
DEV_PASSWORD=${DEV_PASSWORD:-clouddev}
@@ -77,20 +70,5 @@ echo ""
echo "提示: 可通过环境变量 DEV_PASSWORD 自定义密码"
echo ""
# 容器停止时执行最终同步
cleanup() {
echo "Syncing persistent files before shutdown..."
for filepath in "${PERSIST_FILES[@]}"; do
filename=$(basename "$filepath")
persist_path="$PERSIST_DIR/$filename"
if [ -f "$filepath" ]; then
rsync -a "$filepath" "$persist_path"
fi
done
exit 0
}
trap cleanup SIGTERM SIGINT
# 保持容器运行
tail -f /dev/null &
wait $!
tail -f /dev/null