@@ -77,9 +77,9 @@ public function backup(string $resource = null, bool $once = false): string
7777
7878 if (!$ once || !File::exists ($ pathname )) {
7979 switch ($ this ->getOutput ()) {
80- case 'tar_unsafe ' : return $ this ->saveAsTar ($ pathname , $ files , null , true );
81- case 'tar ' : return $ this ->saveAsTar ($ pathname , $ files );
82- case 'tar_gz ' : case 'tar_bz2 ' : return $ this ->saveAsTar ($ pathname , $ files , str_after ($ this ->getOutput (), '_ ' ));
80+ case 'tar_unsafe ' : return $ this ->saveAsTar ($ name , $ pathname , $ files , null , true );
81+ case 'tar ' : return $ this ->saveAsTar ($ name , $ pathname , $ files );
82+ case 'tar_gz ' : case 'tar_bz2 ' : return $ this ->saveAsTar ($ name , $ pathname , $ files , str_after ($ this ->getOutput (), '_ ' ));
8383 case 'zip ' : return $ this ->saveAsZip ($ name , $ pathname , $ files );
8484 default : throw new Exception (trans ('webula.smallbackup::lang.backup.flash.unknown_output ' ));
8585 }
@@ -117,23 +117,30 @@ protected function getOutputPathName(string $name): string
117117 return $ pathname ;
118118 }
119119
120+ protected function getTempPathName (string $ name , string $ extension ): string
121+ {
122+ return temp_path ($ name . '.backup. ' . $ extension );
123+ }
124+
120125 /**
121126 * Save folderlist as TAR archive
122127 *
128+ * @param string $name filename
123129 * @param string $pathname path name
124130 * @param array $folders list of files
125131 * @param string|null $compression compression type
126132 * @param bool $unsafe do not check TAR names and truncate them
127133 * @return string file with current backup
128134 */
129- protected function saveAsTar (string $ pathname , array $ files , ?string $ compression = null , bool $ unsafe = false ): string
135+ protected function saveAsTar (string $ name , string $ pathname , array $ files , ?string $ compression = null , bool $ unsafe = false ): string
130136 {
131137 File::delete ([$ pathname , $ pathname . '.gz ' , $ pathname . '.bz2 ' ]);
138+ $ temp_pathname = $ this ->getTempPathName ($ name , 'tar ' );
132139
133140 $ truncated = [];
134141
135142 try {
136- $ archive = new PharData ($ pathname );
143+ $ archive = new PharData ($ temp_pathname );
137144 foreach ($ files as $ file ) {
138145 $ relative_name = str_after ($ file , PathHelper::normalizePath (base_path ()));
139146 if (!$ unsafe ) {
@@ -150,12 +157,13 @@ protected function saveAsTar(string $pathname, array $files, ?string $compressio
150157
151158 if ($ compression && $ archive ->canCompress ($ compression == 'gz ' ? Phar::GZ : Phar::BZ2 )) {
152159 $ archive ->compress ($ compression == 'gz ' ? Phar::GZ : Phar::BZ2 );
153- File::delete ($ pathname );
154160 $ pathname .= '. ' . $ compression ;
155161 }
162+ File::move ($ temp_pathname . ($ compression ? '. ' . $ compression : '' ), $ pathname );
156163 } catch (Exception $ ex ) {
157- File::delete ([$ pathname , $ pathname . '.gz ' , $ pathname . '.bz2 ' ]);
158164 throw new Exception (trans ('webula.smallbackup::lang.backup.flash.failed_backup ' , ['error ' => $ ex ->getMessage ()]));
165+ } finally {
166+ File::delete ([$ temp_pathname , $ temp_pathname . '.gz ' , $ temp_pathname . '.bz2 ' ]);
159167 }
160168
161169 if (!$ unsafe && !empty ($ truncated )) {
@@ -179,13 +187,15 @@ protected function saveAsTar(string $pathname, array $files, ?string $compressio
179187 */
180188 protected function saveAsZip (string $ name , string $ pathname , array $ files ): string
181189 {
190+ $ temp_pathname = $ this ->getTempPathName ($ name , 'zip ' );
191+
182192 $ files = array_map (function ($ folder ) {
183193 return PathHelper::linuxPath ($ folder ); // FIX October Zip in Windows
184194 }, $ files );
185195
186196 try {
187197 Zip::make (
188- $ pathname ,
198+ $ temp_pathname ,
189199 function ($ zip ) use ($ files , $ name ) {
190200 $ zip ->folder ($ name , function ($ zip ) use ($ files ) {
191201 foreach ($ files as $ file ) {
@@ -194,9 +204,11 @@ function ($zip) use ($files, $name) {
194204 });
195205 }
196206 );
207+ File::move ($ temp_pathname , $ pathname );
197208 } catch (Exception $ ex ) {
198- File::delete ($ pathname );
199209 throw new Exception (trans ('webula.smallbackup::lang.backup.flash.failed_backup ' , ['error ' => $ ex ->getMessage ()]));
210+ } finally {
211+ File::delete ($ temp_pathname );
200212 }
201213
202214 return $ pathname ;
0 commit comments