../../../../inc/ie-header.html

Ant Backup

Description

On every invocation this creates a TAR file containing all changed files since the last invocation and an exact copy of the files in a directory.

Requirements

Configuration

Inside the backup target add an invokation of the backup macro for every directory you want to backup.

<backup src="" dst=""/>

If you need to select only some of the sourcefiles, specify PatternSets and Selectors in a nested <src></src> tag.

<backup src="" dst="">
  <src>
    <include name="**/*.txt"/>
  </src>
</backup>

Don't forget to put purge.jar in Ant's lib directory.

Edit build.properties to your needs:

  • dst-root: Root folder for all backups
  • tar-root: Root folder for all tar files, may be the same as dst-root, eg. == tar-root=${dst-root} ==
  • tar-compression: tar compression method (none, gzip, bzip2)
  • tar-extension: tar file extension, set according to compression method (eg. tar, tar.gz, tar.bz2)
  • keep-tars: number of tar files to keep

Usage

$> ant -f /path/to/build.xml

Files

build.xml

build.xml
<?xml version="1.0"?>
<!-- Created on 10.12.2004 16:00:58 by jawe -->
<project name="backup" default="backup">
  <description>Backup to incremental tar files and an exact copy in directories.</description>
 
  <!-- initialization -->
 
  <property file="build.properties"/>
  <taskdef name="purge" classname="com.dallaway.ant.Purge"/>
 
  <!-- targets -->
 
  <target name="backup" depends="init" description="Do the Backup">
   <!-- Insert backup sources and targets below: -->
   <backup src="H:/temp" dst="test1">
     <src>
     <include name="**/*.txt"/>
     </src>
   </backup>
   <backup src="H:/temp" dst="test2">
     <src>
     <include name="*.jpg"/>
     </src>
   </backup>
   <!-- End of backup sources and targets -->
  </target>
 
  <target name="init">
   <tstamp>
     <format property="timestamp" pattern="yyyyMMddHHmmss"/>
   </tstamp>
  </target>
 
  <!-- macros -->
 
  <macrodef name="backup">
   <element name="src" optional="true"
     description="Source PatternSets and Selectors"/>
   <attribute name="src" description="Source Path"/>
   <attribute name="dst" description="Destination directory within ${dst-root}"/>
   <sequential>
     <mkdir dir="${tar-root}/@{dst}"/>
     <tar destfile="${tar-root}/@{dst}/${timestamp}.${tar-extension}"
     compression="${tar-compression}" longfile="gnu">
       <tarfileset dir="@{src}">
         <src/>
         <depend targetdir="${dst-root}/@{dst}"/>
         <type type="file"/>
       </tarfileset>
       <tarfileset dir="${dst-root}/@{dst}">
         <type type="file"/>
         <present targetdir="@{src}" present="srconly"/>
       </tarfileset>
     </tar>
     <mkdir dir="${dst-root}/@{dst}"/>
     <sync todir="${dst-root}/@{dst}" failonerror="false" verbose="true">
       <fileset dir="@{src}">
         <src/>
       </fileset>
     </sync>
     <purge keep="${keep-tars}">
       <fileset dir="${tar-root}/@{dst}">
         <include name="*.${tar-extension}"/>
       </fileset>
     </purge>
   </sequential>
  </macrodef>
 
</project>

build.properties

build.properties
dst-root = W:
tar-root = L:/backups
tar-compression = bzip2
tar-extension = tar.bz2
keep-tars = 30