-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub-backup-script.py
114 lines (86 loc) · 3.92 KB
/
github-backup-script.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
110
111
112
113
114
import requests
import datetime
import os
import shutil
import urllib.request
import time
#Modify these parameters with your own.
private_repository_url = " "
public_repository_url = " "
private_backup_folder = r" "
public_backup_folder = r" "
token = ""
# Downloading repositories function
# It is important to keep your token safe and don't share it with anyone.
def download_repo(repo_url, dest_folder, token, branch="main"):
# Make a GET request to the GitHub API to download the repository as a zip file
headers = {"Authorization": f"token {token}"}
zip_url = f"{repo_url}/archive/{branch}.zip"
response = requests.get(zip_url, headers=headers, stream=True)
# If the response status code is not 200, raise an error
if response.status_code != 200:
raise ValueError(f"Failed to download repository. Status code: {response.status_code}")
# Create the destination folder if it does not exist
os.makedirs(dest_folder, exist_ok=True)
# Save the contents of the zip file to a local file with the current date in the filename
repo_name = repo_url.split("/")[-1]
now = datetime.datetime.now()
filename = f"{repo_name}-{branch}_{now.strftime('%Y-%m-%d_%Hh_%Mm')}_backup.zip"
filepath = os.path.join(dest_folder, filename)
with open(filepath, "wb") as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)
# Close the response to free up resources
response.close()
print(f"Repository downloaded to {filepath}")
# Removing older backups function
def delete_old_files(folder_path, hours_del):
# Get the current time
now = datetime.datetime.now()
# Calculate the time threshold for deleting files
threshold = now - datetime.timedelta(hours=hours_del)
# Loop over all files in the folder
for filename in os.listdir(folder_path):
filepath = os.path.join(folder_path, filename)
# Check if the file is a zip file and is older than the threshold
if filename.endswith(".zip") and os.path.getmtime(filepath) < threshold.timestamp():
# Delete the file
os.remove(filepath)
print(f"Deleted old file: {filename}")
# Check if the device is connected to the internet
def check_internet_connection():
try:
urllib.request.urlopen("https://www.google.com")
return True
except:
return False
def main():
connected = False
# Loop while user is not connected to the internet
while not connected:
if check_internet_connection():
# Uncomment the next line to start after an user confirmation:
# input('Press any button to start..')
# You can comment/delete one of the download_repo functions if you don't need it
# Set the parammeters in the secrets.ini file
print(f"Downloading private repository...")
download_repo(private_repository_url, private_backup_folder, token)
print(f"Private repository downloaded successfully...")
print(f"Downloading public repository...")
# Set the parammeters in the secrets.ini file
download_repo(public_repository_url, public_backup_folder, token)
print(f"Public repository downloaded successfully...")
# Execute the backup-cleaner function
# Change the second parameter on the next functions to change the deleting frequency. Defalut: 48h
print(f"Deleting old repositories...")
delete_old_files(private_backup_folder, 48)
delete_old_files(public_backup_folder, 48)
print(f"Old backups deleted successfully...")
print(f"All work done here!!!")
connected = True
#If not connected to the internet, returns error message
else:
print("Device is not connected to the internet. Retrying in 10 seconds...")
time.sleep(10)
if __name__ == "__main__":
main()