From eff3bc22464fa1978a862d63a47353254ce64234 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 2 Aug 2013 00:18:43 +0200 Subject: [PATCH 1/6] added dependency section in readme --- README.markdown | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.markdown b/README.markdown index 42e1d4e..64a252e 100644 --- a/README.markdown +++ b/README.markdown @@ -10,6 +10,15 @@ Description GitHub-Backup makes a local backup copy of all of a github user's (or github organization's) repositories. +Dependencies +---- + +GitHub-Backup requires `pygithub3` a Python library for the GitHub API v3. + +Installation is simple with + + pip install pygithub3 + Usage ----- ./github-backup.py USERNAME BACKUPDIR [-c|--cron] [-h|--help] From ee763dfcb612d381780a6fbb31caa58cf0e56186 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 2 Aug 2013 00:43:49 +0200 Subject: [PATCH 2/6] added support for bare mirror repositories --- github-backup.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/github-backup.py b/github-backup.py index 2b36a6d..b04eb4a 100755 --- a/github-backup.py +++ b/github-backup.py @@ -25,13 +25,14 @@ def init_parser(): """ set up the argument parser """ - parser = ArgumentParser( - description="makes a backup of all of a github user's repositories") + + parser = ArgumentParser(description="makes a backup of all of a github user's repositories") + parser.add_argument("username", help="A Github username") - parser.add_argument("backupdir", - help="The folder where you want your backups to go") - parser.add_argument("-c","--cron", help="Use this when running from a cron job", - action="store_true") + parser.add_argument("backupdir", help="The folder where you want your backups to go") + parser.add_argument("-c","--cron", help="Use this when running from a cron job", action="store_true") + parser.add_argument("-m","--mirror", help="Create a bare mirror", action="store_true") + return parser @@ -44,11 +45,22 @@ def process_repo(repo, args): if not args.cron: print("Processing repo: %s"%(repo.full_name)) - if os.access('%s/%s/.git'%(args.backupdir,repo.name),os.F_OK): + config = "%s/%s/%s"%(args.backupdir, repo.name, "config" if args.mirror else ".git/config") + + if os.access(config,os.F_OK): if not args.cron: print("Repo already exists, let's try to update it instead") - os.system('cd %s/%s;git pull %s'%(args.backupdir, repo.name, git_args)) + os.system("cd %s/%s"%(args.backupdir, repo.name)) + if args.mirror: + git_args += " --prune" + os.system("git fetch %s"%(git_args,)) + else: + os.system("git pull %s"%(git_args,)) + else: # Repo doesn't exist, let's clone it + if args.mirror: + git_args += " --mirror" + os.system('git clone %s %s %s/%s'%(git_args, repo.git_url, args.backupdir, repo.name)) if __name__ == "__main__": From b68463bc6a04d93305033da05e031504a3a6ac7e Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 2 Aug 2013 00:50:54 +0200 Subject: [PATCH 3/6] added support for extra git arguments --- github-backup.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/github-backup.py b/github-backup.py index b04eb4a..918dee7 100755 --- a/github-backup.py +++ b/github-backup.py @@ -32,15 +32,14 @@ def init_parser(): parser.add_argument("backupdir", help="The folder where you want your backups to go") parser.add_argument("-c","--cron", help="Use this when running from a cron job", action="store_true") parser.add_argument("-m","--mirror", help="Create a bare mirror", action="store_true") + parser.add_argument("-g","--git", help="Pass extra arguments to git", default="") return parser def process_repo(repo, args): if args.cron: - git_args = "-q" - else: - git_args = "" + args.git += "--quit" if not args.cron: print("Processing repo: %s"%(repo.full_name)) @@ -50,18 +49,20 @@ def process_repo(repo, args): if os.access(config,os.F_OK): if not args.cron: print("Repo already exists, let's try to update it instead") + os.system("cd %s/%s"%(args.backupdir, repo.name)) + if args.mirror: - git_args += " --prune" - os.system("git fetch %s"%(git_args,)) + args.git += " --prune" + os.system("git fetch %s"%(args.git,)) else: - os.system("git pull %s"%(git_args,)) + os.system("git pull %s"%(args.git,)) else: # Repo doesn't exist, let's clone it if args.mirror: - git_args += " --mirror" + args.git += " --mirror" - os.system('git clone %s %s %s/%s'%(git_args, repo.git_url, args.backupdir, repo.name)) + os.system('git clone %s %s %s/%s'%(args.git, repo.git_url, args.backupdir, repo.name)) if __name__ == "__main__": main() From bd8abb797c0f8c0556308f8b25e7af762eb4cacb Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 2 Aug 2013 01:00:43 +0200 Subject: [PATCH 4/6] added support for path suffix: this might be usefull for bare repo's which mostly ending with '.git' --- github-backup.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/github-backup.py b/github-backup.py index 918dee7..b130bb1 100755 --- a/github-backup.py +++ b/github-backup.py @@ -32,7 +32,8 @@ def init_parser(): parser.add_argument("backupdir", help="The folder where you want your backups to go") parser.add_argument("-c","--cron", help="Use this when running from a cron job", action="store_true") parser.add_argument("-m","--mirror", help="Create a bare mirror", action="store_true") - parser.add_argument("-g","--git", help="Pass extra arguments to git", default="") + parser.add_argument("-g","--git", help="Pass extra arguments to git", default="", metavar="ARGS") + parser.add_argument("-s", "--suffix", help="Add suffix to repository directory names", default="") return parser @@ -44,13 +45,14 @@ def process_repo(repo, args): if not args.cron: print("Processing repo: %s"%(repo.full_name)) - config = "%s/%s/%s"%(args.backupdir, repo.name, "config" if args.mirror else ".git/config") + dir = "%s/%s"%(args.backupdir, repo.name + args.suffix) + config = "%s/%s"%(dir, "config" if args.mirror else ".git/config") if os.access(config,os.F_OK): if not args.cron: print("Repo already exists, let's try to update it instead") - os.system("cd %s/%s"%(args.backupdir, repo.name)) + os.system("cd %s"%(dir,)) if args.mirror: args.git += " --prune" @@ -62,7 +64,7 @@ def process_repo(repo, args): if args.mirror: args.git += " --mirror" - os.system('git clone %s %s %s/%s'%(args.git, repo.git_url, args.backupdir, repo.name)) + os.system('git clone %s %s %s'%(args.git, repo.git_url, dir)) if __name__ == "__main__": main() From 3742232295e3d7d4a83dd44d8e94e2a3201ecc50 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 2 Aug 2013 01:21:55 +0200 Subject: [PATCH 5/6] some code cleanup --- github-backup.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/github-backup.py b/github-backup.py index b130bb1..7080509 100755 --- a/github-backup.py +++ b/github-backup.py @@ -1,18 +1,25 @@ #!/usr/bin/env python -# Author: Anthony Gargiulo (anthony@agargiulo.com) -# Created Fri Jun 15 2012 +""" +Authors: Anthony Gargiulo (anthony@agargiulo.com) + Steffen Vogel (post@steffenvogel.de) + +Created: Fri Jun 15 2012 +""" from pygithub3 import Github from argparse import ArgumentParser import os + def main(): - # A sane way to handle command line args. - # Now actually store the args parser = init_parser() args = parser.parse_args() + # Process args + if args.cron: + args.git += "--quit" + # Make the connection to Github here. gh = Github() @@ -21,6 +28,7 @@ def main(): for repo in user_repos: process_repo(repo, args) + def init_parser(): """ set up the argument parser @@ -39,32 +47,39 @@ def init_parser(): def process_repo(repo, args): - if args.cron: - args.git += "--quit" - if not args.cron: print("Processing repo: %s"%(repo.full_name)) dir = "%s/%s"%(args.backupdir, repo.name + args.suffix) config = "%s/%s"%(dir, "config" if args.mirror else ".git/config") - if os.access(config,os.F_OK): + if os.access(config, os.F_OK): if not args.cron: print("Repo already exists, let's try to update it instead") + update_repo(repo, dir, args) + else: + if not args.cron: + print("Repo doesn't exists, lets clone it") + clone_repo(repo, dir, args) + +def clone_repo(repo, dir, args): + if args.mirror: + args.git += " --mirror" + + os.system('git clone %s %s %s'%(args.git, repo.git_url, dir)) + + +def update_repo(repo, dir, args): os.system("cd %s"%(dir,)) + # GitHub => Local if args.mirror: args.git += " --prune" os.system("git fetch %s"%(args.git,)) else: os.system("git pull %s"%(args.git,)) - else: # Repo doesn't exist, let's clone it - if args.mirror: - args.git += " --mirror" - - os.system('git clone %s %s %s'%(args.git, repo.git_url, dir)) if __name__ == "__main__": main() From 84e23c696caac801def38f15de830849151a221c Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 2 Aug 2013 01:29:37 +0200 Subject: [PATCH 6/6] fixed small typo --- github-backup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github-backup.py b/github-backup.py index 7080509..a6ccec3 100755 --- a/github-backup.py +++ b/github-backup.py @@ -18,7 +18,7 @@ def main(): # Process args if args.cron: - args.git += "--quit" + args.git += "--quiet" # Make the connection to Github here. gh = Github()