Created
May 29, 2015 09:12
-
-
Save adison/77868fa71c19ce6f7ec4 to your computer and use it in GitHub Desktop.
git python SAMBA bakcup script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import git | |
from git import Repo | |
from time import gmtime, strftime | |
import time | |
# 這是用來 | |
# 1. 讓 MCS, EC 端檢查檔案是否有更新,有更新則提交 | |
# 2. 讓本地 repo 進行檢查,然後push到 bit 上 | |
# | |
# 前置作業 | |
# python3.x 以上 | |
# pip install gitpython | |
# 使用可讀取mcs/EC的帳號建立網路路徑 | |
# 本地端需要先建好對應的 git repo | |
# repo 需要建立好 remote 资讯 | |
logPath = "d:\\git_backup\\jobs\\" | |
logPath += strftime("%Y-%m-%d-gitlog.txt", time.localtime(time.time())) | |
baseLog = "\n=========================================================================================\n" | |
baseLog += "=========================================================================================\n" | |
baseLog += "排程運作開始,時間 " + strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) + "\n" | |
baseLog += "=========================================================================================\n\n\n" | |
with open(logPath, "a", encoding="utf-8") as file: | |
file.write(baseLog) | |
file.close() | |
################################ | |
### 遠端區, | |
### 檢查是否有變更,有的話就 commit | |
################################ | |
# MCS repo, (路徑, <要 commit 的 branch>) | |
remoteMCS = ("DEV", "\\\\10.0.200.xx\\floder", "master") | |
remoteEC = ("EC DEV", "\\\\10.0.200.xx\\ectest", "master") | |
remoteRepoList = (remoteMCS, remoteEC) | |
for remoteRepo in remoteRepoList: | |
repo = Repo(remoteRepo[1]) | |
try: | |
newLog = "*******************************************\n" | |
newLog += "遠端 " + remoteRepo[0] + " 檔案例行性提交\n" | |
newLog += "開始時間 " + strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) + "\n" | |
newLog += "/////////**************************\\\\\\\\\\\\\\\\\n" | |
print("************************************************") | |
print("************************************************") | |
print("遠端 " + remoteRepo[0] + " 檔案例行性提交") | |
print("時間 " + strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))) | |
print("************************************************") | |
if not repo.is_dirty(): | |
# 沒有改變,加個時間記錄就好 | |
newLog += "\n--------------沒有變更--------------\n" | |
print("--------------沒有變更--------------\n") | |
else: | |
try: | |
# 開始之前, 先看有沒有 index.lock, 有則移除, 避免 commit 失敗 | |
indexFile = repo[1] + '\\.git\\index.lock' | |
try: | |
os.remove(indexFile) | |
except : | |
pass | |
# 加入所有檔案,印出變更、記錄到 log | |
repo.git.add(all=True) | |
index = repo.index | |
gitMessage = "遠端 " + remoteRepo[0] + " 檔案例行性提交\n" | |
index.commit(gitMessage) | |
newLog += gitMessage | |
newLog += "-------------------------------------------------\n變更內容:\n\n" | |
print("新增提交") | |
# 印出這次有變更的檔名 | |
print("提交完成,本次變更內容如下\n") | |
# 記錄變更內容 | |
hcommit = repo.head.commit | |
lastCommit = repo.commit('HEAD~1') | |
for diff_added in lastCommit.diff(hcommit).iter_change_type('A'): | |
newLog += "新增檔案 " + str(diff_added) + "\n=======================================================\n\n" | |
print("新增檔案 " + str(diff_added)) | |
for diff_added in lastCommit.diff(hcommit).iter_change_type('M'): | |
newLog += "修改檔案 " + str(diff_added) + "\n=======================================================\n\n" | |
print("修改檔案 " + str(diff_added)) | |
for diff_added in lastCommit.diff(hcommit).iter_change_type('D'): | |
newLog += "刪除檔案 " + str(diff_added) + "\n=======================================================\n\n" | |
print("刪除檔案 " + str(diff_added)) | |
for diff_added in lastCommit.diff(hcommit).iter_change_type('R'): | |
newLog += "檔案更名 " + str(diff_added) + "\n=======================================================\n\n" | |
print("檔案更名 " + str(diff_added)) | |
print(remoteRepo[0] + " 提交完成 ") | |
except Exception: | |
# commit 失敗 | |
info=sys.exc_info() | |
newLog += "錯誤資訊 \n" + info[0] + ":" + info[1] | |
print(remoteRepo[0] + "錯誤資訊 \n" + info[0] + ":" + info[1]) | |
raise | |
with open(logPath, "a", encoding="utf-8") as file: | |
file.write(newLog) | |
file.close() | |
print("提交完成") | |
except Exception: | |
# 不明失敗, repo 無法操作? | |
info = sys.exc_info() | |
with open(logPath, "a", encoding="utf-8") as file: | |
file.write("exceptions 錯誤 {0} \n{1} \n {2}".format(sys.stderr, str(info[0]), str(info[1]))) | |
file.close() | |
print("錯誤 {0} \n{1} \n {2}".format(sys.stderr, str(info[0]), str(info[1]))) | |
finally: | |
newLog = "\n\\\\\\\\\\\\\\\\**************************/////////\n" | |
newLog += remoteRepo[0] + "同步完成\n" | |
newLog += "完成時間 " + strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) + "\n" | |
newLog += "*******************************************\n\n\n" | |
with open(logPath, "a", encoding="utf-8") as file: | |
file.write(newLog) | |
file.close() | |
################################ | |
### 本地端 | |
### 只負責 pull, push 到遠端 | |
### 與 server 端同步,然後 push 到 bit | |
################################ | |
#本地 mcs repo | |
localMCS = ("MCS", "D:\\git_backup\\MCS_backend", "master") | |
localRepoList = (localMCS) | |
for localRepo in localRepoList: | |
newLog = "*******************************************\n" | |
newLog += "本地" + localRepo[0] + " 開始執行同步工作\n" | |
newLog += "開始時間 " + strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) + "\n" | |
newLog += "/////////**************************\\\\\\\\\\\\\\\\\n" | |
# newLog += "*******************************************\n\n" | |
print("************************************************") | |
print("************************************************") | |
print("本地 " + localRepo[0] + " 開始執行同步工作") | |
print("時間 " + strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))) | |
print("************************************************") | |
# 建立 repo | |
repo = Repo(localRepo[1]) | |
try: | |
# 取得 server 端 origin, 取得最新的 index | |
originRepo = repo.remotes.origin | |
originInfo = originRepo.fetch() | |
originCommit = originInfo[0].commit | |
localCommit = repo.head.commit | |
# 檢查本地與遠端是否有差別,判斷是否要進行同步 | |
if (str(originCommit)==str(localCommit)): | |
# 沒事 | |
newLog += "\n-----------------沒有變更------------------\n" | |
print("--------------沒有變更--------------\n") | |
else: | |
# 撈取變更,推到 bit 並記錄 | |
print(" ready for pull " + localRepo[1] + ": " + str(repo.active_branch)) | |
originRepo.pull(localRepo[2]) | |
print(" end for pull" + localRepo[1] + ": " + str(repo.active_branch)) | |
# 推送 | |
try: | |
bit = repo.remotes.bit | |
print(" ready to push to bit") | |
bit.push(localRepo[2]) | |
print(" end pushing bit") | |
newLog += "\n-------------------------------------------\n" + localRepo[0] + " bit 儲存庫/" + localRepo[2] + ",遠端同步完成\n-------------------------------------------\n\n" | |
print(localRepo[0] + " bit 儲存庫/" + localRepo[2] + ",遠端同步完成") | |
except: | |
newLog += "\n-------------------------------------------\n" + localRepo[0] + " 無 bit 儲存庫,不進行遠端同步\n-------------------------------------------\n\n" | |
print(localRepo[0] + " 無 bit 儲存庫,不進行遠端同步") | |
# 記錄變更內容 | |
hcommit = repo.head.commit | |
lastCommit = repo.commit('HEAD~1') | |
# 印出這次變更 | |
newLog += "最後一次提交為 " + hcommit.author.name + ", " + hcommit.author.email +"\n" + hcommit.message + "\n" | |
newLog += "-------------------------------------------------\n變更內容:\n\n" | |
print("上傳完成,本次變更內容如下\n" + hcommit.message) | |
for diff_added in lastCommit.diff(hcommit).iter_change_type('A'): | |
newLog += "新增檔案 " + str(diff_added) + "\n=======================================================\n\n" | |
print("新增檔案 " + str(diff_added)) | |
for diff_added in lastCommit.diff(hcommit).iter_change_type('M'): | |
newLog += "修改檔案 " + str(diff_added) + "\n=======================================================\n\n" | |
print("修改檔案 " + str(diff_added)) | |
for diff_added in lastCommit.diff(hcommit).iter_change_type('D'): | |
newLog += "刪除檔案 " + str(diff_added) + "\n=======================================================\n\n" | |
print("刪除檔案 " + str(diff_added)) | |
for diff_added in lastCommit.diff(hcommit).iter_change_type('R'): | |
newLog += "檔案更名 " + str(diff_added) + "\n=======================================================\n\n" | |
print("檔案更名 " + str(diff_added)) | |
newLog += "-------------------------------------------------" | |
print(localRepo[0] + " 提交完成 ") | |
with open(logPath, "a", encoding="utf-8") as file: | |
file.write(newLog) | |
file.close() | |
except Exception: | |
info = sys.exc_info() | |
with open(logPath, "a", encoding="utf-8") as file: | |
file.write("exceptions 錯誤 {0} \n{1} \n {2}".format(sys.stderr, str(info[0]), str(info[1]))) | |
file.close() | |
print("錯誤 {0} \n{1} \n {2}".format(sys.stderr, str(info[0]), str(info[1]))) | |
finally: | |
newLog = "\n\\\\\\\\\\\\\\\\**************************/////////\n" | |
newLog += localRepo[0] + " 本地端同步完成\n" | |
newLog += "完成時間 " + strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) + "\n" | |
newLog += "*******************************************\n\n\n" | |
print("************************************************") | |
print(" " + localRepo[0] + " 本地端同步完成 has end sync") | |
print("************************************************") | |
print("************************************************\n") | |
with open(logPath, "a", encoding="utf-8") as file: | |
file.write(newLog) | |
file.close() | |
baseLog = "=========================================================================================\n" | |
baseLog += " 排程運作結束,時間 " + strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) + "\n" | |
baseLog += "=========================================================================================\n" | |
baseLog += "=========================================================================================\n" | |
with open(logPath, "a", encoding="utf-8") as file: | |
file.write(baseLog) | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment