@@ -4,9 +4,47 @@ const debug = require('debug')('ghost-cli:backup');
4
4
const execa = require ( 'execa' ) ;
5
5
const fs = require ( 'fs-extra' ) ;
6
6
7
+ const ghostUser = require ( '../utils/use-ghost-user' ) ;
7
8
const { ProcessError} = require ( '../errors' ) ;
8
9
const { exportTask} = require ( './import' ) ;
9
10
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
+
10
48
module . exports = async function ( ui , instance ) {
11
49
const datetime = require ( 'moment' ) ( ) . format ( 'YYYY-MM-DD-HH-mm-ss' ) ;
12
50
const backupSuffix = `from-v${ instance . version } -on-${ datetime } ` ;
@@ -16,9 +54,7 @@ module.exports = async function (ui, instance) {
16
54
const membersExportFile = `members-${ backupSuffix } .csv` ;
17
55
18
56
// 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 ) ;
22
58
23
59
// Generate backup files
24
60
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) {
32
68
[ `backup/${ membersExportFile } ` ] : `content/data/${ membersExportFile } `
33
69
} ;
34
70
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 ) ;
48
72
49
73
// Finally we zip everything up into a nice little package
50
74
const zipPath = path . join ( process . cwd ( ) , `backup-${ backupSuffix } .zip` ) ;
0 commit comments