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.

Backup MySQL for all clients automatically

Discussion in 'General Discussion' started by cawheele, Dec 9, 2004.

  1. cawheele

    cawheele Registered

    Joined:
    Nov 14, 2004
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Hello everyone,

    I was just wondering how I would go about backing up the databases for all of my clients without logging in to each account manually. I have a reseller account.

    Thanks,

    Chris
     
  2. sawbuck

    sawbuck Well-Known Member

    Joined:
    Jan 18, 2004
    Messages:
    1,367
    Likes Received:
    5
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
  3. sleddog

    sleddog Active Member

    Joined:
    Jun 13, 2004
    Messages:
    44
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Labrador, Canada
    Here's another possibility, a little Bash shell script I wrote and which I've been using for a couple years. It could be nicer and fancier, but it does what I need :)

    - Provide it the root MySQL username/password
    - Enter a list of databases
    - Set the number of days you want to keep backups around
    - Chown it to a trusted user, or root.
    - Chmod it 700
    - Create the backup directory, chown/chmod own it the same as the script
    - Run it manually once (as the owner or root) to check things out
    - Schedule a cronjob (as the owner or root, as appropriate) to run once nightly.

    Code:
    #!/bin/bash
    # Script to backup mySQL databases by [sleddog]
    
    # --- User options - must be edited ---
    
    # A space-delimited list of databases to be backed up:
    db_list="database1 database2 database3"
    
    db_username=root
    db_password=password
    backup_dir=/some/directory/backup
    days_keep=7
    
    # --- Do not edit below here, unless of course you know what you're doing ---
    
    today=`date '+%Y%m%d'`
    oldday=`date '+%Y%m%d' --date "$days_keep days ago"`
    
    if [ -e $backup_dir ];
      then
        cd $backup_dir
        for dbname in $db_list;
          do mysqldump -u $db_username -p$db_password --opt $dbname > $dbname.sql ;
        done
        tar -cf $today.tar *.sql && gzip -9 $today.tar
        if [ -e $oldday.tar.gz ];
          then
            rm -f $oldday.tar.gz
        fi
        rm -f *.sql
      else
        echo Backup directory does not exist!
    fi
    
    chmod 600 $backup_dir/*.gz
    
     
Loading...

Share This Page