I recently created a BASH script that creates a g-zipped tarball and writes it to a NAS. It also checks the free space available on the NAS and deletes previous backups until it has enough space to store a new backup. Maybe someone will find it useful or will be inspired to improve it.
Variables to modify:
DATA = What directories you want to backup.
LOGDIR = Where you want the backup logs to be stored.
BACKUPMNT = The mount point of your NAS or backup device.
BACKUPDIR = The directory on the mount to store the backup.
#!/bin/bash
# Steve Horbachuk 2007
# tars directories and writes to NAS
#
DATA="/home /root /etc /usr/local /var/www /var/log"
LOGDIR="/var/log/my-backup"
BACKUPMNT="/mnt/linkstation"
BACKUPDIR="/mnt/linkstation/server"
MYDATE=`date +%F`
LASTFILESIZE=0
LASTFILENAME=""
echo "Backup started : `date`" > $LOGDIR/backup_$MYDATE.log
echo "-------------------------------------------------" >> $LOGDIR/backup_$MYDATE.log
#find the last file in the backup folder
#and store information about it.
for FILE in $BACKUPDIR/*; do
if [ -f $FILE ]; then
FILEINFO=(`ls -lk $FILE`)
LASTFILENAME=${FILEINFO[8]}
LASTFILESIZE=${FILEINFO[4]}
fi
done
#delete files until there is enough space to hold a file
#the same size as the last backup.
for FILE in $BACKUPDIR/*; do
DISKINFO=(`df -k | grep $BACKUPMNT`)
DISKFREE=${DISKINFO[3]}
if [ -f $FILE ]; then
if [[ "$LASTFILESIZE" -gt "$DISKFREE" ]]; then
rm -vf $FILE >> $LOGDIR/backup_$MYDATE.log
fi
fi
done
tar -czf $BACKUPDIR/backup_$MYDATE.tar.gz --exclude='*.iso' --exclude='*/tmp/*' $DATA >> $LOGDIR/backup_$MYDATE.log
echo "-------------------------------------------------" >> $LOGDIR/backup_$MYDATE.log
echo "Backup finished : `date`" >> $LOGDIR/backup_$MYDATE.log
Here is the mount in /etc/fstab:
//10.0.0.50/share /mnt/linkstation cifs rw,guest 0 0
Where “//10.0.0.50/share” is the SMB share on the NAS, and /mnt/linlkstation is the Linux mount point.