小心滥用GitHub被封,尽量使用GitLab
使用 git config --global credential.helper store
只用第一次验证后便永久记住GitHub账号, 适合Linux上使用HTTPS链接GitHub库,
Windows有自己的密码管理器不用他 使用
git config --global --unset credential.helper
移除设置,
删除持久化密码
脚本与 AList 联合使用
subprocess 会把列表里的每个部分用 ““/’’ 引号 包起来,
所以路径有空格时用 subprocess
无需特殊处理
subprocess 默认将输出打印到控制台 ; 可以更改方法中
stdout=
参数控制输出 subprocess.run
是阻塞的,会一直卡着等待程序运行 subprocess.Popen
后台执行,不阻塞
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
|
from github import Github
from github import Auth import os import shutil import time import subprocess
""" /etc/crontab 中添加定时任务 51 1 ? * 2 root python "/root/py_script/用GitHub API实现备份自己GitHub上所有仓库的代码.py" >/dev/null 2>&1 每周二 1:51 执行脚本 """
access_token = "your_github_access_token"
user_name = "jlower"
auth = Auth.Token(access_token)
g = Github(auth=auth)
user = g.get_user()
repos = user.get_repos()
backup_folder = "/root/Git Repository backup" zip_output_folder = "/etc/alist/storage/_Git Repository backup"
os.makedirs(backup_folder, exist_ok=True)
repo_list = [] repo_skip_list = ["JavaStart-save"] for repo in repos: if repo.name in repo_skip_list: continue repo_path = os.path.join(backup_folder, repo.name) repo_list.append(repo.name) print("正在处理: ", repo.name) if os.path.exists(repo_path): os.chdir(repo_path) subprocess.run(["git", "pull"]) else: subprocess.run(["git", "clone", repo.clone_url, repo_path]) time.sleep(0.5)
for dir_name in os.listdir(backup_folder): dir_path = os.path.join(backup_folder, dir_name) if dir_name not in repo_list: if os.path.isdir(dir_path): shutil.rmtree(dir_path)
for filename in os.listdir(zip_output_folder): file_path = os.path.join(zip_output_folder, filename) os.remove(file_path)
files = os.listdir(backup_folder) zip_list = [] for file in files: file_path = os.path.join(backup_folder, file) if os.path.isdir(file_path): zip_output_path = os.path.join(zip_output_folder, file) zip_output_path = zip_output_path + ".zip" zip_list.append(file + ".zip") subprocess.run(["zip", "-r", "-q", zip_output_path, file_path])
print("所有仓库已备份或更新到本地文件夹!")
|