返回顶部
g

git-backupGit备份

Backup the agent workspace to a GitHub repository. Use when: asked to save/remember something important, after significant changes to memory files, on a schedule, or when asked to push workspace to git. Handles token discovery, repo initialization, and push. If no GitHub token is available, asks the owner for a PAT before proceeding.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.2
安全检测
已通过
98
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

git-backup

Git 备份技能

加载本地上下文

bash CONTEXT_FILE=/opt/ocana/openclaw/workspace/skills/git-backup/.context [ -f $CONTEXTFILE ] && source $CONTEXTFILE

然后使用:$GITHUBACCOUNT, $REPONAME, $WORKSPACE, $GITHUBTOKENFILE 等变量

最低模型要求

任意模型。这是纯 Shell 脚本——无需推理能力。

步骤 1 — 查找 GitHub 令牌

运行以下命令查找令牌。它按优先级顺序检查来源:

bash
findgithubtoken() {
# 1. 检查 git 远程 URL(最常见)
TOKEN=$(git -C $HOME/.openclaw/workspace remote get-url origin 2>/dev/null \
| grep -oP ghp_[A-Za-z0-9]+ | head -1)
[ -n $TOKEN ] && echo $TOKEN && return

# 2. 检查标准环境变量
[ -n ${GITHUBTOKEN:-} ] && echo $GITHUBTOKEN && return
[ -n ${GHTOKEN:-} ] && echo $GHTOKEN && return

# 3. 检查凭据文件
for f in ~/.credentials/github.txt ~/.credentials/gh.txt; do
[ -f $f ] && cat $f | head -1 | tr -d [:space:] && return
done

# 4. 检查 ~/.bashrc 中嵌入的令牌
TOKEN=$(grep -oP ghp_[A-Za-z0-9]+ ~/.bashrc 2>/dev/null | head -1)
[ -n $TOKEN ] && echo $TOKEN && return

# 未找到——返回空值
echo
}

TOKEN=$(findgithubtoken)

如果 TOKEN 为空 → 停止并询问所有者:

我需要一个 GitHub 个人访问令牌来备份你的工作空间。
请前往:github.com → Settings → Developer Settings → Personal access tokens → Generate new token
所需权限:repo(完整)
将令牌发送给我(以 ghp_ 开头)

收到令牌后,保存它:
bash

将令牌保存到凭据文件


echo $TOKEN > ~/.credentials/github-token.txt
chmod 600 ~/.credentials/github-token.txt

更新 git 远程 URL 以包含令牌

git -C $HOME/.openclaw/workspace remote set-url origin \ https://${TOKEN}@github.com/GITHUBUSERNAME/REPONAME.git

步骤 2 — 首次设置(仅运行一次)

检查仓库是否已存在

bash
TOKEN=$(findgithubtoken)
REPO_NAME=pa-workspace-backup # 改为你的仓库名称
GITHUB_USER=github-username # 改为你的 GitHub 用户名

HTTP 200 = 仓库已存在,404 = 需要创建

