11import * as path from 'path' ;
2+ import * as Path from 'path' ;
23import * as fs from 'fs' ;
34import * as osLocale from 'os-locale' ;
45import { FtpManager } from './ftp-manager' ;
56import { AppSettings } from '../app-settings' ;
67import { ConsoleOutput } from './ConsoleOutput' ;
8+ import * as Zipper from 'node-7z'
9+ import * as rimraf from 'rimraf' ;
710import moment = require( 'moment' ) ;
811
912export class BackupManager {
@@ -29,30 +32,32 @@ export class BackupManager {
2932
3033 public doBackup ( ) {
3134 let errors = '' ;
32- if ( fs . existsSync ( path . join ( AppSettings . appPath , 'errors.log' ) ) ) {
33- fs . unlinkSync ( path . join ( AppSettings . appPath , 'errors.log' ) ) ;
35+ let name = AppSettings . settings . backup . root . substring ( 0 , AppSettings . settings . backup . root . lastIndexOf ( '/' ) ) ;
36+ name = name . substring ( name . lastIndexOf ( '/' ) + 1 ) ;
37+ const downloadPath = ( AppSettings . settings . backup . downloadPath === '' ) ? AppSettings . appPath : AppSettings . settings . backup . downloadPath ;
38+ const timeString = moment ( ) . format ( 'YYYY-MM-DD_H-mm' ) + '_' ;
39+ const targetPath = path . join ( downloadPath , timeString + name ) ;
40+
41+ if ( fs . existsSync ( path . join ( downloadPath , `${ timeString } ${ name } _errors.log` ) ) ) {
42+ fs . unlinkSync ( path . join ( downloadPath , `${ timeString } ${ name } _errors.log` ) ) ;
3443 }
35- if ( fs . existsSync ( path . join ( AppSettings . appPath , 'statistics .txt' ) ) ) {
36- fs . unlinkSync ( path . join ( AppSettings . appPath , 'statistics .txt' ) ) ;
44+ if ( fs . existsSync ( path . join ( downloadPath , ` ${ timeString } ${ name } _statistics .txt` ) ) ) {
45+ fs . unlinkSync ( path . join ( downloadPath , ` ${ timeString } ${ name } _statistics .txt` ) ) ;
3746 }
3847 const subscr = this . ftpManager . error . subscribe ( ( message : string ) => {
3948 ConsoleOutput . error ( `${ moment ( ) . format ( 'L LTS' ) } : ${ message } ` ) ;
4049 const line = `${ moment ( ) . format ( 'L LTS' ) } :\t${ message } \n` ;
4150 errors += line ;
42- fs . appendFile ( path . join ( AppSettings . appPath , 'errors .log' ) , line , {
51+ fs . appendFile ( path . join ( downloadPath , ` ${ timeString } ${ name } _errors .log` ) , line , {
4352 encoding : 'utf8'
4453 } , ( ) => {
4554 } ) ;
4655 } ) ;
4756
48- let name = AppSettings . settings . backup . root . substring ( 0 , AppSettings . settings . backup . root . lastIndexOf ( '/' ) ) ;
49- name = name . substring ( name . lastIndexOf ( '/' ) + 1 ) ;
50- const downloadPath = ( AppSettings . settings . backup . downloadPath === '' ) ? AppSettings . appPath : AppSettings . settings . backup . downloadPath ;
51-
5257 ConsoleOutput . info ( `Remote path: ${ AppSettings . settings . backup . root } \nDownload path: ${ downloadPath } \n` ) ;
5358
5459 this . ftpManager . statistics . started = Date . now ( ) ;
55- this . ftpManager . downloadFolder ( AppSettings . settings . backup . root , downloadPath ) . then ( ( ) => {
60+ this . ftpManager . downloadFolder ( AppSettings . settings . backup . root , targetPath ) . then ( ( ) => {
5661 this . ftpManager . statistics . ended = Date . now ( ) ;
5762 this . ftpManager . statistics . duration = ( this . ftpManager . statistics . ended - this . ftpManager . statistics . started ) / 1000 / 60 ;
5863
@@ -67,17 +72,58 @@ Files: ${this.ftpManager.statistics.files}
6772Errors: ${ errors . split ( '\n' ) . length - 1 } ` ;
6873
6974 ConsoleOutput . log ( '\n' + statistics ) ;
70- fs . writeFileSync ( path . join ( AppSettings . appPath , 'statistics .txt' ) , statistics , {
75+ fs . writeFileSync ( path . join ( downloadPath , ` ${ timeString } ${ name } _statistics .txt` ) , statistics , {
7176 encoding : 'utf-8'
7277 } ) ;
7378 if ( errors !== '' ) {
7479 ConsoleOutput . error ( `There are errors. Please read the errors.log file for further information.` ) ;
7580 }
7681 subscr . unsubscribe ( ) ;
7782 this . ftpManager . close ( ) ;
83+
84+ if ( AppSettings . settings . backup . zip . enabled ) {
85+ console . log ( `\nZip folder...` ) ;
86+ this . createZipFile ( downloadPath , timeString + name , this . ftpManager . statistics . files ,
87+ AppSettings . settings . backup . zip . password ) . then ( ( result ) => {
88+ ConsoleOutput . success ( 'Zip file created!' ) ;
89+ rimraf ( targetPath , ( ) => {
90+ console . log ( 'done' ) ;
91+ } ) ;
92+ } ) . catch ( ( error ) => {
93+ ConsoleOutput . error ( error ) ;
94+ } ) ;
95+ }
7896 } ) . catch ( ( error ) => {
7997 ConsoleOutput . error ( error ) ;
8098 this . ftpManager . close ( ) ;
8199 } ) ;
82100 }
101+
102+ /** create zip file for extension */
103+ async createZipFile ( path : string , name : string , numOfFiles : number , password : string ) {
104+ return new Promise < boolean > ( ( resolve , reject ) => {
105+ const localPath = Path . join ( path , name , '*' ) ;
106+ let numOfZipped = 0 ;
107+ let lastFile = '' ;
108+ Zipper . add ( Path . join ( path , name ) + `.zip` , localPath , {
109+ recursive : true ,
110+ password
111+ } ) . on ( 'end' , ( ) => {
112+ resolve ( true ) ;
113+ } ) . on ( 'error' , ( e ) => {
114+ reject ( e ) ;
115+ } ) . on ( 'data' , ( data ) => {
116+ if ( lastFile !== data . file ) {
117+ numOfZipped ++ ;
118+ }
119+ lastFile = data . file ;
120+ const percent = Math . min ( 100 , ( ( numOfZipped / numOfFiles ) * 100 ) ) . toFixed ( 2 ) ;
121+ if ( AppSettings . settings . console . tty ) {
122+ ConsoleOutput . logLive ( `Zipping...${ percent } %` )
123+ } else {
124+ ConsoleOutput . log ( `Zipping...${ percent } %` ) ;
125+ }
126+ } ) ;
127+ } ) ;
128+ }
83129}
0 commit comments