You are here

Drupal backup script

First it is necessary to create a backup user on the database which has (only) read access to everything. This is because its password will be embedded in the backup script which reduces security. This can be done with:

BKUSR=backup
BKPW=backuppassword
BKDIR=/var/www/dbbackup
mkdir -p $BKDIR
echo "GRANT SELECT, LOCK TABLES ON *.* TO '$BKUSR'@'localhost' IDENTIFIED BY '$BKPW'; FLUSH PRIVILEGES;" | mysql -u root -p

In the backup script, first some variables are set up

BKUSR=backup
BKPW=backuppassword
BKDIR=/var/www/dbbackup
SITE=cfaw.info

The mybackup script attached below can be used in a backup script like this. An improved version (in Python) is mybackup.py which does not back up cache or search tables, reducing the size to about 10%-20% of the original.
You can get mybackup or mybackup.py with:
wget -N -O mybackup http://drup.org/files/mybackup. ; chmod +x mybackup
wget -N -O mybackup.py http://drup.org/files/mybackup.py.txt ; chmod +x mybackup.py

DELAY=60
/root/mybackup drupal1 $BKDIR $BKUSR $BKPW ; sleep $DELAY
/root/mybackup drupal2 $BKDIR $BKUSR $BKPW ; sleep $DELAY
etc...

Finally the data (including db backups) is rsync-ed across to the backup server.
The script deletes any previous database backup so that they don't accumulate here, (but they will accumulate in the rsync target below).

#RSREMOTE="backuser@sv.cfaw.info" #use this line for testing until .ssh/config set up
RSREMOTE=sv-backup
RSDIR=/backups/cfa.com
RSOPT=" --exclude *.tmp --exclude *.temp --archive -v --delete --compress --sparse --stats --progress --rsh=ssh"
RSDBOPT=" --archive -v --compress --sparse --stats --progress --rsh=ssh"
rsync $RSOPT /var/www/html/ $RSREMOTE:$RSDIR/www
rsync $RSOPT /home/ $RSREMOTE:$RSDIR/home
rsync $RSOPT /opt/drupal/ $RSREMOTE:$RSDIR/drupal
rsync $RSDBOPT $BKDIR/ $RSREMOTE:$RSDIR/db
rm -f $BKDIR/* #remove old backups

-Automate the login process using: http://drup.org/ssh-scp-without-password
-when it is all tested, ln -s /root/drupalback.sh /etc/cron.daily/
Note: If you are using the python script, you will have to make sure that you install the package:
MySQL-python under Red Hat/CentOS
python-MySQLdb under Debian/Ubuntu

AttachmentSize
Binary Data mybackup.308 bytes
Plain text icon mybackup.py.txt3.7 KB