使用 git 作为 cron 和 ssh 别名的备份工具

Using git as backup tool with cron and ssh aliases

我有一个存储库来存储我的所有配置(例如 .ssh/config)。有时我需要备份它们,所以我需要 运行 这些命令

git commit -am 'Auto git backup `date -I`'
git push origin master

这很烦人,所以我决定做一个 cron 作业,它是 运行 上面这些行的简单脚本。我 运行 crontab -e 并在文件末尾添加了新行。我还在脚本中用 which git 替换了 gitgit commit 命令与 git push 相比效果完美。当我进行一些调试打印时,我得到了

ssh: Could not resolve hostname bitbucket.org: Name or service not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

但是当我 运行 手动编写脚本时,它起作用了。我认为这是因为我的 .ssh/config 中的 SSH 别名,看起来像

Host     bitbucket.org-personal
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

Host    bitbucket.org-work
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/another_id_rsa

有人知道如何将 SSH 别名传播到 运行s cron 作业的环境吗?也可能我错了,问题是由其他原因引起的。

问题出在网络连接上。当我在脚本开头添加睡眠(或从此处 How to check internet access using bash script in linux? 修改解决方案)时,它完美运行。