|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import argparse |
| 5 | + |
| 6 | + |
| 7 | +""" |
| 8 | +-r|--remove|--remove-folder --> Removes the backup folder after the backup |
| 9 | +-s|--scratch|--from-scratch --> Empties the backupb folder previous to start the backup |
| 10 | +-c|--compress --> Produces a compressed file with the backup folders |
| 11 | +-p|--compressed-path --> custom folder where the compressed file will be stored. Needs value., Implies -c |
| 12 | +-h|--hierarchy|--hierarchy-backup --> Clone repos keeping organization and user hierarchy in backup folder |
| 13 | +-j|--json|--generate-json --> generates a json of the of the backup up folders and repos |
| 14 | +-J|--json-path --> custom folder where the json report will be stored |
| 15 | +-b|--backup|--backup-path --> custom folder where all the clones will be done. Needs value |
| 16 | +-l|--gitlab -> Custom user that will be backed from gitlab. needs one or more value |
| 17 | +-f|--force --> Does not ask confirmation for destructive actions |
| 18 | +""" |
| 19 | + |
| 20 | + |
| 21 | +def main(): |
| 22 | + parser = argparse.ArgumentParser(description="Backup your git repos in your local filesystem") |
| 23 | + |
| 24 | + # Optional arguments |
| 25 | + parser.add_argument("-r", "--remove", "--remove-folder", |
| 26 | + action="store_true", |
| 27 | + help="Removes the backup folder after the backup.", |
| 28 | + default=False, |
| 29 | + dest="remove_backup_folder_afterwards") |
| 30 | + parser.add_argument("-s", "--scratch", "--from-scratch", |
| 31 | + action="store_true", |
| 32 | + help="Empties the backup folder before starting the backup.", |
| 33 | + default=False, |
| 34 | + dest="empty_backup_folder_first") |
| 35 | + parser.add_argument("-c", "--compress", |
| 36 | + action="store_true", |
| 37 | + help="Produces a compressed file with the backup folders.", |
| 38 | + default=False, |
| 39 | + dest="produce_compressed") |
| 40 | + parser.add_argument("-p", "--compressed-path", |
| 41 | + type=str, |
| 42 | + metavar="PATH", |
| 43 | + help="Custom path where the compressed file will be stored. Implies -c.", |
| 44 | + default="backup/backup.tar.gz", |
| 45 | + dest="compressed_path") |
| 46 | + parser.add_argument("-h", "--hierarchy", "--hierarchy-backup", "--keep-hierarchy", |
| 47 | + action="store_true", |
| 48 | + help="Clone repos keeping organizations and user hierarchy in the backup folder.", |
| 49 | + default=False, |
| 50 | + dest="reflect_hierarchy") |
| 51 | + parser.add_argument("-j", "--json", "--generate-json", "--produce-json", |
| 52 | + action="store_true", |
| 53 | + help="Generates a JSON report of the backup folders and repos.", |
| 54 | + default=False, |
| 55 | + dest="produce_json") |
| 56 | + parser.add_argument("-J", "--json-path", |
| 57 | + type=str, |
| 58 | + metavar="PATH", |
| 59 | + help="Custom path where the JSON report will be stored. Implies -j.", |
| 60 | + default="backup/backup.json", |
| 61 | + dest="json_path") |
| 62 | + parser.add_argument("-b", "--backup", "--backup-path", |
| 63 | + type=str, |
| 64 | + metavar="PATH", |
| 65 | + help="Custom folder where all the clones will be done. Requires a value.", |
| 66 | + default="backup", |
| 67 | + dest="backup_path") |
| 68 | + parser.add_argument("-l", "--gitlab", |
| 69 | + type=str, |
| 70 | + metavar="GITLAB_USERS", |
| 71 | + nargs="+", |
| 72 | + help="One or more GitLab usernames to back up. Separate multiple usernames with spaces.", |
| 73 | + dest="gitlab_users") |
| 74 | + parser.add_argument("-f", "--force", |
| 75 | + action="store_true", |
| 76 | + help="Does not ask confirmation for destructive actions.", |
| 77 | + default=False, |
| 78 | + dest="is_forced") |
| 79 | + parser.add_argument("-v", "--verbose", |
| 80 | + action="store_true", |
| 81 | + help="Enable verbose output during backup.", |
| 82 | + default=False, |
| 83 | + dest="is_verbose") |
| 84 | + parser.add_argument("github_users", |
| 85 | + type=str, |
| 86 | + metavar="GITHUB_USERS", |
| 87 | + nargs="*", # Zero or more GitHub usernames |
| 88 | + help="One or more GitHub usernames to back up. Separate multiple usernames with spaces. " |
| 89 | + "Positional argument", |
| 90 | + dest="github_users") |
| 91 | + |
| 92 | + args = parser.parse_args() |
| 93 | + |
| 94 | + # Check if at least one of the user lists is provided (GitHub or GitLab) |
| 95 | + if not args.github and not args.gitlab: |
| 96 | + parser.error("You must provide at least one GitHub or GitLab user.") |
| 97 | + |
| 98 | + # Compressed path implies -c |
| 99 | + if args.compressed_path: |
| 100 | + args.produce_compressed = True |
| 101 | + |
| 102 | + # JSON path implies -j |
| 103 | + if args.json_path: |
| 104 | + args.produce_json = True |
| 105 | + |
| 106 | + print(args) |
| 107 | + |
| 108 | + print("Backup summary:") |
| 109 | + |
| 110 | + if args.github_users: |
| 111 | + print("Backing up the following GitHub users:") |
| 112 | + for user in args.github_users: |
| 113 | + print(f" - {user}") |
| 114 | + |
| 115 | + if args.gitlab: |
| 116 | + print("Backing up the following GitLab users:") |
| 117 | + for user in args.gitlab: |
| 118 | + print(f" - {user}") |
| 119 | + |
| 120 | + if args.backup_path: |
| 121 | + print("Backup folder: " + args.backup_path) |
| 122 | + |
| 123 | + if args.produce_compressed: |
| 124 | + print("Compressed backup path: " + args.compressed_path) |
| 125 | + |
| 126 | + if args.produce_json: |
| 127 | + print("JSON summary path: " + args.json_path) |
| 128 | + |
| 129 | + if args.empty_backup_folder_first: |
| 130 | + print("Empty backup folder before performing backup (start from scratch): " + args.empty_backup_folder_first) |
| 131 | + |
| 132 | + if args.remove_backup_folder_afterwards: |
| 133 | + print("Remove backup folder after performing backup: " + args.remove_backup_folder_afterwards) |
| 134 | + |
| 135 | + if args.reflect_hierarchy: |
| 136 | + print("Produces a hierarchical structure for the backup: " + args.remove_backup_folder_afterwards) |
| 137 | + |
| 138 | + if args.verbose: |
| 139 | + print("Verbose: " + args.verbose) |
| 140 | + |
| 141 | + if args.force: |
| 142 | + print("Force: " + args.force) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments