groovy中备份和恢复mysql数据库

Gexrior 2011-07-25

groovy/Java执行的windows命令行如果包含尖括号会出现错误, 可以采取以下两种方法解决:

1) 加 "cmd", "/C"到命令前面。

备份mysql数据库采用

def myCommandString = ["cmd","/C","C:/Program Files/MySQL/MySQL Server 5.5/bin/mysqldump", "-u****","-p****","dbname>c:/backup.sql"]
Process proc = myCommandString.execute()
 

恢复mysql数据库采用

def myCommandString = ["cmd","/C","C:/Program Files/MySQL/MySQL Server 5.5/bin/mysql", "-u****","-p****","dbname<c:/backup.sql"]
 

2) 用其他命令代替<>.

备份mysql数据库采用

def myCommandString = "C:/Program Files/MySQL/MySQL Server 5.5/bin/mysqldump  --opt  -u*** -p*** dbname --result-file=c:/backup.sql"
Process proc = myCommandString.execute()
proc.waitFor()

恢复mysql数据库采用

def p2 = "C:/Program Files/MySQL/MySQL Server 5.5/bin/mysql -u*** -p*** dbname".execute()
p2.withWriter { writer ->
      writer << 'source c:/backup.sql'
}
p2.waitFor()

相关推荐