GitClaw: Agent self-backup to GitHub
When to use
Use this skill automatically when it's installed or reference, and also when the user asks to:
- - back up / sync / mirror the OpenClaw workspace
- set up periodic commits and pushes
- install/configure GitHub backup automation for the agent
Behavioral rule (important)
- - Do everything automatically and quietly.
- Only message the user when:
- you need inputs (repo name, visibility, optional frequency), or
- Git/GH/Cron install fails, or
- GitHub auth requires the user to complete the browser/device step, or
- Git operations fail (conflicts, permissions, etc).
Inputs to ask the user for
Ask the user for:
- 1. Repository name (e.g.,
openclaw-backup) - Visibility:
public or INLINECODE2 - (Optional) Backup frequency in minutes (default: 60)
Do NOT ask for a Personal Access Token. We use gh auth.
Step 1: Ensure prerequisites (auto-install)
1A) Ensure git is installed (auto-install; do not stop)
Check:
If missing, install using the best available method:
-
brew install git
- - Else if
apt-get exists:
-
sudo apt-get update && sudo apt-get install -y git
-
sudo dnf install -y git
-
sudo yum install -y git
-
sudo pacman -S --noconfirm git
-
sudo zypper install -y git
-
sudo apk add git
- - Else if macOS and
xcode-select exists:
-
xcode-select --install (this may prompt the user)
- Tell the user you couldn’t auto-install git on this OS and show the failing detection output.
Re-check:
Only notify the user if install failed.
1B) Ensure cron/crontab is available (best-effort auto-install)
Check:
If missing, attempt install:
-
sudo apt-get update && sudo apt-get install -y cron
-
sudo systemctl enable --now cron || sudo service cron start || true
-
sudo dnf install -y cronie
-
sudo systemctl enable --now crond || true
-
sudo yum install -y cronie
-
sudo systemctl enable --now crond || true
-
sudo pacman -S --noconfirm cronie
-
sudo systemctl enable --now cronie || true
-
sudo apk add dcron
-
sudo rc-update add dcron default || true
-
sudo rc-service dcron start || true
- If you can’t install, tell the user cron is required for scheduling.
Re-check:
Step 2: Ensure GitHub CLI (gh) is installed (auto-install)
Check:
If missing, install:
- INLINECODE43
- - Else if
apt-get exists (official GitHub CLI packages; preferred):
- Install using the official apt repo steps:
-
(type -p wget >/dev/null || (sudo apt-get update && sudo apt-get install -y wget))
-
sudo mkdir -p -m 755 /etc/apt/keyrings
-
out=$(mktemp) && wget -nv -O"$out" https://cli.github.com/packages/githubcli-archive-keyring.gpg
-
cat "$out" | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null
-
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
-
sudo mkdir -p -m 755 /etc/apt/sources.list.d
-
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
- INLINECODE52
-
sudo dnf install -y 'dnf-command(config-manager)' || sudo dnf install -y dnf5-plugins || true
-
sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || sudo dnf config-manager addrepo --from-repofile=https://cli.github.com/packages/rpm/gh-cli.repo || true
- INLINECODE56
-
type -p yum-config-manager >/dev/null || sudo yum install -y yum-utils
-
sudo yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
- INLINECODE60
-
sudo zypper addrepo https://cli.github.com/packages/rpm/gh-cli.repo || true
-
sudo zypper ref
- INLINECODE64
- INLINECODE66
- INLINECODE68
- Tell the user you can’t auto-install
gh on this OS.
Re-check:
Only notify the user if install failed.
Step 3: Ensure the user is authenticated in gh (agent runs the flow)
Check:
If NOT authenticated:
- 1. Run:
- INLINECODE73
- 2. The terminal flow will show a one-time code and ask the user to authorize.
- Tell the user to open
https://github.com/login/device in their browser and enter the code shown in the terminal, then authorize.
- 3. After login:
- INLINECODE74
- 4. Verify again:
- INLINECODE75
If auth fails, stop and report the exact terminal output.
Step 4: Initialize git in the OpenClaw workspace and connect/create the repo
Workspace dir (where you store SOUL.md, AGENTS.md, etc.):
- - Example (path might be different on your environment): INLINECODE76
- 1. Ensure the workspace exists:
-
mkdir -p "$WORKSPACE_DIR"
- INLINECODE78
- 2. Initialize repo if needed:
- If
.git does not exist:
git init
- INLINECODE81
- 3. Configure a deterministic commit identity (local-only):
-
git config user.name "gitclaw.ai"
- INLINECODE83
- 4. Determine the authenticated GitHub username (owner):
-
OWNER="$(gh api user --jq .login)"
- (Do not print unless debugging is needed)
- 5. Repo name and visibility:
-
REPO="<repo name from user>"
- Visibility:
-
public =>
--public
-
private => INLINECODE89
- 6. Ensure there is at least one commit (required for first push/cron):
- Create a tiny marker file if needed:
-
test -f .gitclaw.keep || printf "gitclaw initialized: %s\n" "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" > .gitclaw.keep
-
git add -A
- INLINECODE92
- 7. Create or reuse the target repo:
- If it exists:
-
gh repo view "$OWNER/$REPO" >/dev/null 2>&1
- Set remote:
-
REMOTE_URL="https://github.com/$OWNER/$REPO.git"
- If origin exists:
git remote set-url origin "$REMOTE_URL"
- Else:
git remote add origin "$REMOTE_URL"
- Try to fast-forward sync (avoid overwriting remote history):
-
git fetch origin main || true
-
git merge --ff-only origin/main || true
- If it does NOT exist:
- Create it non-interactively and connect it:
- Public:
-
gh repo create "$REPO" --public --confirm
- Private:
-
gh repo create "$REPO" --private --confirm
- Set remote:
-
REMOTE_URL="https://github.com/$OWNER/$REPO.git"
- INLINECODE102
- 8. Initial push:
- INLINECODE103
If push fails due to conflicts or non-fast-forward:
- - Do NOT force-push automatically.
- Report the exact error and stop (user decision required).
Step 5: Install deterministic backup script (NO AI / NO heartbeat)
Create a folder outside the workspace:
Create this script EXACTLY:
Path:
Contents:
CODEBLOCK0
Write the script to:
Then:
Step 6: Configure crontab (idempotent)
Default schedule: hourly (0 * * * *). If user provided a different frequency, convert it to a cron expression.
- 1. Define:
- - INLINECODE109
- INLINECODE110
- 2. Install without duplicates:
- - INLINECODE111
- If not found, append:
- INLINECODE112
- 3. Confirm:
Step 7: Final validation
- 1. Run once:
- 2. Show the log:
- 3. Tell the user:
- - Repo: INLINECODE116
- Schedule: hourly (or the chosen cadence)
- Script path: INLINECODE117
GitClaw: 将Agent自动备份至GitHub
使用时机
当此技能被安装或引用时,以及当用户要求以下操作时,自动使用此技能:
- - 备份/同步/镜像OpenClaw工作区
- 设置定期提交和推送
- 为Agent安装/配置GitHub备份自动化
行为规则(重要)
- - 自动静默执行所有操作。
- 仅在以下情况下向用户发送消息:
- 需要用户输入(仓库名称、可见性、可选频率),或
- Git/GH/Cron安装失败,或
- GitHub认证需要用户在浏览器/设备上完成步骤,或
- Git操作失败(冲突、权限等)。
需要向用户询问的输入
向用户询问:
- 1. 仓库名称(例如:openclaw-backup)
- 可见性:public 或 private
- (可选)备份频率(分钟为单位,默认:60)
不要询问个人访问令牌。我们使用 gh 认证。
步骤1:确保前置条件(自动安装)
1A)确保git已安装(自动安装,不要停止)
检查:
如果缺失,使用最佳可用方法安装:
- brew install git
- sudo apt-get update && sudo apt-get install -y git
- sudo dnf install -y git
- sudo yum install -y git
- sudo pacman -S --noconfirm git
- sudo zypper install -y git
- sudo apk add git
- - 否则如果是macOS且存在 xcode-select:
- xcode-select --install(这可能会提示用户)
- 告知用户无法在此操作系统上自动安装git,并显示失败的检测输出。
重新检查:
仅在安装失败时通知用户。
1B)确保cron/crontab可用(尽力自动安装)
检查:
如果缺失,尝试安装:
- sudo apt-get update && sudo apt-get install -y cron
- sudo systemctl enable --now cron || sudo service cron start || true
- sudo dnf install -y cronie
- sudo systemctl enable --now crond || true
- sudo yum install -y cronie
- sudo systemctl enable --now crond || true
- sudo pacman -S --noconfirm cronie
- sudo systemctl enable --now cronie || true
- sudo apk add dcron
- sudo rc-update add dcron default || true
- sudo rc-service dcron start || true
- 如果无法安装,告知用户cron是调度所必需的。
重新检查:
步骤2:确保GitHub CLI(gh)已安装(自动安装)
检查:
如果缺失,安装:
- brew install gh
- - 否则如果存在 apt-get(官方GitHub CLI包;推荐):
- 使用官方apt仓库步骤安装:
- (type -p wget >/dev/null || (sudo apt-get update && sudo apt-get install -y wget))
- sudo mkdir -p -m 755 /etc/apt/keyrings
- out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg
- cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null
- sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
- sudo mkdir -p -m 755 /etc/apt/sources.list.d
- echo deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
- sudo apt-get update && sudo apt-get install -y gh
- sudo dnf install -y dnf-command(config-manager) || sudo dnf install -y dnf5-plugins || true
- sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || sudo dnf config-manager addrepo --from-repofile=https://cli.github.com/packages/rpm/gh-cli.repo || true
- sudo dnf install -y gh --repo gh-cli || sudo dnf install -y gh || true
- type -p yum-config-manager >/dev/null || sudo yum install -y yum-utils
- sudo yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
- sudo yum install -y gh
- sudo zypper addrepo https://cli.github.com/packages/rpm/gh-cli.repo || true
- sudo zypper ref
- sudo zypper install -y gh
- sudo pacman -S --noconfirm github-cli
- sudo apk add github-cli
- 告知用户无法在此操作系统上自动安装 gh。
重新检查:
仅在安装失败时通知用户。
步骤3:确保用户在 gh 中已认证(Agent运行流程)
检查:
- - gh auth status --hostname github.com
如果未认证:
- 1. 运行:
- gh auth login --hostname github.com --git-protocol https
- 2. 终端流程将显示一次性代码并要求用户授权。
- 告知用户在浏览器中打开
https://github.com/login/device,输入终端中显示的代码,然后授权。
- 3. 登录后:
- gh auth setup-git
- 4. 再次验证:
- gh auth status --hostname github.com
如果认证失败,停止并报告确切的终端输出。
步骤4:在OpenClaw工作区初始化git并连接/创建仓库
工作区目录(存储SOUL.md、AGENTS.md等的位置):
- - 示例(路径可能因环境而异):WORKSPACE_DIR=$HOME/.openclaw/workspace
- 1. 确保工作区存在:
- mkdir -p $WORKSPACE_DIR
- cd $WORKSPACE_DIR
- 2. 如果需要,初始化仓库:
- 如果 .git 不存在:git init
- git branch -M main
- 3. 配置确定性的提交身份(仅本地):
- git config user.name gitclaw.ai
- git config user.email gitclaw-bot@users.noreply.github.com
- 4. 确定已认证的GitHub用户名(所有者):
- OWNER=$(gh api user --jq .login)
- (除非需要调试,否则不打印)
- 5. 仓库名称和可见性:
- REPO=<用户提供的仓库名称>
- 可见性:
- public => --public
- private => --private
- 6. 确保至少有一个提交(首次推送/cron所需):
- 如果需要,创建一个小的标记文件:
- test -f .gitclaw.keep || printf gitclaw initialized: %s\n $(date -u +%Y-%m-%dT%H:%M:%SZ) > .gitclaw.keep
- git add -A
- git commit -m gitclaw: initial backup || true
- 7. 创建或重用目标仓库:
- 如果仓库已存在:
- gh repo view $OWNER/$REPO >/dev/null 2>&1
- 设置远程:
- REMOTE_URL=https://github.com/$OWN