Git Backup Skill
Load Local Context
CODEBLOCK0
Minimum Model
Any model. This is pure shell — no reasoning needed.
Step 1 — Find the GitHub Token
Run this to find the token. It checks sources in priority order:
CODEBLOCK1
If TOKEN is empty → stop and ask the owner:
CODEBLOCK2
After receiving the token, save it:
# Save token to credentials file
echo "$TOKEN" > ~/.credentials/github-token.txt
chmod 600 ~/.credentials/github-token.txt
# Update the git remote URL to include the token
git -C "$HOME/.openclaw/workspace" remote set-url origin \
"https://${TOKEN}@github.com/GITHUB_USERNAME/REPO_NAME.git"
Step 2 — First-Time Setup (Run Once)
Check if the repo already exists
CODEBLOCK4
Create the repo (if HTTP 404)
CODEBLOCK5
Initialize git (if no .git folder)
CODEBLOCK6
Step 3 — Regular Backup
Run this whenever you need to back up:
CODEBLOCK7
If push fails with "non-fast-forward":
CODEBLOCK8
If push fails with 401 (token expired):
# Ask owner for a new PAT, then update the remote URL
git remote set-url origin "https://NEW_TOKEN@github.com/GITHUB_USERNAME/REPO_NAME.git"
When to Back Up
Run a backup when:
- - Owner says "remember this" / "save this"
- You update
MEMORY.md, SOUL.md, or daily notes - After completing a significant task
- On a schedule (see cron config below)
Cron Config
CODEBLOCK10
Runs every 6 hours silently. Change to "0 23 * * *" for nightly only.
What to Include / Exclude
Always include:
- -
MEMORY.md, SOUL.md, AGENTS.md, TOOLS.md, INLINECODE7 - INLINECODE8 — daily notes
- INLINECODE9 — installed skills
- INLINECODE10 — corrections and learnings
- INLINECODE11 — PA directory and other data
- INLINECODE12 — MCP and other configs
Always exclude (add to .gitignore):
- - API keys, tokens, secrets
- INLINECODE13 directory
- Log files (
*.log) - Node modules, Python cache
Production Notes
- - Workspace is at
/opt/ocana/openclaw/workspace (not ~/.openclaw/workspace) - GitHub account:
netanel-abergel, repo: INLINECODE18 - After every significant memory update — always push to git (do not batch indefinitely)
- After skill updates (like this review) — push immediately
Cost Tips
- - Very cheap: Pure git/bash — no LLM tokens used during backup
- Small model OK: Any model can trigger this skill
- Batch: Commit all changes in one
git add -A and one push — not file by file - Schedule wisely: Every 6 hours is enough. More frequent = noise in git history
Git 备份技能
加载本地上下文
bash
CONTEXT_FILE=/opt/ocana/openclaw/workspace/skills/git-backup/.context
[ -f $CONTEXT
FILE ] && 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/GITHUB
USERNAME/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/$GITHUB
USER/$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/$GITHUB
USER/$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://NEW
TOKEN@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