Restore MySQL database dump

(Brian Johnsen) #1

Hi gradlebrains!

I’m working on migrating our entire build environment to Gradle; build, test, deploy, backup, restore, the whole shebang. I’m having trouble with restore of a MySQL dump.

I made the backup work like this:

task backupdb(dependsOn: 'setupBackup') << {
 exec {
  commandLine mysqldump, 'db_name', '--host=localhost', '--user=user', '--password=pwd', '--single-transaction', '--quick', "--result-file=db_dump.sql"

Unfortunately, I find myself unable to get restore of a dump working I Gradle.

Command line restore is a no brainer:

mysql --user=user --password=pwd --quick
db_name < db_dump.sql

Unfortunately the “&lt” does not seem to work in Gradle (Groovy) - I had the same problem with the dump, but there I can use “result-file=…” - so I cannot get it to work in a build script.

I’ve tried several ways unsuccessful: 1)

exec {
  commandLine mysql, '-uuser', '-ppwd', '-q', '-Ddb_name', db_dump.sql
def proc = ["mysql", "-uuser", "-ppwd", "-Ddb_name"].execute()
 proc.withWriter { writer -> writer << “db_dump.sql” }

3…10) Several other ways (mostly permutations of above)…

Please, oh wise collective, help me.

Regards, Brian.

Environment: Gradle 1.0 Gradle build time: 12. juni 2012 00:56:21 UTC Groovy: 1.8.6 Ant: Apache Ant™ version 1.8.2 compiled on December 20 2010 Ivy: 2.2.0 JVM: 1.6.0_25 (Sun Microsystems Inc. 20.0-b11) OS: Windows 7 6.1 x86

(Luke Daley) #2

You should use the exec task, instead of the ‘exec {}’ action.

task restoreBackup(type: Exec) {
  commandLine mysql, '-uuser', '-ppwd', '-q', '-Ddb_name'
  standardInput file("db_dump.sql").newInputStream()

IO redirection (i.e. using ‘<’ on the CLI) is a shell feature, so will only work there.

(Brian Johnsen) #3

Worked like a charm!

Cheers mate!