User Tools

Site Tools


Sidebar


Tags Cloud
mysql_backup_simple

This is an old revision of the document!


Простейший скрипт бекапа MySQL

#!/usr/bin/env bash
#
# Daily backup mysql databases
# CLI args: none

set -e # exit on error

LANG=C
PATH="/bin:/usr/sbin:/usr/bin:/sbin:/usr/local/bin:/usr/local/sbin"

DATE=$(date +%F)
BACKUP_DIR=/home/mysql_backups
DUMP_DIR=$BACKUP_DIR/dumps
LOG=$BACKUP_DIR/backup.log

DAYS=7
DB="production development"
OPTS="--order-by-primary --add-drop-database"

#########################

print_error() { echo "$@"; write_log "[ERROR] $@"; exit 1; }
print_info()  { echo "$@"; write_log "[INFO] $@"; }
write_log()   { echo "`date -R -u` $@" >> $LOG; }

##########################

# check dir
[[ -d $DUMP_DIR ]] || mkdir -p $DUMP_DIR

# check permissions on backup dir
if PERMS=$(stat --format="%a" $BACKUP_DIR)
then
  # restore chmod
  [[ $PERMS -eq 700 ]] || chmod 700 $BACKUP_DIR
fi

# check binary
[[ -f `which mysqldump 2>/dev/null` ]] && MYSQLDUMP=mysqldump || print_error "No mysqldump binary found in $PATH"

# create dumps
for db in $DB
do
  DUMP_FILE=$DUMP_DIR/$DATE/$db.sql.gz
  # is dump exist?
  [[ -s $DUMP_FILE ]] || (
    # log
    print_info "Creating new: $DUMP_FILE"

    # mkdir
    mkdir -p $DUMP_DIR/$DATE

    # dump
    $MYSQLDUMP $OPTS $db | gzip -1 > $DUMP_FILE
  )
done

# remove dirs
if OUTDATED_DIRS=$(find $DUMP_DIR -maxdepth 1 -type d -mtime +$DAYS)
then
  for dir in $OUTDATED_DIRS
  do
    # log
    print_info "Deleting outdated: $dir"

    # rm
    [[ -d $dir ]] && rm -r "$dir" || print_error "Can't remove $dir: dir not exists"
  done
fi
mysql_backup_simple.1409616572.txt.gz · Last modified: 2014/09/02 00:09 by kyxap