-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitbackup.py
109 lines (88 loc) · 3.57 KB
/
gitbackup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import git
import shutil
from pytz import timezone
from datetime import datetime
from files import readJson, writeJson,setupFiles
from gitcredentials import username, password, repoURL
from analyze import merge
defaultPath = os.path.join(__file__.replace("gitbackup.py",""), 'data')
def initializeRepo(directory=defaultPath):
if not os.path.isdir(os.path.join(directory, '.git')): #Checks if the directory is already a repo
print(f"Initializing {directory} as a git repository...")
repo = git.Repo.init(directory)
else:
repo = git.Repo(directory)
if repo.head.ref.name != 'main':
repo.git.branch('-M', 'main')
print("added Main branch to repo")
setRemote(repo)
return repo
def setRemote(repo, remoteURL=repoURL, remoteName='origin'):
if remoteName not in repo.remotes:
remote = repo.create_remote(remoteName, remoteURL)
else:
remote = repo.remotes[remoteName]
remote.set_url(remoteURL)
print(f"set {remoteName} as remote ")
return remote
def cloneRepo(remoteURL=repoURL, localDir=defaultPath):
try:
# Modify the URL to include authentication credentials
if 'http://' in remoteURL:
remoteURL = remoteURL.replace('http://', f'http://{username}:{password}@')
elif 'https://' in remoteURL:
remoteURL = remoteURL.replace('https://', f'https://{username}:{password}@')
print(f"Cloning repository from {remoteURL} into {localDir}")
repo = git.Repo.clone_from(remoteURL, localDir)
print("Repository cloned successfully.")
return repo
except Exception as e:
print(f"Error occurred while cloning the repository: {e}")
def pushChanges(repo, remoteName='origin'):
pdt_zone = timezone("America/Los_Angeles")
currentTime = datetime.now(pdt_zone).strftime('%Y-%m-%d %H:%M:%S %Z')
try:
repo.git.add(A=True) # Add all files
repo.index.commit(f"Backup at {currentTime}")
print("Added and committed files")
except Exception as e:
print(f"Error occurred while adding and committing: {e}")
try:
remote = repo.remotes[remoteName]
remoteURL = remote.url
if 'http://' in remoteURL:
remoteURL = remoteURL.replace('http://', f'http://{username}:{password}@')
elif 'https://' in remoteURL:
remoteURL = remoteURL.replace('https://', f'https://{username}:{password}@')
remote.set_url(remoteURL)
print(f"Pushing changes to {remoteName}...")
print(repo.head.ref.name)
if repo.head.ref.tracking_branch() is None:
repo.git.push('--set-upstream', remoteName, repo.head.ref.name)
else:
remote.push()
print("Changes pushed successfully.")
except Exception as e:
print(f"Error occurred while pushing changes: {e}")
def download():
setupFiles()
shutil.move('data/history.json','temp/history.json')
shutil.move('data/logs.json','temp/logs.json')
shutil.rmtree('data')
print("moved existing files to temp dir")
cloneRepo()
setupFiles()
for jsonFile in ['history.json', 'logs.json']:
data1 = readJson(f'data/{jsonFile}')
data2 = readJson(f'temp/{jsonFile}')
mergedData = merge(data1, data2)
writeJson(f'data/{jsonFile}', mergedData)
print(f"Merged {jsonFile} from remote and local")
try:
os.remove(f'temp/{jsonFile}')
except Exception as e:
print(f'error removing {jsonFile} from temp: {e}')
def upload():
Repo = initializeRepo()
pushChanges(Repo)