Skip to content

Commit eacfbe2

Browse files
committed
support for env file
closes #5
1 parent f7c6385 commit eacfbe2

File tree

6 files changed

+24
-28
lines changed

6 files changed

+24
-28
lines changed

.env.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
USERNAME=
2+
TOKEN=
3+
FOLDER=

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
# NPM Modules
55
node_modules/
66

7-
# Credentials File
8-
credentials.js
7+
# Environment Variables File
8+
.env

Makefile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
.PHONY: backup docker_cleanup
2-
3-
backup: docker_run
1+
include ./.env
42

53
docker_run: docker_build
6-
docker run --rm --name github-backup github-backup
4+
docker run --rm --name github-backup -v $(FOLDER):/usr/src/backup --env-file ./.env github-backup
75

86
docker_build: docker_cleanup
97
docker build -t github-backup .

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ Local backup of your GitHub data.
1818
Requirements: Docker
1919

2020
1. Clone this repository
21-
2. Update and save `credentials.template.js` as `credentials.js`
21+
2. Update and save `.env.template` file as `.env`
2222
3. Run `make` to start the backup process

credentials.template.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

index.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import shell from 'shelljs'
33
import { dirname, basename } from 'path'
44
import fetch from 'node-fetch'
55
import { extension } from 'mime-types'
6-
import credentials from './credentials.js'
76

87
shell.config.fatal = true
98

@@ -12,7 +11,8 @@ const retryCount = 10
1211
const retryDelayRateLimit = 6 * 60
1312
const retryDelayOthers = 6
1413

15-
const { username, token, folder } = credentials
14+
const { USERNAME, TOKEN } = process.env
15+
const FOLDER = '/usr/src/backup'
1616

1717
function delay(seconds) {
1818
return new Promise(resolve => {
@@ -32,7 +32,7 @@ function request(path, options = {}) {
3232
try {
3333
resp = await fetch(`${baseUrl}${path}`, {
3434
headers: {
35-
Authorization: `Token ${token}`
35+
Authorization: `Token ${TOKEN}`
3636
},
3737
...options
3838
})
@@ -125,15 +125,15 @@ function downloadFile(sourceFileUrl, targetFilePath) {
125125
})
126126
}
127127

128-
function downloadAssets(body, folder, filename) {
128+
function downloadAssets(body, FOLDER, filename) {
129129
return new Promise(async (resolve, reject) => {
130130
try {
131131
const assets = body?.match(/["(]https:\/\/github\.com\/(.+)\/assets\/(.+)[)"]/g) || []
132132
for (const assetId in assets) {
133133
const targetFilename = filename.replace('{id}', assetId)
134-
const targetPath = folder + '/' + targetFilename
134+
const targetPath = FOLDER + '/' + targetFilename
135135
const sourceUrl = assets[assetId].replace(/^["(](.+)[)"]$/, '$1')
136-
fs.ensureDirSync(folder)
136+
fs.ensureDirSync(FOLDER)
137137
const realTargetFilename = basename(await downloadFile(sourceUrl, targetPath))
138138
body = body.replace(`"${sourceUrl}"`, '"file://./assets/' + realTargetFilename + '"')
139139
body = body.replace(`(${sourceUrl})`, '(file://./assets/' + realTargetFilename + ')')
@@ -153,28 +153,28 @@ function writeJSON(path, json) {
153153
async function backup() {
154154
try {
155155

156-
// Reset the backup folder
157-
fs.emptyDirSync(folder)
156+
// Reset the backup FOLDER
157+
fs.emptyDirSync(FOLDER)
158158

159159
// Get repositories
160160
const repositories = await requestAllWithRetry('/user/repos')
161161

162162
// Save repositories
163-
writeJSON(`${folder}/repositories.json`, repositories)
163+
writeJSON(`${FOLDER}/repositories.json`, repositories)
164164

165165
// Loop repositories
166166
for (const repository of repositories) {
167167

168168
// Get issues
169-
const issues = await requestAllWithRetry(`/repos/${username}/${repository.name}/issues?state=all`)
169+
const issues = await requestAllWithRetry(`/repos/${USERNAME}/${repository.name}/issues?state=all`)
170170

171171
// Loop issues
172172
for (const issueId in issues) {
173173

174174
// Download issue assets
175175
issues[issueId].body = await downloadAssets(
176176
issues[issueId].body,
177-
`${folder}/repositories/${repository.name}/assets`,
177+
`${FOLDER}/repositories/${repository.name}/assets`,
178178
`issue_${issueId}_{id}`
179179
)
180180

@@ -190,7 +190,7 @@ async function backup() {
190190
// Download issue assets
191191
issues[issueId].comments[commentId].body = await downloadAssets(
192192
issues[issueId].comments[commentId].body,
193-
`${folder}/repositories/${repository.name}/assets`,
193+
`${FOLDER}/repositories/${repository.name}/assets`,
194194
`issue_${issueId}_comment_${commentId}_{id}`
195195
)
196196

@@ -199,20 +199,20 @@ async function backup() {
199199
}
200200

201201
// Save issues
202-
writeJSON(`${folder}/repositories/${repository.name}/issues.json`, issues)
202+
writeJSON(`${FOLDER}/repositories/${repository.name}/issues.json`, issues)
203203

204204
// Clone repository
205-
shell.exec(`git clone https://${token}@github.com/${username}/${repository.name}.git ${folder}/repositories/${repository.name}/repository`)
205+
shell.exec(`git clone https://${TOKEN}@github.com/${USERNAME}/${repository.name}.git ${FOLDER}/repositories/${repository.name}/repository`)
206206

207207
}
208208

209209
// Get user details
210210
const user = await requestJson('/user')
211-
writeJSON(`${folder}/user/user.json`, user)
211+
writeJSON(`${FOLDER}/user/user.json`, user)
212212

213213
// Get starred repositories
214214
const starred = await requestAllWithRetry('/user/starred')
215-
writeJSON(`${folder}/user/starred.json`, starred)
215+
writeJSON(`${FOLDER}/user/starred.json`, starred)
216216

217217
// Complete script
218218
console.log('Backup completed!')

0 commit comments

Comments
 (0)