如何使用 crontab、.netrc 和 git push?

如何使用 crontab、.netrc 和 git push?

我正在将各个服务器的备份自动备份到一个中心点,然后将这些配置更改推送到 git 存储库,以便我可以跟踪随时间推移的任何更改。其余脚本运行良好,我可以通过网络将文件复制/rsync 到一个中心点。最后一个脚本是将配置文件放入/更新到存储库中。脚本如下:

#!/bin/bash
clear

SERVERNAME="betty"
SCRIPTDIR="/home/jon"
GITROOT="/tmp/git"
TEMPROOT="/tmp/backups"
BACKUPROOTDIR="/mnt/backups"

echo "  - running as user: $UID"

echo "backingup git config on $SERVERNAME"
echo ""

# check to see if root backup folder exists, otherwise create it.
if [ -d $GITROOT ]; then
    rm -rf $GITROOT
fi
mkdir $GITROOT

cd $GITROOT

echo "  - testing if home is where I think it should be!"
echo $HOME
echo "  - testing if it can see netrc"
tail $HOME/.netrc


git clone http://192.168.10.97:8000/repositories/HOH-config-backups.git
cd HOH-config-backups

echo "  - copy Configuration Folders across"
cp -r $BACKUPROOTDIR/Configuration/* $GITROOT/HOH-config-backups/
cp -r $BACKUPROOTDIR/scripts $GITROOT/HOH-config-backups/

git add .
git commit -a -m "committing any new configuration changes!"
git push origin master

echo ""
echo "Git repo updated"

echo ""
echo "  - backing up this script"
FIREWIGSCRIPTLOC="$BACKUPROOTDIR/scripts/$SERVERNAME"

if [ ! -d $FIREWIGSCRIPTLOC ]; then
    mkdir $FIREWIGSCRIPTLOC
fi

cp /home/jon/gitConfig.sh  $FIREWIGSCRIPTLOC

git repo 位于网络中使用 Apache 和 HTTP-backend.exe(智能 HTTP 协议)的另一台机器上。如果我以“jon”的身份运行此脚本,它会成功。如果我在 crontab 中运行它,它会失败。git 使用 /home/jon/.netrc 文件进行身份验证:

machine 192.168.10.97
login gitconfig
password 1234579

来自 crontab 的日志是:

TERM environment variable not set.
  - running as user: 1000
backingup git config on betty

  - testing if home is where I think it should be!
/home/jon
  - testing if it can see netrc
machine 192.168.10.97
login gitconfig
password 1234579
got 08de5bc2b27b4940d9412256e76d5e3c3d9dbcdd
walk 08de5bc2b27b4940d9412256e76d5e3c3d9dbcdd
got be880f2d306778a538d592e7a02eb19f416612f7
got bd387e8def9f77aafa798bf53e80d949aba443e8
got 1bc1a59e12775841d4c59d77c63b8a73823138c2
walk bd387e8def9f77aafa798bf53e80d949aba443e8
Getting alternates list for http://192.168.10.97:8000/repositories/HOH-config-backups.git
got 030512237bca72faf211e0e8ec2906164eac34f6
got 9bc2f575240bc1f61ff7d69777ce1a165d06b184
got b8400f7f01429104a9d4786a6bb1a16d293e37c1
got 2403b5bf611010e0b401f776f0e23b09ce744838
got 1a27944c48269ef3608a8f2466e43402d06faac0
got b686f45b7d57af4fa8ca0d528bb85216d6247e19
Getting pack list for http://192.168.10.97:8000/repositories/HOH-config-backups.git
Getting index for pack ae881957c0f0e8c22eb6cc889a22ef78eb4ce6ff
Getting pack ae881957c0f0e8c22eb6cc889a22ef78eb4ce6ff
 which contains ff84d6d48e9326066438d167a10251218d612b3d
walk b686f45b7d57af4fa8ca0d528bb85216d6247e19
got 364e30daec17814073e668f490bb84af891fe1f7
got 23f6497e7f9b80e0d90adad73bd0407a0e5ac6ce
got 9e77c47574b5e23ea669afe0c23ab235e4917ee1
got 6654e0d328a216b3783e98c47206cb2d01b3353d
got 28821ffd437d2689ffb82c6e4b9c3f5372c95c4b
got 8c384a24f645389e4d4b08013c79e9e73a658342
got d203be0123736ee025ce20c081f1489098648dfc
got 1852603bf7709e71417d8ccec02390279d533642
got fb753a26b20b04694419fce8ecdaa8dbec105cf1
got 736028997cd84dd1c135f57e9d246674b9cd0b9d
got 7af836249e20096d0476a548d5be702a071cdd4b
got 240dc39d9db50df63073fc7927b2d002dfa0f54c
got 93abd36e3935a01011eb753b635a1a0e984bf31e
got c6269e28fecf4d8d0d98b9358aecb3acff02df44
got b0aa29432f73e64032682a351d436c24b14078ab
walk 240dc39d9db50df63073fc7927b2d002dfa0f54c
got 58fb66d9f35f8a5e32ff4683309c5f0c2a3a03c5
got 0da2def4de0565483cdbe6b87418ee2beb122e58
got 0f6a86c6f87ed52ad2ed01e5c6edd661d364930c
got 437a93d27b5bb89c739a0564a34a616e832c3ebe
got fe0385abe5c0acd8462268dac330bae00e934f1b
got 24259f8f5c5c9ee974a75fe3d1e07c02e3e20fe9
got d29f624bf1a5eceedaa86c10fee35f62747c7d04
got 0154e4c987132585ea7a92b77d02dba285512d6b

got eda8bf526567c25ee70addb2ad3c3c6aa57eac77
got 9f3d9d7262d66f9fa4f6a13b7c86199953f4bc4e
got 8e20881e19667aa22245d0598646991067455a4d
got abb1123145689b35eb19519952c71253ee45fa98
got dfeff593c79b4156ce2ce1adf043d0e80356488c
got e20c5b48b1d360e0bcf34189e3f3d2bbf23e92cc
got b13eb81cc274780322ecf786372320343926bec9
walk 8de83868b3fac748b0a55eba16c8f668ec852abb
got b5961421bbc42afe7a07cc1c8b615aba26ba74d7
got 2650ba819019df4193b482733e29ca79b29f3f2c
got b3111e1be8103e91803a97a817ed81f28025aca1
got b060be934d709684f5eb5dad3c03932a3589e864
got cf70d2043f081d7a4438e9d5a290a9f986c84060
got 80bf0f1cc836feab86d6935bb7968d8555a8d531
got da318d167920e34bc6573e4fc236249ccbbee316
got d82ac853d387b760149599e6e1ab96403f6ec672
got 0005f691d1f46550fdb4e56025f52e30a5b18cc2
Initialized empty Git repository in /tmp/git/HOH-config-backups/.git/
  - copy Configuration Folders across
Created commit 424df2f: committing any new configuration changes!
 3 files changed, 55 insertions(+), 1 deletions(-)
 create mode 100755 scripts/betty/gitConfig.sh
error: Cannot access URL http://192.168.10.97:8000/repositories/HOH-config-backups.git/, return code 22
error: failed to push some refs to 'http://192.168.10.97:8000/repositories/HOH-config-backups.git'

    Git repo updated

      - backing up this script
    cp: cannot create regular file `/mnt/backups/scripts/betty/gitConfig.sh': Permission denied

我的 crontab 是:

# m h  dom mon dow   command
04 * * * * /home/jon/gitConfig.sh > /tmp/gitconfig.log 2>&1

我通过以下方式打开它:

$crontab -e

即不是以 root 身份。

查明有关用户标识的信息:

jon@betty:~$ id
uid=1000(jon) gid=1000(jon)  groups=4(adm),20(dialout),24(cdrom),46(plugdev),109(sambashare),114(lpadmin),115(admin),1000(jon)

这是我的 $HOME/.gitconfig 文件:

[user]
    name = Jon Hawkins
    email = [email protected]

答案1

我认为问题出在您的 .gitconfig 中。您在其中设置了用户名吗?此外,如果您要备份到远程计算机,我强烈建议您使用 SSH 进行备份。

相关内容