DEV Community

Mohammed Brueckner
Mohammed Brueckner

Posted on • Updated on

Wrap up your data backups in a neat ANT script

Sometimes you just want to be able to create the same sort of packaged file bundles again and again. Like for backing up parts of your data. Not always do you have the comfort of having some client on your machine running that would constantly back your stuff up to OneDrive, GoogleDrive, Dropbox or whatever.

Just manually ZIPping everything up is no joy, even more so if you have plenty of data and only a small part of it really changes frequently.

I have a very easy solution for you, folks! Good old ANT to the rescue! It's a build tool widely known in the Java space, even though Maven certainly is wider spread nowadays.

I still like it though. Here is all you need:

  • A Java runtime - does not matter whether JDK or JRE, it can sit anywhere in your filesystem. No need to "install", just take a binary dump and unzip anywhere you want. No need to touch the Windows registry or anything alike, so skip the installer tools.
  • Likewise, get an ANT distribution. Unzip it anywhere in your file system.

And that is it, you are good to run the scripts I will show you in this post.

First, you will need a simple batch file to start off ANT.

On Windows, this could be contents of a start.cmd file (which is only a text file with a .cmd suffix):

@echo off
set JAVA_HOME=C:\mystuff\admin\jre
set ANT_HOME=C:\mystuff\admin\apache-ant-1.8.2-bin
%ANT_HOME%\bin\ant.bat -f compile.docs.xml -Dencoding="UTF-8" -logfile compile.files.log -v

The first variables point to the Java runtime as well to the ANT distribution.

compile.docs.xml is the build script ANT will process. In this case I am piping everything the output into a log file. -v stands for verbose, so you will get everything done at every step.

The compile.docs.xml may look like this:

<?xml version="1.0" encoding="UTF-8" ?>

<project name="etc pp" default="copy.over" basedir=".">


        <property name="target.dir" value="S:"/>
        <property name="src.dir" value="C:\whatever"/>
        <property name="src.dir2" value="C:\anotherfolder"/>
        <property name="src.dir3" value="C:\andanotherfolder"/>
        <property name="archive_name" value="bkp.zip"/>


<target name="copy.over">

     <tstamp>
      <format property="logtime" pattern="d-MMMM-yyyy" locale="de"/>
    </tstamp>

    <echo message="Time now: ${logtime}" />


    <delete file="${target.dir}/${logtime}/${archive_name}" failonerror="false" />

  <!-- for the includes: *.* will only take files, /** will take files AND sub directories, so take a pick there -->

    <zip destfile="${target.dir}/${logtime}/${archive_name}" encoding="UTF-8" keepcompression="true" >

      <zipfileset dir="${src.dir}">
        <include name="**/**"/>
        <exclude name="**/*.rar"/>
        <exclude name="**/*.zip"/>
      </fileset>

      <zipfileset dir="${src.dir2}" prefix="personal">
        <exclude name="**/*.pdf"/>
      </fileset>

      <zipfileset dir="${src.dir3}" prefix="learning">
        <exclude name="**/*.bat"/>
      </fileset>


    </zip>


</target>

</project>

This script will package file contents from different folders on your disk into one ZIP file located wherever you want. This example assumes a mapped drive S:, but it does not matter. The ${} pieces are variables, the script would start crunching through (the only) target that is copy.over.

ANT tasks are pretty versatile and you could write your own. I wrote once a couple of self-written tasks that would create a big ZIP file, encrypt it, split it up into chunks and then upload files to Dropbox.

These tasks would be used very similar to the task you have seen in the xml above. Pretty easy I'd say. Not too shabby for automating tedious tasks.

Talking about tasks. There are already plenty of task extensions, ANT-CONTRIB is very well known and handy.

Yep, ANT is oldschool but has very little requirements, runs on every operation system and even the scripts require usually very little change if at all from one OS to the next. Hope this is helpful to somebody!

Discussion (0)