Auto Backup Server Files & MySQL To FTP Server

by lifeLinux on August 23, 2011

This is a simple script take to daily backup server files and MySQL databases to FTP server. Make sure you have lftp client installed. Type the following command if you are using Centos/Fedora/RedHat

# yum install lftp


If you are using Ubuntu/Debian

# sudo apt-get install install lftp

Create the folder /backup, enter

# mkdir /backup
# chmod 0600 /backup

Create backup.sh file, enter

# vi /backup/backup.sh

Add the following content to backup.sh

#!/bin/bash
# Daily backup script
# Backup Server Files & MySQL
# Support backup multiple directories and multiple databases
# Copyright (c) 2010-2011 lifeLinux 
# This script is licensed under GNU GPL version 2.0 or above

### Webserver directory
WD="/var/www/domain"

### MySQL Server Login Info ###
MUSER="root"
MPASS="MYSQL_ROOT_PASSWORD"
MHOST="localhost"
DBS="DATABASE_NAME_1 DATABASE_NAME_2"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
TAR="$(which tar)"

### Backup directory
BAK="/backup"

### FTP SERVER Login info ###
FTPU="FTP USER"
FTPP="FTP PASSWORD"
FTPS="FTP SERVER IP"
DAILY=$(date +"%u")

[ ! -d $BAK/$DAILY ] && mkdir -p $BAK/$DAILY || /bin/rm -f $BAK/$DAILY/*

for db in $DBS
do
	FILE=$BAK/$DAILY/mysql-$db.gz
	$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

WDS=`dir $WD`

for w in $WDS
do
	FILE=$BAK/$DAILY/source-$w.gz
	$TAR -zcvf $FILE $WD/$w/* 
done

lftp -u $FTPU,$FTPP -e "mkdir $DAILY;cd $DAILY; mput $BAK/$DAILY/*; quit" $FTPS

Set cron job

# crontab -e

If you want to auto backup at 3:00AM, add the following line to crontab

00 03 * * * /backup/backup.sh > /dev/null 2>&1

Related Posts:

{ 2 comments… read them below or add one }

Hubert October 19, 2011 at 2:34 am

WD=”/var/www/domain1 /var/www/domain2″ is this right?

Reply

lifeLinux October 20, 2011 at 8:15 am

You edit to: WD=”/var/www/″ and script will create backup for domain1 & domain2

Reply

Previous post:

Next post: