-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtugboat-backup.js
executable file
·54 lines (45 loc) · 1.69 KB
/
tugboat-backup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const { execSync } = require('child_process')
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question('Enter Docker image names (separated by a comma): ', (imageAnswer) => {
const images = imageAnswer.split(',').map(img => img.trim()).filter(img => img)
rl.question('Enter Docker volume names (separated by a comma): ', (volumeAnswer) => {
const volumes = volumeAnswer.split(',').map(vol => vol.trim()).filter(vol => vol)
backupDocker(images, volumes)
rl.close()
})
})
function backupDocker (images, volumes) {
console.log('Backing up images:', images)
console.log('Backing up volumes:', volumes)
const backupDir = process.argv[2] || './backup' // You can pass the backup directory as an argument
// Create backup directory if it doesn't exist
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir, { recursive: true })
}
// Backup Docker Images
images.forEach(image => {
const imagePath = path.join(backupDir, `${image}.tar`)
console.log(`Backing up Docker image: ${image}`)
try {
execSync(`docker save -o "${imagePath}" ${image}`)
} catch (error) {
console.error(`Error backing up image ${image}:`, error)
}
})
// Backup Docker Volumes
volumes.forEach(volume => {
console.log(`Backing up Docker volume: ${volume}`)
try {
execSync(`docker run --rm -v ${volume}:/volume -v ${backupDir}:/backup alpine tar cvf /backup/${volume}.tar /volume`)
} catch (error) {
console.error(`Error backing up volume ${volume}:`, error)
}
})
console.log('Backup completed successfully.')
}