Dienstag, 17. April 2012

[Bash Scripting] Doing some rsync backups

I know there are quite some tools a round to do backups, but i was in the mood to write a bash script so here's how i currently do backups
#!/bin/bash

ACTKW=`date +KW_%U_%A`
ACT=`date +%U`
DELKW=$[$ACT - 2]
if [ `ls -l /home/rsync/*${DELKW}*| wc -l` -gt 0 ]; then
echo "Deleting KW ${DELKW} backup files...."
rm -rf /home/rsync/KW_${DELKW}_*
fi
echo "Making actual Backup..."
echo "Making dir ${ACTKW}"
mkdir /home/rsync/${ACTKW}
if [ $? -eq 0 ]; then
echo "Syncing"
rsync -b -r <user>@<remotehost>:/var/customers/webs/ /home/rsync/${ACTKW}
fi

echo "...done"

So what does this script do?

First of all, the design of an backup

  1. Backups are made to /home/rsync

  2. The script creates a new directory which name  is based on KW_<number of calendar week>_<day>


What happens

  1. We compare the old backups on there calendar week with the actual. Than we erase all data which is older than 1 week.

  2. We create a new directory based on  KW_<number of calendar week>_<day>  for today this would be KW_16_Tuesday

  3. All data from the froxlor webspace directory on the remote server are rsynced to this  new directory


In Addition to that: I do a mysqldump on the server
mysqldump --all-databases -c --single-transaction --master-data -p<dbpassword> > /var/customers/webs/db_backup"`date +%A%U`".sql

via cronjob, so i have a backup of the database too.