Skip to content

Instantly share code, notes, and snippets.

@tionis
Last active November 1, 2020 14:49
Show Gist options
  • Save tionis/c6b3d2623f0965ac9f37e1606839cdc0 to your computer and use it in GitHub Desktop.
Save tionis/c6b3d2623f0965ac9f37e1606839cdc0 to your computer and use it in GitHub Desktop.
Backup github repos and gists.

Github repo and gist backup

These scripts depend on deno.
Simply execute both script like so: deno run --allow-net --allow-run --unstable --allow-read

import { Base64 } from "https://deno.land/x/bb64/mod.ts";
import { existsSync } from "https://deno.land/std/fs/mod.ts";
let username = "editMe";
let token = "editMe";
// Build correct headers
let headers = new Headers();
//headers.append('Content-Type', 'text/json');
headers.append(
"Authorization",
"Basic " + Base64.fromString(username + ":" + token).toString(),
);
// Get the data and start the compare & clone process
fetch("https://api.github.com/gists", {
method: "GET",
headers: headers,
} //credentials: 'user:passwd'
).then((response) => {
return response.json();
}).then((jsonData) => {
jsonData.forEach(handleGithubJSON);
});
// Handle the data gotten from github and start the compare & clone process
async function handleGithubJSON(item, index) {
// Check if folder at location already exists
const isPathExist = existsSync(username + "-gists/" + item.id);
if (isPathExist) {
// create subprocess for git pull
const gitPullProcess = Deno.run({
cmd: ["git", "-C", username + "-gists/" + item.id, "pull"],
});
// await its completion
await gitPullProcess.status();
} else {
// create subprocess for git clone
const gitCloneProcess = Deno.run({
cmd: ["git", "clone", item.git_pull_url, username + "-gists/" + item.id],
});
// await its completion
await gitCloneProcess.status();
}
}
import { Base64 } from "https://deno.land/x/bb64/mod.ts";
import { existsSync } from "https://deno.land/std/fs/mod.ts";
let username = "editMe";
let token = "editMe";
// Build correct headers
let headers = new Headers();
//headers.append('Content-Type', 'text/json');
headers.append(
"Authorization",
"Basic " + Base64.fromString(username + ":" + token).toString(),
);
// Get the data and start the compare & clone process
fetch("https://api.github.com/user/repos", {
method: "GET",
headers: headers,
} //credentials: 'user:passwd'
).then((response) => {
return response.json();
}).then((jsonData) => {
jsonData.forEach(handleGithubJSON);
});
// Handle the data gotten from github and start the compare & clone process
async function handleGithubJSON(item, index) {
// Check if folder at location already exists
const isPathExist = existsSync(username + "-repos/" + item.name);
if (isPathExist) {
// create subprocess for git pull
const gitPullProcess = Deno.run({
cmd: ["git", "-C", username + "-repos/" + item.name, "pull"],
});
// await its completion
await gitPullProcess.status();
} else {
// create subprocess for git clone
const gitCloneProcess = Deno.run({
cmd: ["git", "clone", item.ssh_url, username + "-repos/" + item.name],
});
// await its completion
await gitCloneProcess.status();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment