Skip to content

Commit

Permalink
Added regularity of backups.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dazsmith committed Apr 20, 2020
1 parent a9657a0 commit bffad6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 7 additions & 3 deletions config.yaml-example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
16 changes: 13 additions & 3 deletions gitlab-project-export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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))
Expand Down Expand Up @@ -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("/", "-")
)
Expand Down

0 comments on commit bffad6f

Please sign in to comment.