ansible/roles/backup-server/files/backup-bitbucket.py

51 lines
1.4 KiB
Python

#!/usr/bin/env python3
import os
import json
from subprocess import call
from urllib.request import urlopen
USERS = ["tmakinen"]
BACKUPDIR = "/srv/backup/bitbucket.org"
def repolist(username):
f = urlopen(f"https://api.bitbucket.org/2.0/repositories/{username}")
data = json.load(f)
f.close()
for repo in data["values"]:
yield (
{
"name": repo["name"],
"scm": repo["scm"],
"wiki": repo["has_wiki"],
"issues": repo["has_issues"],
}
)
def gitbackup(destination, repo):
if not os.path.exists(destination):
os.makedirs(destination)
call(["git", "clone", "--quiet", repo, destination])
else:
os.chdir(destination)
call(["git", f"--git-dir={destination}/.git", "pull", "--quiet"])
if __name__ == "__main__":
for user in USERS:
for repo in repolist(user):
if repo["scm"] == "git":
gitbackup(
f"{BACKUPDIR}/{user}/{repo['name']}",
f"https://bitbucket.org/{user}/{repo['name']}.git",
)
if repo["wiki"]:
gitbackup(
f"{BACKUPDIR}/{user}/{repo['name']}-wiki",
f"https://bitbucket.org/{user}/{repo['name']}.git/wiki",
)
else:
raise NotImplementedError("{repo['scm']} repositories not supported")