Skip to content

Instantly share code, notes, and snippets.

@netkiller
Created September 3, 2013 06:39
Show Gist options
  • Save netkiller/6420375 to your computer and use it in GitHub Desktop.
Save netkiller/6420375 to your computer and use it in GitHub Desktop.

Revisions

  1. netkiller created this gist Sep 3, 2013.
    30 changes: 30 additions & 0 deletions backup.db.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/bin/bash
    ###################################
    # $Id: backup 381 2012-04-03 09:49:08Z netkiller $
    # Author: [email protected]
    # Home: http://netkiller.github.com
    ###################################
    BACKUP_HOST="localhost"
    BACKUP_USER="root"
    BACKUP_PASS=""
    BACKUP_DIR=/opt/backup
    BACKUP_DBNAME="test neo"
    #Number of copies
    COPIES=7
    ####################################
    MYSQLDUMP="mysqldump"
    #TIMEPOINT=$(date -u +%Y-%m-%d)
    TIMEPOINT=$(date -u +%Y-%m-%d.%H:%M:%S)
    MYSQLDUMP_OPTS="-h $BACKUP_HOST -u$BACKUP_USER -p$BACKUP_PASS"
    ####################################
    umask 0077
    test ! -d "$BACKUP_DIR" && mkdir -p "$BACKUP_DIR"
    test ! -w $BACKUP_DIR && echo "Error: $BACKUP_DIR is un-writeable." && exit 0

    for dbname in $BACKUP_DBNAME
    do
    test ! -d "$BACKUP_DIR/$dbname" && mkdir -p "$BACKUP_DIR/$dbname"

    $MYSQLDUMP $MYSQLDUMP_OPTS $dbname | gzip > $BACKUP_DIR/$dbname/$dbname.$TIMEPOINT.sql.gz
    done
    find $BACKUP_DIR -type f -mtime +$COPIES -delete