So I wrote this shell script for a client which needed a simple solution for daily backups. It will:

1. Check if the USB HDD is mounted, if it is not, it will mount it.

2. It will start writing it is own log file, will check for files older than 60 days, if it finds, it will delete it. Therefore the backups will never exceed the space in the external HDD (500G HDD).

I am using “tar.bz2” for really good compression and setting the arguments to create files in the format “DAY-MONTH-YEAR”

It’s working in a server for a small office (trusted users) and all the files are shared in the same directory in a samba server (/files), which are backuped in a daily basis in a rotational system of back to 60 days older files, as I have already explained.

I know I  have to “clean it up” my code, especially removing all this static code and insert them into variables for a better reading AND eventually editing. I will work on it in the next days.

Well, I hope it will be useful to anyone on the net. Let me know if you have used it anywhere and if it is working for you (or not).  🙂

#!/bin/bash
################# BACKUP SCRIPT #####################
chkmnt() {

cat /etc/mtab | grep /mnt/usb >/dev/null
 if [ "$?" -eq "0" ]; then
 echo mounted &&
 start_backup
 else
 date >> /home/backup/backup_hist.txt && echo "cannot be mounted, exiting..." >> /home/backup/backup_hist.txt
 exit
 fi
}

######################################

mount_backup() {

echo Mounting disk...

mount /mnt/usb
}

######################################

start_backup() {

echo Starting backup... &&

echo "::Starting backup::" >> /home/backup/backup_hist.txt && date >> /home/backup/backup_hist.txt

find /mnt/usb/ -mtime +60 -type f -exec rm -rf {} \; && tar -cvjf /mnt/usb/`date +%d-%m-%Y.tar.bz2` /files &&
umount /mnt/usb &&

echo "::End of backup::" >> /home/backup/backup_hist.txt
date >> /home/backup/backup_hist.txt
echo "-----------------------" >> /home/backup/backup_hist.txt &&

echo Backup concluded.

}

#####################################

mount_backup

chkmnt