@@ -92,7 +92,7 @@ class DBExport
9292 * @param string|null $saveAsFileType Save the backup file as [zip|rar]
9393 * @param int $retentionDays Days to keep backups before deletion
9494 */
95- public function __construct ($ connection = null , $ saveAsFileType = null , int $ retentionDays = 7 )
95+ public function __construct ($ connection = null , $ saveAsFileType = null , ? int $ retentionDays = 5 )
9696 {
9797 $ saveAsFileType = Str::lower ($ saveAsFileType );
9898
@@ -106,6 +106,10 @@ public function __construct($connection = null, $saveAsFileType = null, int $ret
106106
107107 $ this ->conn = DB ::connection ($ connection );
108108 $ this ->db = $ this ->conn ->dbConnection ();
109+
110+ // since we're exporting from a live/database server
111+ // we need to temporarily disable prefixing, by setting its value to empty string
112+ $ this ->conn ->changeTablePrefix ('' );
109113 }
110114
111115 /**
@@ -129,10 +133,16 @@ public function run($backupDir = null)
129133 $ filename = "{$ dbName }_backup_ {$ timestamp }.sql " ;
130134 $ path = "{$ backupDir }/ {$ filename }" ;
131135
136+ // make required directory
132137 if (!File::isDirectory ($ backupDir )) {
133138 File::makeDirectory ($ backupDir , 0755 , true );
134139 }
135140
141+ // Cleanup old backups if expired
142+ if ($ this ->retentionDays >= 0 ) {
143+ $ this ->cleanupOldBackups ($ backupDir , $ this ->retentionDays );
144+ }
145+
136146 $ fh = fopen ($ path , 'w ' );
137147 if (!$ fh ) {
138148 $ this ->message = sprintf ("Cannot open file for writing: [`%s`] " , $ path );
@@ -166,11 +176,6 @@ public function run($backupDir = null)
166176 // Compress the backup using
167177 $ this ->saveFileAs ($ path );
168178
169- // Cleanup old backups if expired
170- if ($ this ->retentionDays > 0 ) {
171- $ this ->cleanupOldBackups ($ backupDir , $ this ->retentionDays );
172- }
173-
174179 $ this ->error = Constant::STATUS_200 ;
175180 $ this ->message = "- Database has been exported successfully. " ;
176181 } catch (\Throwable $ e ) {
0 commit comments