+ Reply to Thread
Results 1 to 2 of 2

Thread: Automatic MySQL Backup Script

  1. #1
    Administrator Advisor   peter's Avatar
    Join Date
    Apr 2004
    Posts
    878

    Automatic MySQL Backup Script

    By canela
    (Transferred from the wiki by Peter)

    I modified crouse’s mysql backup script with some modifications and have been using it for a few weeks. It saved my tush yesterday, so I thought I’d share it.
    1. It stores backup files in the directory /var/backups/mysql that it automatically creates. It also only backs up if MySQL is running by checking for the lock file.
    2. The nice thing about it is that you don’t have to specify the names of the databases. It figures it out by itself.
    3. Stick it in cron, and make sure you have enough space in /var !
    4. The script is really simple, and won’t backup databases with the string "Database" (with the capitalization) in the name
    5. You are free to modify it if you think you can get better functionality without making it too complicated. Here is the forum thread where I mentioned it first. You can post comments on it there.
    You run it like this:


    Code:
    # mysql-backup <root-username> <root-password>The code looks like this:
    Here is the script
    Code:
    #!/bin/bash 
    LOCK_FILE="/var/lock/subsys/mysqld"
    # Create the backup directory if it doesn't already exist
    if [ ! -d /var/backups/mysql ]; then
         mkdir -p /var/backups/mysql
    fi
    # Do backups
    if [ -f "$LOCK_FILE" ]; then
        # Do MySQL backups
        for i in `/usr/bin/mysql -B -u $1 -e 'show databases' -p$2 | /bin/grep -v Database`; do
            /usr/bin/mysqldump --opt -u $1 -p$2 $i | \
               gzip > /var/backups/mysql/$i.`date --iso-8601`.sql.gz
        done
    fi

    My sites: Linux Home Networking – Linux Quick Fix Notebook

  2. #2

    Useful

    I actually modified it to backup just the one database. Out of interest, the mysqldump didn’t seem to backup the stored procedures i had in my DB. Do i need to add another switch for that?

+ Reply to Thread

Similar Threads

  1. Automatic MySQL Backup Script
    By canela in forum Programming
    Replies: 2
    Last Post: 12-20-2006, 04:59 AM
Replies: 5
Last Post: 11-11-2006, 11:23 PM
  • Rsync incremental backup script
    By vwgtiturbo in forum Programming
    Replies: 6
    Last Post: 06-07-2006, 09:19 PM
  • mysql backup
    By elovkoff in forum Linux – Hardware, Networking & Security
    Replies: 28
    Last Post: 08-13-2004, 03:15 PM
  • MySQL Backup and Reanimation
    By Ashcrow in forum Linux – General Topics
    Replies: 2
    Last Post: 05-28-2002, 04:44 PM
  • Bookmarks

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts