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

时间:2017-08-12 18:38:53

标签: git ssh cron alias

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

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

这很烦人,所以我决定做一个cron作业,用上面的这些行运行简单的脚本。我运行crontab -e并在文件末尾添加了新行。我还在脚本中用git替换了which 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别名传播到运行cron作业的环境?也许我错了,问题是由别的东西引起的。

1 个答案:

答案 0 :(得分:0)

问题是网络连接。当我在脚本的乞讨时添加睡眠(或来自此处How to check internet access using bash script in linux?的修改后的解决方案)时,它完美地运行。