Script to backup all repos in a GitHub Organisation
Clone or update every repository in a GitHub organization. Supports passing a custom SSH key, logs to a timestamped file, and rotates old logs (keeps N days).
NOTE: If using ssh, make sure ssh key is chmod 400
python github_org_backup.py --org ORG_NAME [options]
python github_org_backup.py --org my-org --ssh-key ~/.ssh/id_backup --log-dir /var/log/github-backups --retention-days 30
With GITHUB_TOKEN:
export GITHUB_TOKEN="github_pat_xxx"; python3 /home/backup/code/github_org_backup.py --org <org-name> --token $GITHUB_TOKEN --ssh-key /home/backup/code/key --log-dir /home/backup/code/logs/ --retention-days 30
Put the script somewhere permanent and make it executable:
mkdir -p /opt/github-backups/
mv github_org_backup.py /opt/github-backups/github_org_backup.py
chmod +x /opt/github-backups/github_org_backup.py
Create a systemd service file, e.g. /etc/systemd/system/github-org-backup.service
[Unit]
Description=GitHub Organization Backup
[Service]
Type=oneshot
# Run as backup user (change as needed)
User=backup
Group=backup
# Working directory where repos are stored
WorkingDirectory=/home/backup/code
# Command to run the script
ExecStart=/usr/bin/python3 /opt/github-backups/github_org_backup.py \
--org my-org \
--mode ssh \
--ssh-key /home/backup/.ssh/id_backup \
--update-submodules \
--log-dir /var/log/github-backups \
--retention-days 30
# Make sure environment is clean
Environment="PATH=/usr/bin:/bin"
Create a systemd timer, e.g. /etc/systemd/system/github-org-backup.timer
[Unit]
Description=Run GitHub Organization Backup nightly
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
[Install]
WantedBy=timers.target
This runs the backup every day at 02:30.
Persistent=true means if the machine was off at 02:30, the job runs as soon as possible after boot.
sudo systemctl daemon-reload
sudo systemctl enable --now github-org-backup.timer
Check it is enabled:
systemctl list-timers --all
And after it has run, check the logs:
journalctl -u github-org-backup.service