mirror of
https://github.com/morten-olsen/github-backup.git
synced 2026-02-08 01:36:24 +01:00
Use remote update instead of re-mirroring
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"lint": "docker run --rm -i hadolint/hadolint < Dockerfile",
|
||||||
"start": "ts-node src/index.ts",
|
"start": "ts-node src/index.ts",
|
||||||
"bundle": "ncc build src/index.ts -o dist"
|
"bundle": "ncc build src/index.ts -o dist"
|
||||||
},
|
},
|
||||||
|
|||||||
31
src/index.ts
31
src/index.ts
@@ -15,22 +15,24 @@ const token = process.env.GITHUB_TOKEN;
|
|||||||
const backupLocation = process.env.GITHUB_BACKUP_LOCATION || '/backup';
|
const backupLocation = process.env.GITHUB_BACKUP_LOCATION || '/backup';
|
||||||
|
|
||||||
const mirror = async (target: string, repo: string) => {
|
const mirror = async (target: string, repo: string) => {
|
||||||
const git = simpleGit();
|
|
||||||
const authRemote = `https://foo:${token}@github.com/${repo}`
|
const authRemote = `https://foo:${token}@github.com/${repo}`
|
||||||
|
|
||||||
if (fs.existsSync(target)) {
|
if (fs.existsSync(target)) {
|
||||||
const backup = path.join(os.tmpdir(), nanoid());
|
const git = simpleGit(target);
|
||||||
await fs.move(target, backup);
|
const remotes = await git.getRemotes();
|
||||||
try {
|
const origin = remotes.find(r => r.name === 'origin');
|
||||||
await git.mirror(authRemote, target);
|
if (origin) {
|
||||||
await rimraf(backup);
|
await git.remote(['set-url', 'origin', authRemote]);
|
||||||
} catch(err) {
|
|
||||||
await rimraf(target);
|
|
||||||
await fs.move(backup, target);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
await git.addRemote('origin', authRemote);
|
||||||
|
}
|
||||||
|
await git.remote(['update']);
|
||||||
|
await git.remote(['set-url', 'origin', `https://github.com/${repo}`]);
|
||||||
|
} else {
|
||||||
|
await fs.mkdirp(target);
|
||||||
|
const git = simpleGit(target);
|
||||||
await git.mirror(authRemote, target);
|
await git.mirror(authRemote, target);
|
||||||
|
await git.remote(['set-url', 'origin', `https://github.com/${repo}`]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -43,14 +45,19 @@ const run = async () => {
|
|||||||
const errors: any[] = [];
|
const errors: any[] = [];
|
||||||
for await (const repos of github.paginate.iterator(action, { visibility: 'all' })) {
|
for await (const repos of github.paginate.iterator(action, { visibility: 'all' })) {
|
||||||
for (const repo of repos.data) {
|
for (const repo of repos.data) {
|
||||||
const loader = ora(repo.full_name).start();
|
const loader = ora('preparing');
|
||||||
|
loader.prefixText = repo.full_name;
|
||||||
|
loader.start();
|
||||||
try {
|
try {
|
||||||
const repoBackupLocation = path.join(backupLocation, repo.full_name);
|
const repoBackupLocation = path.join(backupLocation, repo.full_name);
|
||||||
const infoLocation = path.join(repoBackupLocation, 'info.json');
|
const infoLocation = path.join(repoBackupLocation, 'info.json');
|
||||||
const gitLocation = path.join(repoBackupLocation, 'git');
|
const gitLocation = path.join(repoBackupLocation, 'git');
|
||||||
await fs.mkdirp(repoBackupLocation);
|
await fs.mkdirp(repoBackupLocation);
|
||||||
|
loader.text = 'fething info';
|
||||||
await fs.writeFile(infoLocation, JSON.stringify(repo, null, ' '), 'utf-8');
|
await fs.writeFile(infoLocation, JSON.stringify(repo, null, ' '), 'utf-8');
|
||||||
|
loader.text = 'mirroring';
|
||||||
await mirror(gitLocation, repo.full_name);
|
await mirror(gitLocation, repo.full_name);
|
||||||
|
loader.text = '';
|
||||||
loader.succeed();
|
loader.succeed();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
loader.fail(err.toString());
|
loader.fail(err.toString());
|
||||||
|
|||||||
Reference in New Issue
Block a user