The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Script to backup home directory and mysql

Discussion in 'Workarounds and Optimization' started by ale.ab, Jan 25, 2015.

  1. ale.ab

    ale.ab Registered

    Joined:
    Jul 9, 2013
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi,
    i search in this forum for a backup for home directory and mysql database.
    i wrote this script, if you is correct?
    i tried it and it seems to work...

    Code:
    #!/bin/sh
    destinazione=user@server.backup.tld
    cartella=/home/user/backup
    key=/root/.ssh/key
    
    mkdir -p /home/some_user/mysql_backup/
    rm -rf /home/some_user/mysql_backup/*
    
    # Add your backup dir location, password, mysql location and mysqldump location
    DATE=$(date +"%Y-%m-%d-%H-%M")
    BACKUP_DIR="/home/some_user/mysql_backup"
    MYSQL_USER="root"
    MYSQL_PASSWORD="password"
    MYSQL=/usr/bin/mysql
    MYSQLDUMP=/usr/bin/mysqldump
    
    # To create a new directory into backup directory location
    mkdir -p $BACKUP_DIR/$DATE
    
    # get a list of databases
    databases=`$MYSQL -u$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`
    
    # dump each database in separate name
    for db in $databases; do
    echo $db
    $MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$DATE/$db.sql.gz"
    done
    
    ssh -i $key $destinazione rm -rf $cartella/backup.6
    ssh -i $key $destinazione mv $cartella/backup.5 $cartella/backup.6
    ssh -i $key $destinazione mv $cartella/backup.4 $cartella/backup.5
    ssh -i $key $destinazione mv $cartella/backup.3 $cartella/backup.4
    ssh -i $key $destinazione mv $cartella/backup.2 $cartella/backup.3
    ssh -i $key $destinazione mv $cartella/backup.1 $cartella/backup.2
    ssh -i $key $destinazione cp -al $cartella/backup.0 $cartella/backup.1
    cd /var/cpanel/users
    for i in *; do
    ssh -i $key $destinazione mkdir -p $cartella/backup.0/$i/ ; 
    rsync --delete --recursive –-exclude 'tmp' -avz -e "ssh -i $key" /home/$i/* $destinazione:$cartella/backup.0/$i/ ;
    done
    exit
    
    my goal is replicate each home dicretory (exclude tmp folder) and in some user backup each mysql database.
    the script create an incremental backup in 6 folder.

    then i want to configure crontab every 2 or 4 hours...

    thank you in advance
     
  2. ale.ab

    ale.ab Registered

    Joined:
    Jul 9, 2013
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    the script sometimes crashes on mysql dump...
    but rsync works correctly...

    thank you
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    654
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    I've moved this thread to our "Workarounds" forum. Note that while we are happy to answer questions about cPanel/WHM development and our API, code review for these types of scripts generally fall outside our scope of support.

    Thank you.
     
Loading...

Share This Page