Website Backup

Submitted by Lo Yuk Fai on Tue, 2008-09-16 07:23

A script is used to backup the site files and database daily with automatic rotation. Forgot where I found the codes so, sorry for not able to give credit.

P.S. Use a cron job to run it daily.

#!/bin/sh

#
#set date and variables
#

DATE=`date +%Y%m%d`
BACKUPNAME="byfaicom"

cd ~

#
#backup site
#

tar -cvf ~/backup/$BACKUPNAME-site-$DATE.tar public_html

cd ~/backup

bzip2 -9 $BACKUPNAME-site-$DATE.tar

#
#backup mysql databases
#

mysqldump --opt -u{username} -p{password} {database name} > ~/backup/$BACKUPNAME-drupaldb-$DATE.sql

tar -cvzf $BACKUPNAME-drupaldb-$DATE.tar $BACKUPNAME-drupaldb-$DATE.sql

rm *sql

#delete old backups

find ~/backup/ -mtime +31 | xargs rm -f

Edit (2009-06-29): The cron command is "/bin/bash ~/bin/backup.sh 2>&1 >/dev/null" without the quotes.

( categories: )