天天看點

linux定時對git拉取和推送

準備工作

檢視目前拉取和推送位址

git remote -v
           
linux定時對git拉取和推送

更改目前位址為ssh位址,通過将ssh公鑰放到git實作免密登陸

git remote set-url ssh位址
           
linux定時對git拉取和推送

pull時告警去除

linux定時對git拉取和推送
# 3種pull政策根據自己的需求選擇
git config pull.rebase false  # 合并(預設政策)
git config pull.rebase true   # 變基
git config pull.ff only       # 僅快進
           
pull
#!/bin/bash

dir=`ls /mygit`
echo "git dir list:${dir}---"
for d in $dir
do
    if [ -d $d ]
    then
        echo "d:$d"
        cd /mygit/$d
        git pull >/mygit/$d/pull_logs.txt 2>&1
    fi
done
           
push
#!/bin/bash

localtime=`date "+%Y-%m-%d_%H:%M:%S"`
dir=`ls /mygit`
echo "git dir list:${dir}---"
for d in $dir
do
    if [ -d $d ]
    then
        echo "d:$d"
        cd /mygit/$d
        echo "-------------localtime:$localtime------------" >> /mygit/${d}/push_logs.txt
        git push >> /mygit/${d}/push_logs.txt 2>&1
        git add .
        git commit -am "cron task auto push" >> /mygit/${d}/push_logs.txt 2>&1
        git push >> /mygit/${d}/push_logs.txt 2>&1
    fi
done
           

合并

#!/usr/bin/env bash
set -x
# backup ansible hosts to git
gitDir=/home/Jenkins_install

# git pull
cd $gitDir
date "+%Y-%m-%d_%H:%M:%S" >> logs/pull.log
git pull >> logs/pull.log 2>&1

# git push
git add .
date "+%Y-%m-%d_%H:%M:%S" >> logs/push.log
git commit -am "cron task auto push" >> logs/push.log 2>&1
git push >> logs/push.log

           
定時執行腳本

因為是在docker容器裡面執行,注意時差

crontab -e
0 16 * * 1-5 sh /home/Jenkins_install/ansibleBak.sh