forked from rvojcik/gitlab-project-export
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wiro
committed
Dec 25, 2018
1 parent
1ae4e7f
commit 622518e
Showing
2 changed files
with
41 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,14 @@ | |
epilog='Created by Robert Vojcik <[email protected]>') | ||
|
||
# Arguments | ||
parser.add_argument('-c', dest='config', default='config.yaml', | ||
help='config file') | ||
parser.add_argument('-d', dest='debug', default=False, action='store_const', const=True, | ||
help='Debug mode') | ||
parser.add_argument( | ||
'-c', dest='config', default='config.yaml', | ||
help='config file' | ||
) | ||
parser.add_argument( | ||
'-d', dest='debug', default=False, action='store_const', | ||
const=True, help='Debug mode' | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
|
@@ -42,39 +46,44 @@ | |
|
||
# Init gitlab api object | ||
if args.debug: | ||
print("%s, token"%(gitlab_url)) | ||
print("%s, token" % (gitlab_url)) | ||
gitlab = gitlab.Api(gitlab_url, token) | ||
|
||
# Export each project | ||
for project in c.config["gitlab"]["projects"]: | ||
if args.debug: | ||
print("Exporting %s"%(project)) | ||
print("Exporting %s" % (project)) | ||
status = gitlab.project_export(project) | ||
|
||
# Export successful | ||
if status: | ||
if args.debug: | ||
print("Success for %s"%(project)) | ||
print("Success for %s" % (project)) | ||
# Download project to our destination | ||
if c.config["backup"]["project_dirs"]: | ||
destination = c.config["backup"]["destination"] + "/" + project | ||
else: | ||
destination = c.config["backup"]["destination"] | ||
|
||
if args.debug: | ||
print(" Destination %s"%(destination)) | ||
print(" Destination %s" % (destination)) | ||
|
||
# Prepare actual date | ||
d = date.today() | ||
# File template from config | ||
file_tmpl = c.config["backup"]["backup_name"] | ||
# Projectname in dest_file | ||
dest_file = destination + "/" + file_tmpl.replace("{PROJECT_NAME}", project.replace("/", "-")) | ||
dest_file = destination + "/" + file_tmpl.replace( | ||
"{PROJECT_NAME}", | ||
project.replace("/", "-") | ||
) | ||
# Date in dest_file | ||
dest_file = dest_file.replace("{TIME}",d.strftime(c.config["backup"]["backup_time_format"])) | ||
dest_file = dest_file.replace( | ||
"{TIME}", d.strftime(c.config["backup"]["backup_time_format"]) | ||
) | ||
|
||
if args.debug: | ||
print(" Destination file %s"%(dest_file)) | ||
print(" Destination file %s" % (dest_file)) | ||
|
||
# Create directories | ||
if not os.path.isdir(destination): | ||
|
@@ -83,10 +92,14 @@ | |
# Get URL from gitlab object | ||
url = gitlab.download_url["api_url"] | ||
if args.debug: | ||
print(" URL: %s"%(url)) | ||
print(" URL: %s" % (url)) | ||
|
||
# Download file | ||
r = requests.get(url, allow_redirects=True, stream=True, headers={"PRIVATE-TOKEN": token}) | ||
r = requests.get( | ||
url, | ||
allow_redirects=True, | ||
stream=True, | ||
headers={"PRIVATE-TOKEN": token}) | ||
|
||
with open(dest_file, 'wb') as f: | ||
for chunk in r.iter_content(chunk_size=1024): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,14 +26,18 @@ | |
epilog='Created by Robert Vojcik <[email protected]>') | ||
|
||
# Arguments | ||
parser.add_argument('-c', dest='config', default='config.yaml', | ||
help='config file') | ||
parser.add_argument('-f', dest='filepath', default=False, | ||
help='Path to gitlab exported project file') | ||
parser.add_argument('-p', dest='project_path', default=False, | ||
help='Project path') | ||
parser.add_argument('-d', dest='debug', default=False, action='store_const', const=True, | ||
help='Debug mode') | ||
parser.add_argument( | ||
'-c', dest='config', default='config.yaml', | ||
help='config file') | ||
parser.add_argument( | ||
'-f', dest='filepath', default=False, | ||
help='Path to gitlab exported project file') | ||
parser.add_argument( | ||
'-p', dest='project_path', default=False, | ||
help='Project path') | ||
parser.add_argument( | ||
'-d', dest='debug', default=False, action='store_const', const=True, | ||
help='Debug mode') | ||
|
||
args = parser.parse_args() | ||
|
||
|
@@ -46,18 +50,18 @@ | |
|
||
# Init gitlab api object | ||
if args.debug: | ||
print("%s, token"%(gitlab_url)) | ||
print("%s, token" % (gitlab_url)) | ||
gitlab = gitlab.Api(gitlab_url, token) | ||
|
||
# import project | ||
if args.project_path and args.filepath and os.path.isfile(args.filepath): | ||
if args.debug: | ||
print("Exporting %s"%(args.project_path)) | ||
print("Exporting %s" % (args.project_path)) | ||
status = gitlab.project_import(args.project_path, args.filepath) | ||
|
||
# Import successful | ||
if status: | ||
print("Import success for %s"%(args.project_path)) | ||
print("Import success for %s" % (args.project_path)) | ||
sys.exit(0) | ||
else: | ||
print("Import was not successful") | ||
|
@@ -66,4 +70,3 @@ | |
else: | ||
print("Error, you have to specify correct project_path and filepath") | ||
sys.exit(1) | ||
|