#!/bin/sh
# This script is designed to be called from a cron job, once per night.
# It tars the entire filesystem, and transports it via the network to a
# Samba share elsewhere.
#
# Paul Kreiner 12/1/2003

# Set some authentication options:
SMB_USERNAME="someuser"
SMB_PASSWORD="a_sekret"
SMB_DOMAIN="SOMEDOMAIN"

# The server name and share name:
SMB_SERVER="servername"
SMB_SHARE="share name"

# List of mount points to back up, path to log file, name/path of tar file:
FILESYSTEMS="/ /boot /var /home /usr /tmp /mnt/storage"
LOGFILE="/var/log/backup-smb.log"
TARFILE="linux_system_backup.tgz"


# Here we go...
# First, we create the tar-file, streaming over stdout to smbclient, so it
# gets created directly on the SMB share.
# Next, assuming the backup went successfully, we then rename the new/temp
# backup file, overwriting the previous backup file.  This means we should
# always have exactly ONE complete backup file stored on the SMB share.
# We redirect FD2 for both processes out to a local log file.

(
  ( /bin/tar -clvz \
      $FILESYSTEMS \
      2>>"$LOGFILE" | \
    /usr/bin/smbclient \
      "//$SMB_SERVER/$SMB_SHARE" \
      "$SMB_PASSWORD" \
      -U "$SMB_USERNAME" \
      -W "$SMB_DOMAIN" \
      -c "put - $TARFILE.temp"
  ) && (
    /usr/bin/smbclient \
      "//$SMB_SERVER/$SMB_SHARE" \
      "$SMB_PASSWORD" \
      -U "$SMB_USERNAME" \
      -W "$SMB_DOMAIN" \
      -c "rm $TARFILE ; ren $TARFILE.temp $TARFILE"
  )
) 2>>"$LOGFILE"


