How To take Backup MySQL Databases and Web server Files to a FTP Server on Linux/CentOs





This is a simple backup solution for people who run their own web server and MySQL database server on a dedicated or VPS server. Most dedicated hosting provider provides backup service using NAS or FTP servers. These service providers will hook you to their redundant centralized storage array over private VLAN. Since, I manage couple of boxes, here is my own automated solution. If you just want a shell script, go here (you just need to provided appropriate input and it will generate FTP backup script for you on fly, you can also grab my php script generator code).

Making Incremental Backups With tar

You can make tape backups. However, sometime tape is not an option. GNU tar allows you to make incremental backups with -g option. In this example, tar command will make incremental backup of /var/www/html, /home, and /etc directories, run:
# tar -g /var/log/tar-incremental.log -zcvf /backup/today.tar.gz /var/www/html /home /etc
Where,
  • -g: Create/list/extract new GNU-format incremental backup and store information to /var/log/tar-incremental.log file.

Making MySQL Databases Backup

mysqldump is a client program for dumping or backing up mysql databases, tables and data. For example, the following command displays the list of databases:
$ mysql -u root -h localhost -p -Bse 'show databases'
Output:
Enter password:
brutelog
cake
faqs
mysql
phpads
snews
test
tmp
van
wp
Next, you can backup each database with the mysqldump command:
$ mysqldump -u root -h localhost -pmypassword faqs | gzip -9 > faqs-db.sql.gz

Creating A Simple Backup System For Your Installation

The main advantage of using FTP or NAS backup is a protection from data loss. You can use various protocols to backup data:
  1. FTP
  2. SSH
  3. RSYNC
  4. Other Commercial solutions
However, I am going to write about FTP backup solution here. The idea is as follows:
  • Make a full backup every Sunday night i.e. backup everything every Sunday
  • Next backup only those files that has been modified since the full backup (incremental backup).
  • This is a seven-day backup cycle.

Our Sample Setup

   Your-server     ===>       ftp/nas server
IP:202.54.1.10   ===>       208.111.2.5
Let us assume that your ftp login details are as follows:
  • FTP server IP: 208.111.2.5
  • FTP Username: nixcraft
  • FTP Password: somepassword
  • FTP Directory: /home/nixcraft (or /)
You store all data as follows:
=> /home/nixcraft/full/mm-dd-yy/files - Full backup
=> /home/nixcraft/incremental/mm-dd-yy/files - Incremental backup

Automating Backup With tar

Now, you know how to backup files and mysql databases using the tar and mysqldump commands. It is time to write a shell script that will automate entire procedure:
  1. First, our script will collect all data from both MySQL database server and file system into a temporary directory called /backup using a tar command.
  2. Next, script will login to your ftp server and create a directory structure as discussed above.
  3. Script will dump all files from /backup to the ftp server.
  4. Script will remove temporary backup from /backup directory.
  5. Script will send you an email notification if ftp backups failed due to any reason.
You must have the following commands installed (use yum or apt-get package manager to install ftp client called ncftp):
  • ncftp ftp client
  • mysqldump command
  • GNU tar command
Here is the sample script:
  1. #!/bin/sh
  2. # System + MySQL backup script
  3. # Full backup day - Sun (rest of the day do incremental backup)
  4. # Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
  7. # ---------------------------------------------------------------------
  8. ### System Setup ###
  9. DIRS="/home /etc /var/www"
  10. BACKUP=/tmp/backup.$$
  11. NOW=$(date +"%d-%m-%Y")
  12. INCFILE="/root/tar-inc-backup.dat"
  13. DAY=$(date +"%a")
  14. FULLBACKUP="Sun"
  15. ### MySQL Setup ###
  16. MUSER="admin"
  17. MPASS="mysqladminpassword"
  18. MHOST="localhost"
  19. MYSQL="$(which mysql)"
  20. MYSQLDUMP="$(which mysqldump)"
  21. GZIP="$(which gzip)"
  22. ### FTP server Setup ###
  23. FTPD="/home/vivek/incremental"
  24. FTPU="vivek"
  25. FTPP="ftppassword"
  26. FTPS="208.111.11.2"
  27. NCFTP="$(which ncftpput)"
  28. ### Other stuff ###
  29. EMAILID="admin@theos.in"
  30. ### Start Backup for file system ###
  31. [ ! -d $BACKUP ] && mkdir -p $BACKUP || :
  32. ### See if we want to make a full backup ###
  33. if [ "$DAY" == "$FULLBACKUP" ]; then
  34. FTPD="/home/vivek/full"
  35. FILE="fs-full-$NOW.tar.gz"
  36. tar -zcvf $BACKUP/$FILE $DIRS
  37. else
  38. i=$(date +"%Hh%Mm%Ss")
  39. FILE="fs-i-$NOW-$i.tar.gz"
  40. tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
  41. fi
  42. ### Start MySQL Backup ###
  43. # Get all databases name
  44. DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
  45. for db in $DBS
  46. do
  47. FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
  48. $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
  49. done
  50. ### Dump backup using FTP ###
  51. #Start FTP backup using ncftp
  52. ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
  53. mkdir $FTPD
  54. mkdir $FTPD/$NOW
  55. cd $FTPD/$NOW
  56. lcd $BACKUP
  57. mput *
  58. quit
  59. EOF
  60. ### Find out if ftp backup failed or not ###
  61. if [ "$?" == "0" ]; then
  62. rm -f $BACKUP/*
  63. else
  64. T=/tmp/backup.fail
  65. echo "Date: $(date)">$T
  66. echo "Hostname: $(hostname)" >>$T
  67. echo "Backup failed" >>$T
  68. mail -s "BACKUP FAILED" "$EMAILID" <$T
  69. rm -f $T
  70. fi
More Details Click
          Autherby-cyberciti.biz

2 comments:

  1. This is the very informative blog to get the solutions of all your problems and queries which you are facing in taking backup of mysql database and if you have trading regarding queries then use Stock Tips.

    ReplyDelete
  2. Such a great and informative article. Thanks for sharing.
    data entry service provider

    ReplyDelete