{"id":21478930,"url":"https://github.com/dqzboy/back_sync_github","last_synced_at":"2025-07-15T11:31:21.618Z","repository":{"id":259896428,"uuid":"879754251","full_name":"dqzboy/BACK_SYNC_GITHUB","owner":"dqzboy","description":"🔥🔥自动化备份重要数据到GitHub仓库","archived":false,"fork":false,"pushed_at":"2025-06-25T03:52:59.000Z","size":51,"stargazers_count":23,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-25T04:34:09.021Z","etag":null,"topics":["backup-script"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dqzboy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-10-28T13:47:20.000Z","updated_at":"2025-06-25T03:53:03.000Z","dependencies_parsed_at":"2025-06-25T04:28:31.528Z","dependency_job_id":"f46df08d-bf43-4da9-bd3a-3590d5aafa57","html_url":"https://github.com/dqzboy/BACK_SYNC_GITHUB","commit_stats":null,"previous_names":["dqzboy/back_sync_github"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dqzboy/BACK_SYNC_GITHUB","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dqzboy%2FBACK_SYNC_GITHUB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dqzboy%2FBACK_SYNC_GITHUB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dqzboy%2FBACK_SYNC_GITHUB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dqzboy%2FBACK_SYNC_GITHUB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dqzboy","download_url":"https://codeload.github.com/dqzboy/BACK_SYNC_GITHUB/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dqzboy%2FBACK_SYNC_GITHUB/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265431527,"owners_count":23764029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backup-script"],"created_at":"2024-11-23T11:20:09.839Z","updated_at":"2025-07-15T11:31:21.603Z","avatar_url":"https://github.com/dqzboy.png","language":"Shell","readme":"# BACK_SYNC_GITHUB\n在服务器中通过脚本并结合crontab定时备份数据到GitHub仓库，适用于个人\n\n---\n\u003ctable style=\"width: 100%; table-layout: fixed;\"\u003e\n    \u003ctr\u003e\n      \u003ctd width=\"50%\" align=\"center\"\u003e\u003cb\u003e海外服务器\u003c/b\u003e\u003c/td\u003e\n      \u003ctd width=\"50%\" align=\"center\"\u003e\u003cb\u003e海外服务器\u003c/b\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd width=\"50%\" align=\"center\"\u003e\n            \u003ca href=\"https://dqzboy.github.io/proxyui/racknerd\" target=\"_blank\"\u003e\n                \u003cimg src=\"https://cdn.jsdelivr.net/gh/dqzboy/Images/dqzboy-proxy/1.png?raw=true\" \n                     alt=\"RackNerd\" \n                     style=\"width: 100%; height: auto; max-width: 400px; object-fit: contain;\"\u003e\n            \u003c/a\u003e\n        \u003c/td\u003e\n        \u003ctd width=\"50%\" align=\"center\"\u003e\n            \u003ca href=\"https://dqzboy.github.io/proxyui/CloudCone\" target=\"_blank\"\u003e\n                \u003cimg src=\"https://cdn.jsdelivr.net/gh/dqzboy/Images/dqzboy-proxy/2.png?raw=true\" \n                     alt=\"CloudCone\" \n                     style=\"width: 100%; height: auto; max-width: 400px; object-fit: contain;\"\u003e\n            \u003c/a\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n## 脚本逻辑：\n#### （1）克隆仓库或更新仓库\n- 克隆仓库：如果备份目录不存在，脚本会使用 `git clone` 克隆指定的 GitHub 仓库。\n- 更新仓库：如果备份目录已存在，脚本会通过 `git pull` 更新本地仓库，确保本地仓库与远程仓库同步。\n  \n#### （2）备份文件处理\n- 清理旧文件：清空服务器特定的备份目录，确保只保留最新的备份。\n- 备份文件复制：遍历指定的文件和目录（如脚本中的示例为： `/etc/passwd` 和 `/etc/nginx/conf.d`），并将其复制到服务器特定的备份目录中。\n- 检查复制结果：如果复制失败，脚本会显示警告信息。\n#### （3）提交和推送更改到 GitHub\n- 添加更改到 Git：将备份目录下的所有更改（包括删除的文件）添加到 Git 的暂存区。\n- 提交更改：如果有更改（通过 `git diff --cached --quiet` 检查），则提交更改，提交信息包括当前时间。\n- 推送到远程仓库：将本地的备份更改推送到 GitHub 上的远程仓库，确保数据备份同步到远程仓库。\n- 多台服务器同时备份：推送被拒绝时自动处理，自动执行git fetch和git merge，然后重试推送，最大重试次数为3次(`max_retries=3`)。\n\n## 前提条件：\n  - **1、** 创建GitHub仓库，设置为私有\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003ctd width=\"50%\" align=\"center\"\u003e\u003cimg src=\"https://github.com/user-attachments/assets/f4b750c3-b4cd-48e0-8bc3-2313d45726dd\"?raw=true\"\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n\n  \n  - **2、** 创建GitHubToken，给个pull、push权限即可\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003ctd width=\"50%\" align=\"center\"\u003e\u003cimg src=\"https://github.com/user-attachments/assets/fc51040f-a7ea-4b9e-bc7e-c35469849674\"?raw=true\"\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003ctd width=\"50%\" align=\"center\"\u003e\u003cimg src=\"https://github.com/user-attachments/assets/bf54121f-ccd7-4058-84fb-25f3a526e679\"?raw=true\"\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003ctd width=\"50%\" align=\"center\"\u003e\u003cimg src=\"https://github.com/user-attachments/assets/1e38b9d1-5da3-4056-b967-a5fbdaa93e39\"?raw=true\"\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n**注意**：把Toekn保留下来，只会出现一次。下面修改脚本变量时需要使用到！\n\n## 步骤流程：\n### （1）下载脚本到你的服务器\n```bash\nwget https://raw.githubusercontent.com/dqzboy/BACK_SYNC_GITHUB/refs/heads/main/git_sync_backup.sh\n```\n\n### （2）修改脚本中的变量\n- 根据脚本中的注释，修改变量。主要修改的变量如下\n  - `GIT_USER=\"your_username\"`      请替换为你的GitHub用户名\n  - `GIT_TOKEN=\"GITHUB_TOKEN\"`      请替换为你的GitHub Token\n  - `REPO_NAME=\"your_repository\"`   请替换为你的GitHub仓库名称\n  - `BACKUP_SOURCES`                需要备份的目录路径或者文件路径\n### （3）手动执行脚本测试\n```shell\nchmod +x git_sync_backup.sh\n./git_sync_backup.sh\n```\n\n### （4）添加定时任务\n```shell\n# 定义定时任务\ncrontab -e\n\n# 例如：每天2点执行备份\n0 2 * * * /your_path/git_sync_backup.sh\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdqzboy%2Fback_sync_github","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdqzboy%2Fback_sync_github","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdqzboy%2Fback_sync_github/lists"}