Author: david
Date: Fri May 4 12:05:52 2012
New Revision: 11634
Log:
Attempt to backup database. Only works with MySQL.
Modified:
trunk/lib/task/migrate/arUpgradeSqlTask.class.php
Modified: trunk/lib/task/migrate/arUpgradeSqlTask.class.php
==============================================================================
--- trunk/lib/task/migrate/arUpgradeSqlTask.class.php Fri May 4 00:03:02
2012 (r11633)
+++ trunk/lib/task/migrate/arUpgradeSqlTask.class.php Fri May 4 12:05:52
2012 (r11634)
@@ -62,6 +62,10 @@
*/
protected function execute($arguments = array(), $options = array())
{
+ $dbManager = new sfDatabaseManager($this->configuration);
+ $database = $dbManager->getDatabase($options['connection']);
+ // $conn = $database->getConnection();
+
$this->getInitialVersion();
// A bug in the migration script for Release 1.1 left the version="62"
@@ -90,15 +94,17 @@
return 1;
}
+ // Attempt to backup database (MySQL only)
+ $backupName = $this->backupDatabase($database);
+
// Confirm update
- if (!$options['no-confirmation']
- &&
- !$this->askConfirmation(array(
- 'WARNING: This data upgrade can not be undone!',
- '',
- 'Are you sure you want to proceed? (y/N)'
- ), 'QUESTION_LARGE', false)
- )
+ if (!isset($backupName) && !$this->askConfirmation(array(
+ 'WARNING: Could not back-up your database!',
+ 'Please back-up your database manually before you proceed.',
+ '',
+ 'Have you done a manual backup and are ready to proceed? (y/N)'),
+ 'QUESTION_LARGE',
+ false))
{
$this->logSection('upgrade-sql', 'Upgrade aborted.');
@@ -107,7 +113,6 @@
$this->logSection('upgrade-sql', sprintf('Upgrading from version %s',
$this->initialVersion));
- // TODO Dump database
// TODO Do upgrade
}
@@ -119,4 +124,77 @@
$this->initialVersion = QubitPdo::fetchColumn($sql);
}
+
+ protected function parseDsn($dsn)
+ {
+ $params = array(
+ 'prefix' => null,
+ 'dbname' => null,
+ 'host' => 'localhost',
+ 'port' => '3307');
+
+ //$dsn = 'mysql:dbname=foo';
+
+ // Requires a prefix and dbname
+ if (!preg_match('/^(\w+):(.*)$/', $dsn, $matches) || !isset($matches[2]))
+ {
+ return;
+ }
+
+ $params['prefix'] = $matches[1];
+
+ foreach (explode(';', $matches[2]) as $tuple)
+ {
+ list($name,$value) = explode('=', $tuple);
+
+ $params[$name] = $value;
+ }
+
+ return $params;
+ }
+
+ protected function backupDatabase($database)
+ {
+ $backupSuccess = false;
+ $dsn = $this->parseDsn($database->getParameter('dsn'));
+
+ if (isset($dsn) && 'mysql' == strtolower($dsn['prefix']))
+ {
+ // MySQL backup
+ $backupName = 'db_'.date('YmdHis').'.sql.bak';
+ $this->logSection('backup', sprintf('Backing up database "%s" to %s',
$dsn['dbname'], $backupName));
+
+ $cmd = sprintf('mysqldump -u %s', $database->getParameter('username'));
+
+ // Passing a blank "-p" will prompt for password, which we don't want
+ if (null != $database->getParameter('password'))
+ {
+ $cmd .= sprintf(' -p%s', $database->getParameter('password'));
+ }
+
+ $cmd .= sprintf(' -h %s -P %s %s > %s',
+ $dsn['host'],
+ $dsn['port'],
+ $dsn['dbname'],
+ $backupName);
+
+ // Run backup command
+ system($cmd, $returned);
+
+ if (0 == $returned)
+ {
+ $this->logSection('backup', 'Database backup complete!');
+ $backupSuccess = true;
+ }
+ else
+ {
+ $this->logSection('backup', 'Database backup failed!', null, 'ERROR');
+ }
+ }
+
+ if ($backupSuccess)
+ {
+ return $backupName;
+ }
+ }
}
--
You received this message because you are subscribed to the Google Groups
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/qubit-commits?hl=en.