linuxshellbacula

How to write a script for backup using bacula?


I am very new to this shell scripting and bacula. I want to create a script that schedules the backup using bacula?

How to do that?

Any lead is appreciated?

Thanks.


Solution

  • If you are going to administer your own Linux system, learn bash. The man page is really quite detailed and useful. Do man bash.

    If you are really new to Linux and command-lines, administering bacula is not for newbies. It is a fairly comprehensive backup system, for multiple systems, with a central database, which means that is is also complex.

    There are much simpler tools available on Linux to perform simple system backups, which are just as reliable. If you just want to backup you home directory, tar or zip are excellent tools. In particular, tar can do both full backups and incremental backups.

    Assuming that you really want to use bacula and have enough information to write a couple of simple scripts, then even so, the original request is ambiguous.

    Do you mean schedule a periodic cron job to accomplish backups unattended? Or, do you mean to schedule an single invocation of bacula at a determined time and date?

    In either case, it's a good idea to create two simple scripts: one to perform a full backup, and one to perform an incremental backup. The full backup should be run, say, once a week or once a month, and the incremental backup should be run every day, or once a week -- depending on how often your system data changes.

    Most modest sites undergoing daily usage would have a daily incremental backup with a full backup on the weekends (say, Sunday). This way, if the system crashed on, say, Friday, you would need to recover with the most recent full backup (on the previous Sunday), and then recover with each day's incremental backup (Mon, Tue, Wed, Thu). You would probably lose data changes that had occurred on the day of the crash.

    If the rate of data change was hourly, and recovery at an hourly rate was important, then the incrementals should be scheduled for each hour, with full backups each night.

    An important consideration is knowing what, exactly, is to be backed up. Most home users want their home directory to be recoverable. The OS root and application partitions are often easily recoverable without backups. Alternatively, these are backed up on a very infrequent schedule (say once a month or so), since they change must less frequently than the user's home director.

    Another important consideration is where to put the backups. Bacula supports external storage devices, such as tapes, which are not mounted filesystems. tar also supports tape archives. Most home users have some kind of USB or network-attached storage that is used to store backups.

    Let's assume that the backups are to be stored on /mnt/backups/, and let's assume that the user's home directory (and subdirectories) are all to be backed up and made recoverable.

    % cat <<EOF >/usr/local/bin/full-backup
    #!/bin/bash
    # full-backup SRCDIRS [--options]
    # incr-backup SRCDIRS [--options]
    #
    # set destdir to the path at which the backups will be stored
    # each backup will be stored in a directory of the date of the
    # archive, grouped by month.  The directories will be:
    #
    # /mnt/backups/2014/01
    # /mnt/backups/2014/02
    # ...
    # the full and incremental files will be named this way:
    #
    # /mnt/backups/2014/01/DIR-full-2014-01-24.192832.tgz
    # /mnt/backups/2014/01/DIR-incr-2014-01-25.192531.tgz
    # ...
    # where DIR is the name of the source directory.
    #
    # There is also a file named ``lastrun`` which is used for
    # its last mod-time which is used to select files changed 
    # since the last backup.
    
    $PROG=${0##*/}              # prog name: full-backup or incr-backup
    
    destdir=/mnt/backup
    now=`date +"%F-%H%M%S"`
    monthdir=`date +%Y-%m`
    dest=$destdir/$monthdir/
    set -- "$@"
    while (( $# > 0 )) ; do
      dir="$1" ; shift ; 
      options=''                                  # collect options
      while [[ $# -gt 0 && "x$1" =~ x--* ]]; do   # any options?
        options="$options $1"
        shift
      done
      basedir=`basename $dir`
      fullfile=$dest/$basedir-full-$now.tgz
      incrfile=$dest/$basedir-incr-$now.tgz
      lastrun=$destdir/lastrun
      case "$PROG" in
        full*) archive="$fullfile" newer=                   kind=Full ;;
        incr*) archive="$incrfile" newer="--newer $lastrun" kind=Incremental ;;
      esac
      cmd="tar cfz $archive $newer $options $dir"
      echo "$kind backup starting at `date`"
      echo ">> $cmd"
      eval "$cmd"
      echo "$kind backup done at `date`"
      touch $lastrun  # mark the end of the backup date/time
    exit
    EOF
    (cd /usr/local/bin ; ln -s full-backup incr-backup )
    chmod +x /usr/local/bin/full-backup
    

    Once this script is configured and available, it can be scheduled with cron. See man cron. Use cron -e to create and edit a crontab entry to invoke full-backup once a week (say), and another crontab entry to invoke incr-backup once a day. The following are three sample crontab entries (see man 5 crontab for details on syntax) for performing incremental and full backups, as well as removing old archives.

    # run incremental backups on all user home dirs at 3:15 every day
    15 3 * * *   /usr/local/bin/incr-backup /Users
    # run full backups every sunday, at 3:15
    15 3 * * 7   /usr/local/bin/full-backup /Users
    # run full backups on the entire system (but not the home dirs) every month
    30 4 * 1 7   /usr/local/bin/full-backup / --exclude=/Users --exclude=/tmp --exclude=/var
    # delete old backup files (more than 60 days old) once a month
    15 3 * 1 7   find /mnt/backups -type f -mtime +60 -delete
    

    Recovering from these backups is an exercise left for later.

    Good luck.