From bffad6f0cc416d64dc974f4aae1bb992527b5916 Mon Sep 17 00:00:00 2001 From: Dave Date: Mon, 20 Apr 2020 17:40:35 +0800 Subject: [PATCH] Added regularity of backups. Not every project needs to be exported / backed up at the same regularity, as some projects rarely see progress. But it would be good to have just one config file for various different regularities of backup. Now only the projects with the selected regularity will be backed up. Had to change the format of the config.yaml file to achieve this. Now each project has a name and a regularity. The name is as before, the regularity is a list of strings. If any of those strings matches the new command line argument passed with -r, then that project will be back up. Otherwise, that project will not be backed up. --- config.yaml-example | 10 +++++++--- gitlab-project-export.py | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/config.yaml-example b/config.yaml-example index 42004d6..9f335c3 100644 --- a/config.yaml-example +++ b/config.yaml-example @@ -10,8 +10,10 @@ gitlab: gitlab_url: "https://gitlab.com" token: "MY_PERSONAL_SECRET_TOKEN" projects: - - rvojcik/example-project + - name: rvojcik/example-project + regularity: [year, month, week, day] - rvojcik/group/* + regularity: [year] # # Backup configuration @@ -24,14 +26,16 @@ backup: destination: "/data/backup" # Backup Name template - # Is it possible to use some placeholders in the name + # It is possible to use some placeholders in the name # {PROJECT_NAME} - Name of the project with full path # Path slashes is replaces with dashes. # Example: # rvojcik/project1 => rvojcik-project1 # # {TIME} - Time of the export - backup_name: "gitlab-com-{PROJECT_NAME}-{TIME}.tar.gz" + # {REGULARITY} - The regularity selected as an + # argument passed to the script. + backup_name: "gitlab-com-{PROJECT_NAME}-{REGULARITY}-{TIME}.tar.gz" # Time format tamplate # Time is construct by python strftime. diff --git a/gitlab-project-export.py b/gitlab-project-export.py index cfd69ff..2f0f516 100755 --- a/gitlab-project-export.py +++ b/gitlab-project-export.py @@ -35,6 +35,10 @@ '-d', dest='debug', default=False, action='store_const', const=True, help='Debug mode' ) + parser.add_argument( + '-r', dest='regularity', default="day", + help='Specify the regularity of this backup. See config.yaml-example' + ) args = parser.parse_args() @@ -59,12 +63,17 @@ print("Unable to get projects for your account", file=sys.stderr) sys.exit(1) + if args.debug: + print("regularity is %s" % args.regularity) + # Check projects against config # Create export_projects array for project_pattern in c.config["gitlab"]["projects"]: for gitlabProject in projects: - if re.match(project_pattern, gitlabProject): - export_projects.append(gitlabProject) + if re.match(project_pattern["name"], gitlabProject): + for regularity_pattern in project_pattern["regularity"]: + if re.match(regularity_pattern, args.regularity): + export_projects.append(gitlabProject) if args.debug: print("Projects to export: " + str(export_projects)) @@ -94,8 +103,9 @@ d = date.today() # File template from config file_tmpl = c.config["backup"]["backup_name"] + file_tmpl_reg = file_tmpl.replace("{REGULARITY}", args.regularity) # Projectname in dest_file - dest_file = destination + "/" + file_tmpl.replace( + dest_file = destination + "/" + file_tmpl_reg.replace( "{PROJECT_NAME}", project.replace("/", "-") )