Bitbucket full backup script (deno)
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
/** | |
* Bitbucket bulk backup script using Deno. | |
* | |
* Backs up all the repos in a workspace to a mirror | |
* note: Use an app password scoped to read repo only | |
* workspace is the url slug for your username/company/group etc | |
* | |
* Should also update existing bare repos that are found. | |
* | |
* Released under MIT License, use at own risk | |
*/ | |
// const USER = ""; | |
// const PASS = ""; // Use an app password with repo read access only | |
// const WORKSPACE = ""; | |
const exists = (filename) => { | |
try { | |
Deno.statSync(filename); | |
// successful, file or directory must exist | |
return true; | |
} catch (error) { | |
if (error instanceof Deno.errors.NotFound) { | |
// file or directory does not exist | |
return false; | |
} else { | |
// unexpected error, maybe permissions, pass it along | |
throw error; | |
} | |
} | |
}; | |
let repos = []; | |
let url = `https://api.bitbucket.org/2.0/repositories/${WORKSPACE}`; | |
if(!exists("./repos")) | |
{ | |
Deno.mkdirSync("./repos"); | |
} | |
Deno.chdir("./repos"); | |
const baseDir = Deno.cwd(); | |
do { | |
console.log(url); | |
const data = await ( | |
await fetch(url, { | |
headers: { authorization: `basic ${btoa([USER, PASS].join(":"))}` }, | |
}) | |
).json(); | |
for (let entry of data.values.map((r) => ({ | |
name: r.full_name.split("/")[1], | |
url: r.links.clone.find((m) => m.name == "ssh").href, | |
}))) { | |
if (!exists(baseDir + "/" + entry.name + ".git")) { | |
console.log(`Mirroring ${entry.name}...`); | |
Deno.chdir(baseDir); | |
const p = Deno.run({ cmd: ["git", "clone", "--mirror", entry.url] }); | |
await p.status(); | |
} else { | |
console.log(`Syncing ${entry.name}...`); | |
Deno.chdir(baseDir + "/" + entry.name + ".git"); | |
const p = Deno.run({ cmd: ["git", "remote", "update"] }); | |
await p.status(); | |
} | |
} | |
url = data.next; | |
} while (url != null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment