[SHELL Script] - Backup WEB & DATABASES

Discussion in 'Tips/Tricks/Mods' started by vaio1, Sep 5, 2007.

?

Is it useful for you a backup integrated on the ISPConfig?

Poll closed Dec 4, 2007.
  1. Yes it is essential!

    66.7%
  2. Yes, but is better write our own shell script (more flexible)

    33.3%
  3. No, it is a stupid thing!

    0 vote(s)
    0.0%
  1. vaio1

    vaio1 ISPConfig Developer

    Hi guys,

    I have created a little script on the basis of the: http://www.howtoforge.com/forums/showpost.php?p=57761&postcount=2

    This is a shell script that helps you to backup of all WEB SITES & DATABASES and compress them in various GZip file.

    All gzip files are located in /home/backup/date[2007/09/05]/

    The ISPCONFIG users prefix must be "web". So we have some databases like these:

    • web1_db1
    • web2_db1

    and folders like these:

    • web1
    • web2

    Now we must create a text file in the root directory and copy and paste this script. Remember to enable the file to the execution [rwxr-xr-x];

    Code:
    #!/bin/bash
     
    DATE=`date +%F`
    
    COPY_TO=/home/backups/$DATE
    COPY_FROM=/var/www
    
    DBBACKUPNAME="mysql-"$DATE
    WEBBACKUPNAME="website-"$DATE
    USER=your_username
    PASSWORD=your_password
    HOST=your_host
    
    mkdir -p $COPY_TO
    
    echo "";
    echo "************************************";
    echo "       DATABASES BACKUP";
    echo "************************************";
    echo "";
    for db in $(echo 'SHOW DATABASES;'|mysql -u$USER -p$PASSWORD -h$HOST|grep -v '^Database$'|grep "^web"); 
    do
    	  mysqldump \
                  -u$USER -p$PASSWORD -h$HOST \
                  -Q -c -C --add-drop-table --add-locks --quick --lock-tables \
                  $db | gzip --best -c > $COPY_TO/$DBBACKUPNAME-$db.sql.gz;
    		echo "Backup of" $db;
    done;
    
    echo "";
    echo "************************************";
    echo "       WEB BACKUP";
    echo "************************************";
    echo "";
    
    echo "Wait a moment please...";
    for x in $(find $COPY_FROM -maxdepth 1 -name "web*" -type d -print0 | xargs -0)
    do
      tar -cpvzf $COPY_TO/$WEBBACKUPNAME-$(basename $x).tar.gz $x &> /dev/null
    done;
    echo "Done!";
    
    [root@myserver1 ~]# ./make-backup

    ************************************
    DATABASES BACKUP
    ************************************

    Backup of web11_db1
    Backup of web13_db1
    Backup of web13_db2
    Backup of web13_db3
    Backup of web13_db4
    Backup of web14_db1
    Backup of web14_db2
    Backup of web16_db1
    Backup of web18_db1
    Backup of web7_db1
    Backup of web7_db2
    Backup of web9_db1

    ************************************
    WEB BACKUP
    ************************************

    Wait a moment please...
    Done!
    [root@myserver1 ~]#


    Any improve of this script is appreciated.
     
    Last edited: Sep 5, 2007

Share This Page