From 1b8062b6fa4b0c14adf015f4bd2e9f56b5dc2a84 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Sun, 21 Feb 2010 08:25:58 +0000 Subject: [PATCH] maintenance/sqlite.php: added an option for safe backup --- maintenance/sqlite.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/maintenance/sqlite.php b/maintenance/sqlite.php index 46a038ceca..40018bd2c7 100644 --- a/maintenance/sqlite.php +++ b/maintenance/sqlite.php @@ -28,6 +28,7 @@ class SqliteMaintenance extends Maintenance { $this->mDescription = "Performs some operations specific to SQLite database backend"; $this->addOption( 'vacuum', 'Clean up database by removing deleted pages. Decreases database file size' ); $this->addOption( 'integrity', 'Check database for integrity' ); + $this->addOption( 'backup-to', 'Backup database to the given file', false, true ); } public function execute() { @@ -40,11 +41,17 @@ class SqliteMaintenance extends Maintenance { $this->db = wfGetDB( DB_MASTER ); - if ( $this->hasOption( 'vacuum' ) ) + if ( $this->hasOption( 'vacuum' ) ) { $this->vacuum(); + } - if ( $this->hasOption( 'integrity' ) ) + if ( $this->hasOption( 'integrity' ) ) { $this->integrityCheck(); + } + + if ( $this->hasOption( 'backup-to' ) ) { + $this->backup( $this->getOption( 'backup-to' ) ); + } } private function vacuum() { @@ -77,6 +84,21 @@ class SqliteMaintenance extends Maintenance { $this->output( $row->integrity_check ); } } + + private function backup( $fileName ) { + $this->output( "Backing up database:\n Locking..." ); + $this->db->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ ); + $ourFile = $this->db->mDatabaseFile; + $this->output( " Copying database file $ourFile to $fileName... " ); + wfSuppressWarnings( false ); + if ( !copy( $ourFile, $fileName ) ) { + $err = error_get_last(); + $this->error( " {$err['message']}" ); + } + wfSuppressWarnings( true ); + $this->output( " Releasing lock...\n" ); + $this->db->query( 'COMMIT TRANSACTION', __METHOD__ ); + } } $maintClass = "SqliteMaintenance"; -- 2.20.1