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__":