Backing up Databases

Discussion in 'Other Programming' started by midlandi, Apr 22, 2008.

Thread Status:
Not open for further replies.
  1. midlandi Can you smell that?

    Member Since:
    Jan 22, 2007
    Message Count:
    4,769
    Likes Received:
    36
    Location:
    Uk
    I know its easy enough to back up databases, its more of a chore than anything to ensure its done regulally.
    Are there any applications out there that can be installed to do it automatically at pre set intervals?
  2. gkd Active Member

    Member Since:
    Apr 16, 2007
    Message Count:
    1,738
    Likes Received:
    16
    Location:
    UK
    I would like to know if there is one that can do it automatically also. I belive it can be done through cronjobs but not sure
  3. ST-Mike TriPHP Contributor

    Member Since:
    Oct 18, 2007
    Message Count:
    387
    Likes Received:
    13
    Location:
    Liverpool, UK
    What sort of databases?

    I wrote a small script in bash to backup each database on our mysql database server and it is ran as a cron every 4 hours. I'm unsure if it's completely efficient but it does the job just fine for me. The script also then deletes backups older than 28 days.. they are sorted by /rootbackupdir/databasename/backup_time.sql.gz

    Code (text):
    #!/bin/bash
    username="mysql_root_user_or_user_with_access"
    password="mysql_password"
    for i in `echo "show databases" | mysql -u$username -p$password|grep -v Database`; do
    filestamp=$(date '+%d')-$(date '+%m')-$(date '+%y')_$(date '+%H'):$(date '+%M'):$(date '+%S')
    rootdir=/backup
    dir=$rootdir/$i
    file=$dir/$filestamp.sql.gz
    echo Starting backup for $i as $filestamp
    if ! [ -d $dir ]; then
    echo -- Directory does not exist, creating it at $dir
    mkdir $dir
    fi
    mysqldump -u$username -p$password $i | gzip -9 > $file
    chmod go-xrw $file
    echo -- File stored at $file
    done
    echo Deleting backup files created or changed over 28 days ago
    find $rootdir -type f -ctime +28 -exec rm -rf {} \;
    echo Done!
    Now now, doing this isn't too great if you have absolutely huge databases being accessed very rapidly I don't think... in that case you have to look at other setups, such as (if you have one) having the backups made on the slave server. The slave mysql server is stopped [well, just replication from the master], backups are then taken from there and stored, then the slave replication starts again and it catches up with the master. This then causes no disruption to applications using the mysql server and prevents corrupt backups.

    In the simplest form, you can use mysqldump to backup mysql databases on a cron to a file containing the timestamp at that point.. then have another cron removing old files.

    In our case, we just use the above script every 4 hours on a cron and then every 12 hours the backup directory is rsyncd with remote backup space in another datacenter (in another country, far away!). As our database server gets busier, we may move the load around to other servers or we may setup slaves - where backups can be taken from without corruption.
  4. hitman New Member

    Member Since:
    Jul 12, 2007
    Message Count:
    946
    Likes Received:
    1
    I have an empty forum so not a problem for me yet hehe
  5. gkd Active Member

    Member Since:
    Apr 16, 2007
    Message Count:
    1,738
    Likes Received:
    16
    Location:
    UK
    Nice one - Thanks :):
  6. ST-Mike TriPHP Contributor

    Member Since:
    Oct 18, 2007
    Message Count:
    387
    Likes Received:
    13
    Location:
    Liverpool, UK
    Incase anybody is wondering, just place that script into a file. Then enter the mysql username/password with access to the databases which need backing up. Make sure /backup exists (or you can change the directory in the script) and is writeable by the user you intend to run the script.. then set the script so that it can be executed (chmod +x script_name.sh). You can then run the file as a cron, do a test run first in your console though.. I'm unsure if it will work properly for you, it was just quickly setup to get our backups running when we first moved into our current setup.. it works just great here though :)
Thread Status:
Not open for further replies.

Share This Page