Martin Magakian
Martin Magakian

Reputation: 3766

How do I dump a MySQL database using ant?

I couldn't find any information about how to dump a MySQL database with an ant task.

Do I have to create my own task to do this?

ANT script ===generate==> myDataBase.sql

Upvotes: 10

Views: 6139

Answers (5)

antenia_jlucas
antenia_jlucas

Reputation: 1

When I use this solution https://stackoverflow.com/a/1410859/19735954, my dump takes 3 hours to complete while it only takes 20 minutes using the mysqldump command directly (backup weighs 20Go).

Direct command :

mysqldump --login-path=mysql_serv --skip-comments --max-allowed-packet=128M --extended-insert mysql_database > /road/to/output/file.sql

Ant task :

<target name="backupBDD">
    <exec executable="mysqldump" output="/road/to/output/file.sql">
        <arg value="--login-path=mysql_serv"/>
        <arg value="--skip-comments" />
        <arg value="--max-allowed-packet=128M" />
        <arg value="--extended-insert" />
        <arg value="mysql_database" />
    </exec>
</target>

How to explain this difference ?

Upvotes: 0

Adam Morgan
Adam Morgan

Reputation: 495

If you wanna make it data driven, try this guy out using the ant sql task:

<macrodef name="sql-retrieve-table-schema">
    <attribute name="schema-name"/>
    <attribute name="table-name"/>
    <attribute name="connection-url"/>
    <attribute name="output-file"/>
    <sequential>
        <sql userid="username" url="@{connection-url}"  classpathref="compile.classpath"
            password="apassword" driver="com.mysql.jdbc.Driver" print="true" output="@{output-file}"
            showheaders="false" showtrailers="false">
            SHOW CREATE TABLE @{table-name};
        </sql>
    </sequential>
</macrodef>

Upvotes: 0

Ricardo Martins
Ricardo Martins

Reputation: 6003

And to import some sql file using ant, that is also useful:

    <exec executable="mysql" failonerror="true" input="file.sql">
        <arg value="-u ${user}" />  
        <arg value="-p${pass}" />  
        <arg value="-h ${host}" />  
        <arg value="-P 3306" />  
        <arg value="-D ${database}" />  
    </exec>

*note that the correct is -ppassword and not -p password

or:

    <exec executable="mysql" failonerror="true" input="file.sql">
        <arg value="--user=${user}" />  
        <arg value="--password=${pass}" />  
        <arg value="--host=${host}" />  
        <arg value="--port=3306" />  
        <arg value="--database=${database}" />  
    </exec>

It's also nice because it doesn't use external libs/sql drivers like org.gjt.mm.mysql.Driver.

Upvotes: 9

Tom van Zummeren
Tom van Zummeren

Reputation: 9220

Create a target that runs the "mysqldump" command like this:

<target name="dump-database">  
    <exec executable="mysqldump" output="database-dump.sql">  
        <arg value="--user=username" />  
        <arg value="--password=password" />  
        <arg value="--host=localhost" />  
        <arg value="--port=3306" />  
        <arg value="mydatabase" />  
    </exec>  
</target>  

Now you can make the dump by executing ant dump-database

Upvotes: 23

Victor Sorokin
Victor Sorokin

Reputation: 12006

You can use Exec task, which will start your script which will perform all actions necessary for dumping (or whatever).

Upvotes: 2

Related Questions