Do you regularly backup your data off of your server? Many people setup database dumps and then leave them on their server. This doesn’t really help you in the case of the server dying on you. I’m going to share with you the script that I have that backs up all of the databases on this server as well as doing a full/incremental file backup as well. Wrapping it all up we will then FTP it off to a secondary location for storage.
I’ll post code snippets and then explain them.
There are quite a few things that you’ll have to do beforehand as this script, while mostly is ‘drop in’ still should require some pre-work on your end.
What you’ll need.
- *FTP ip/user/pass of destination server
- *MySQL user/pass/host
- *Folders to backup
As I discovered while writing this, some things may need adjusted for your system and I’ll flag them as we go through the script.
First we will need to setup the configuration variables of the script.
### Setup Environment ### DIRS="/home/user/public_html" BACKUP=/tmp/backup.$$ NOW=$(date +"%d-%m-%Y") INCFILE="/root/tar-inc-backup.dat" DAY=$(date +"%a") FULLBACKUP="Sun" ### MySQL Config ### MUSER="user" MPASS="pass" MHOST="localhost" MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" GZIP="$(which gzip)" ### FTP Config ### FTPD="/incremental" FTPU="username" FTPP="pass" FTPS="hostname" ### Email Config ### EMAILID="email@domain.com"
DIRS: Separate paths with spaces.
BACKUP: Where to store backup files temporarily during the script.
INCFILE: Is what we will use to control the incremental backups.
FULLBACKUP: Day of the week to run your full file backups.
Let me point something out about the MySQL configuration. It is quite easy to just slap your root account in there and let it go to town. Try NOT to do that if you can help yourself. Make a database backup user with the proper privileges and access to the databases you want to back up. Those privileges are, SELECT, SHOW DATABASES, LOCK, RELOAD.
The rest of the configuration is self-explanatory. On to the next section.
### FS Backup ### [ ! -d $BACKUP ] && mkdir -p $BACKUP || : ### Determine which backup to run ### if [ "$DAY" == "$FULLBACKUP" ]; then i=$(date +"%Hh%Mm%Ss") FTPD="/full" FILE="fs-full-$NOW.tar.gz" tar -zcvf $BACKUP/$FILE $DIRS else i=$(date +"%Hh%Mm%Ss") FILE="fs-i-$NOW-$i.tar.gz" tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS fi ### Start MySQL Backup ### # Get all databases name DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')" for db in $DBS do FILE=$BACKUP/mysql-$db.$NOW-$i.gz $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE done
This is the meat of the script. If you are reading this you can most likely read that and see what it is doing.
We are going to create the backup directory, and then backup the files based on what day it is. Then we will dump all the tables that the user account has access to a gzipped text file.
Time for the next section.
(sorry couldn’t get this section working right with the code highlighter.)
### FTP Backup to Remote Server ###
#Start FTP backup using ncftp
ftp $FTPS<
bin
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
Here is something that you will need to test on your system. This is what you need to figure out. How can you, in one line, open and authenticate to a remote FTP server. This example looks very simple, but it requires setting up an .netrc file with the authentication information. I don’t want to get into .netrc config but it is very simple and documented all over the Internet. I had to create an entry for the remote server, and then I could connect like you see in the script. Some people may need to pass user/pass/host on one line, etc. Figure it out for your system and change the line above that is right after the comments.
The rest is basic FTP commands, easy enough.
### Backup Fail Check ### if [ "$?" == "0" ]; then rm -f $BACKUP/* else T=/tmp/backup.fail echo "Date: $(date)">$T echo "Hostname: $(hostname)" >>$T echo "Backup failed" >>$T mail -s "BACKUP FAILED" "$EMAILID" <$T rm -f $T fi
This is just the wrap up that will email you if the backup bombed. You can add all the info that you want into that email if you choose to.
Save all those code chunks as a new shell script and start testing. It's working great for me and hopefully it helps you out.