STATUS=$(curl -s -o /dev/null -w %{http_code} \ -H Authorization: token $TOKEN \ https://api.github.com/repos/$GITHUBUSER/$REPONAME)

echo 仓库检查:HTTP $STATUS

创建仓库(如果 HTTP 404)

bash
curl -s -X POST https://api.github.com/user/repos \
-H Authorization: token $TOKEN \
-H Content-Type: application/json \
-d {\name\: \$REPO_NAME\, \private\: true, \description\: \PA 工作空间备份\} \
| python3 -c
import sys, json
d = json.load(sys.stdin)
if html_url in d:
print(已创建:, d[html_url])
else:
print(错误:, d)
sys.exit(1)

初始化 git(如果没有 .git 文件夹)

bash
cd ~/.openclaw/workspace

初始化仓库

git init git config user.email agent@openclaw.ai git config user.name PA Agent git remote add origin https://$TOKEN@github.com/$GITHUBUSER/$REPONAME.git

创建 .gitignore 以排除密钥和临时文件

cat > .gitignore << EOF *.log .env credentials/ *.key *.pem node_modules/ pycache/ .DS_Store EOF

初始提交和推送

git add -A git commit -m 初始工作空间备份 git push -u origin main

步骤 3 — 定期备份

每当需要备份时运行此命令:

bash
backup_workspace() {
WORKSPACE=${1:-$HOME/.openclaw/workspace}
cd $WORKSPACE || { echo 错误:无法进入目录 $WORKSPACE; return 1; }

# 检查令牌
TOKEN=$(findgithubtoken)
if [ -z $TOKEN ]; then
echo 已阻止:未找到 GitHub 令牌。请向所有者请求 PAT。
return 1
fi

# 检查 git 是否已初始化
if [ ! -d .git ]; then
echo 已阻止:Git 未初始化。请先运行首次设置。
return 1
fi

# 暂存所有更改
git add -A

# 计算更改的文件数量
CHANGES=$(git diff --cached --name-only | wc -l)

# 没有需要处理的内容?
if [ $CHANGES -eq 0 ]; then
echo 无需备份——工作空间未更改。
return 0
fi

# 使用时间戳提交并推送
DATE=$(date -u +%Y-%m-%d %H:%M UTC)
git commit -m 自动备份 $DATE
git push origin main 2>&1

echo 备份完成——已更新 $CHANGES 个文件。
}

backup_workspace

如果推送失败并显示non-fast-forward:
bash

先拉取远程更改,然后推送


git pull --rebase origin main
git push origin main

如果推送失败并显示 401(令牌已过期):
bash

向所有者请求新的 PAT,然后更新远程 URL


git remote set-url origin https://NEWTOKEN@github.com/GITHUBUSERNAME/REPO_NAME.git


何时备份

在以下情况下运行备份:

  • - 所有者说记住这个/保存这个
  • 你更新了 MEMORY.md、SOUL.md 或每日笔记
  • 完成重要任务后
  • 按计划进行(参见下面的 cron 配置)

Cron 配置

json
{
jobs: [
{
id: workspace-backup,
schedule: 0 /6 ,
timezone: UTC,
task: 运行工作空间的 git 备份到 GitHub。使用 git-backup 技能。提交所有更改并推送。报告完成或已阻止。,
delivery: {
mode: silent
}
}
]
}

每 6 小时静默运行一次。改为 0 23 * 则仅在夜间运行。



包含/排除的内容

始终包含:

  • - MEMORY.md、SOUL.md、AGENTS.md、TOOLS.md、PA_LIST.md
  • memory/ — 每日笔记
  • skills/ — 已安装的技能
  • .learnings/ — 修正和学习记录
  • data/ — PA 目录和其他数据
  • config/ — MCP 和其他配置

始终排除(添加到 .gitignore):

  • - API 密钥、令牌、密钥
  • credentials/ 目录
  • 日志文件(*.log)
  • Node 模块、Python 缓存

生产环境说明

  • - 工作空间位于 /opt/ocana/openclaw/workspace(而非 ~/.openclaw/workspace)
  • GitHub 账户:netanel-abergel,仓库:heleni-memory
  • 每次重要的记忆更新后——始终推送到 git(不要无限期地批量处理)
  • 技能更新后(如本次审查)——立即推送

成本提示

  • - 非常便宜: 纯 git/bash——备份期间不使用 LLM 令牌
  • 小模型也可以: 任何模型都可以触发此技能
  • 批量处理: 将所有更改一次性提交为 git add -A 并一次推送——不要逐个文件操作
  • 合理安排计划: 每 6 小时一次就足够了。更频繁 = git

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 git-backup-1775889086 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 git-backup-1775889086 技能

通过命令行安装

skillhub install git-backup-1775889086

下载

⬇ 下载 git-backup v1.0.2(免费)

文件大小: 3.87 KB | 发布时间: 2026-4-12 10:05

v1.0.2 最新 2026-4-12 10:05
- Added local context loading instructions for path, repo, and token configuration.
- Updated workspace and repository references for production use (`/opt/ocana/openclaw/workspace`, `netanel-abergel/heleni-memory`).
- Enhanced file inclusion list to cover `PA_LIST.md`.
- clarified that after significant memory or skill updates, changes should be pushed to git immediately.
- Provided details on production paths and deployment notes.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部