Skip to content

Commit

Permalink
feat(backup.js): added ability to backup existin files
Browse files Browse the repository at this point in the history
Added the ability to add existing files to a backup folder, also creates one and uses docker ignore
to not copy this over. If the folder does exist we will rename old backsup before copy new ones
  • Loading branch information
Eventyret committed May 25, 2023
1 parent 3a54d7e commit 6e2ae02
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 72 deletions.
3 changes: 2 additions & 1 deletion templates/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
build/
node_modules/
.env
data/
data/
backup/
121 changes: 50 additions & 71 deletions utils/backup.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,85 @@
const fs = require(`fs`);
const path = require(`path`);
const util = require(`util`);
const exec = util.promisify(require(`child_process`).exec);
const { spinner, chalk, constants } = require(`./utils`);
const { spinner, chalk } = require(`./utils`);

const detectDockerFiles = async () => {
const dockerIgnoreFile = `.dockerignore`;
const backupDir = `backup`;
spinner.stopAndPersist({
symbol: `🐳`,
text: `Checking for existing Docker files... \n`
text: ` Checking for existing Docker files... \n`
});

const dockerFileRegex = /^Dockerfile(\..+)?$/;
const filesToCheck = await fs.promises.readdir(`.`);
const dockerFiles = filesToCheck.filter(file => dockerFileRegex.test(file));
const dockerFiles = filesToCheck.filter(
file => dockerFileRegex.test(file) || file === `.dockerignore`
);
if (dockerFiles.length > 0) {
spinner.stopAndPersist({
symbol: `🐳`,
text: `Docker files found in root directory! \n`
text: ` Found: ${chalk.yellow(
dockerFiles.join(`, `)
)} in project directory. \n`
});
try {
await fs.promises.access(backupDir, fs.constants.F_OK);
} catch (err) {
await fs.promises.mkdir(backupDir);
}
const backupFiles = await fs.promises.readdir(backupDir);
const backedUpFiles = [];
await Promise.all(
dockerFiles.map(async file => {
const backupFile = path.join(backupDir, file);
if (backupFiles.includes(file)) {
spinner.text = `Renaming existing backup file ${file}...`;
const backupFileNew = path.join(backupDir, `${file}.${Date.now()}`);
await fs.promises.rename(backupFile, backupFileNew);
try {
const backupFile = path.join(backupDir, file);
if (backupFiles.includes(file)) {
const backupFileNew = path.join(backupDir, `${file}.${Date.now()}`);
await fs.promises.rename(backupFile, backupFileNew);
}
spinner.stopAndPersist({
symbol: `🎉`,
text: `Renamed existing backup file ${file} to ${path.basename(
backupFileNew
)}! \n`
symbol: `🪄`,
text: ` Moving ${chalk.yellow(file)} to backup directory... \n`
});
}
spinner.text = `Moving ${file} to backup directory...`;
if (file === `Dockerfile.prod`) {
const backupFile = path.join(
backupDir,
`Dockerfile.prod.${Date.now()}`
);
await fs.promises
.rename(file, backupFile)
.then(() => {
spinner.stopAndPersist({
symbol: `🎉`,
text: `Backed up ${file} successfully! \n`
spinner.text = ``;
if (file === `Dockerfile.prod`) {
const backupFile = path.join(
backupDir,
`Dockerfile.prod.${Date.now()}`
);
await fs.promises
.rename(file, backupFile)
.then(() => {
backedUpFiles.push(file);
})
.catch(err => {
console.error(`Error backing up ${file}: ${err.message}`);
});
})
.catch(err => {
spinner.fail(`Error backing up ${file}: ${err.message}`);
});
} else {
const backupFile = path.join(backupDir, `${file}.${Date.now()}`);
await fs.promises
.rename(file, backupFile)
.then(() => {
spinner.stopAndPersist({
symbol: `🎉`,
text: `Backed up ${file} successfully! \n`
} else {
const backupFile = path.join(backupDir, `${file}.${Date.now()}`);
await fs.promises
.rename(file, backupFile)
.then(() => {
backedUpFiles.push(file);
})
.catch(err => {
console.error(`Error backing up ${file}: ${err.message}`);
});
})
.catch(err => {
spinner.fail(`Error backing up ${file}: ${err.message}`);
});
}
} catch (error) {
console.log(error);
}
})
);
spinner.stopAndPersist({
symbol: `🐳`,
text: `Dockerfiles found and backed up successfully! \n`
});
} else {
spinner.stopAndPersist({
symbol: `🧠`,
text: `No Dockerfiles found, nothing to backup...`
});
}

if (fs.existsSync(dockerIgnoreFile)) {
spinner.text = `Moving .dockerignore file to backup directory...`;
const backupFile = path.join(backupDir, dockerIgnoreFile);
await fs.promises
.rename(dockerIgnoreFile, backupFile)
.then(() => {
spinner.stopAndPersist({
symbol: `🎉`,
text: `Backed up ${dockerIgnoreFile} successfully! \n`
});
})
.catch(err => {
spinner.fail(`Error backing up ${dockerIgnoreFile}: ${err.message}`);
if (backedUpFiles.length > 0) {
spinner.stopAndPersist({
symbol: `📦`,
text: ` Backed up ${chalk.yellow(backedUpFiles.join(`, `))} \n`
});
}
} else {
spinner.stopAndPersist({
symbol: `🐳`,
text: `.dockerignore file found and backed up successfully! \n`
symbol: `💁`,
text: ` No Dockerfiles found in the root directory. Skipping backup. \n`
});
}
};
Expand Down

0 comments on commit 6e2ae02

Please sign in to comment.