Skip to content

Commit 0cbb492

Browse files
committed
fix(backup): handle local + server permissions
refs: TryGhost/Toolbox#334 refs: #468 - we have to handle files differently depending on whether we're working locally or on a server - first pass worked locally, second worked on ubuntu, this one should work on both
1 parent a0f1d36 commit 0cbb492

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

lib/tasks/backup.js

+40-16
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,47 @@ const debug = require('debug')('ghost-cli:backup');
44
const execa = require('execa');
55
const fs = require('fs-extra');
66

7+
const ghostUser = require('../utils/use-ghost-user');
78
const {ProcessError} = require('../errors');
89
const {exportTask} = require('./import');
910

11+
async function ensureBackupFolder(ui, instance) {
12+
const folderName = '../backup';
13+
14+
const contentDir = instance.config.get('paths.contentPath');
15+
if (ghostUser.shouldUseGhostUser(contentDir)) {
16+
const {USER} = process.env;
17+
await ui.sudo(`mkdir -p ${path.resolve(contentDir, folderName)}`, {sudoArgs: `-E -u ${USER}`});
18+
} else {
19+
fs.ensureDirSync(path.resolve(contentDir, folderName));
20+
}
21+
}
22+
23+
async function copyFiles(ui, instance, files) {
24+
const contentDir = instance.config.get('paths.contentPath');
25+
const shouldUseSudo = ghostUser.shouldUseGhostUser(contentDir);
26+
27+
for (const fileKey in files) {
28+
const filePath = path.join(instance.dir, fileKey);
29+
const fileExists = fs.existsSync(filePath);
30+
31+
if (fileExists) {
32+
debug(`copying ${fileKey} to ${files[fileKey]}`);
33+
34+
const destinationFilePath = path.join(instance.dir, files[fileKey]);
35+
if (shouldUseSudo) {
36+
await ui.sudo(`cp ${filePath} ${destinationFilePath}`);
37+
} else {
38+
await fs.copy(filePath, destinationFilePath);
39+
}
40+
}
41+
}
42+
43+
if (shouldUseSudo) {
44+
await ui.sudo(`chown -R ghost:ghost ${contentDir}`);
45+
}
46+
}
47+
1048
module.exports = async function (ui, instance) {
1149
const datetime = require('moment')().format('YYYY-MM-DD-HH-mm-ss');
1250
const backupSuffix = `from-v${instance.version}-on-${datetime}`;
@@ -16,9 +54,7 @@ module.exports = async function (ui, instance) {
1654
const membersExportFile = `members-${backupSuffix}.csv`;
1755

1856
// Ensure the backup directory exists and has write permissions
19-
const {USER} = process.env;
20-
await ui.sudo(`mkdir -p ${path.join(instance.dir, 'backup')}`);
21-
await ui.sudo(`chown -R ${USER}:${USER} ${path.join(instance.dir, 'backup')}`);
57+
await ensureBackupFolder(ui, instance);
2258

2359
// Generate backup files
2460
await exportTask(ui, instance, path.join(instance.dir, 'backup/', contentExportFile), path.join(instance.dir, 'backup/', membersExportFile));
@@ -32,19 +68,7 @@ module.exports = async function (ui, instance) {
3268
[`backup/${membersExportFile}`]: `content/data/${membersExportFile}`
3369
};
3470

35-
for (const backupFilesKey in backupFiles) {
36-
const filePath = path.join(instance.dir, backupFilesKey);
37-
const fileExists = fs.existsSync(filePath);
38-
39-
if (fileExists) {
40-
debug(`copying ${backupFilesKey} to ${backupFiles[backupFilesKey]}`);
41-
42-
const destinationFilePath = path.join(instance.dir, backupFiles[backupFilesKey]);
43-
await ui.sudo(`cp ${filePath} ${destinationFilePath}`);
44-
}
45-
}
46-
47-
await ui.sudo(`chown -R ghost:ghost ${path.join(instance.dir, 'content')}`);
71+
await copyFiles(ui, instance, backupFiles);
4872

4973
// Finally we zip everything up into a nice little package
5074
const zipPath = path.join(process.cwd(), `backup-${backupSuffix}.zip`);

0 commit comments

Comments
 (0)