User Tools

Site Tools


mysql_backup_simple

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Last revision Both sides next revision
mysql_backup_simple [2014/09/01 20:40]
kyxap created
mysql_backup_simple [2015/04/04 10:35]
kyxap
Line 6: Line 6:
 # CLI args: none # CLI args: none
  
-set -e # exit on error+#exit on error 
 +set -e
  
 LANG=C LANG=C
Line 17: Line 18:
  
 DAYS=7 DAYS=7
-DB="production development"+DB="all"
 OPTS="​--order-by-primary --add-drop-database"​ OPTS="​--order-by-primary --add-drop-database"​
  
 #########################​ #########################​
  
-print_error() +print_error() { echo "​$@"​write_log "​[ERROR] $@"exit 1
-{ +print_info() ​ { echo "​$@"​write_log "​[INFO] $@"
-  ​echo "​$@"​ +write_log() ​  ​{ echo "`date -R -u` $@" >> $LOG}
-  ​write_log "​[ERROR] $@" +
-  ​exit 1 +
-} +
- +
-print_info() +
-{ +
-  ​echo "​$@"​ +
-  ​write_log "​[INFO] $@" +
-} +
- +
-write_log() +
-{ +
-  ​echo "`date -R -u` $@" >> $LOG +
-}+
  
 ##########################​ ##########################​
Line 52: Line 39:
 fi fi
  
-# check binary+# check binaries
 [[ -f `which mysqldump 2>/​dev/​null` ]] && MYSQLDUMP=mysqldump || print_error "No mysqldump binary found in $PATH" [[ -f `which mysqldump 2>/​dev/​null` ]] && MYSQLDUMP=mysqldump || print_error "No mysqldump binary found in $PATH"
 +[[ -f `which mysql 2>/​dev/​null` ]] && MYSQL=mysql || print_error "No mysql binary found in $PATH"
 +
 +# validate $DB and skip default dbs
 +[[ $DB == all ]] && DB=$(mysql -e 'show databases'​ | tail -n +2 | grep -v -E "​^(information_schema|performance_schema)$"​)
  
 # create dumps # create dumps
 for db in $DB for db in $DB
 do do
 +  # is db exist?
 +  if ! EXISTS=$($MYSQL -s -N -e "show databases"​ | tail -n +2 | grep -x $db)
 +  then
 +    print_info "No such database: $db"
 +    break
 +  fi
 +
   DUMP_FILE=$DUMP_DIR/​$DATE/​$db.sql.gz   DUMP_FILE=$DUMP_DIR/​$DATE/​$db.sql.gz
   # is dump exist?   # is dump exist?
Line 66: Line 64:
     # mkdir     # mkdir
     mkdir -p $DUMP_DIR/​$DATE     mkdir -p $DUMP_DIR/​$DATE
 +
 +    # add --events to mysql db
 +    [[ $db == mysql ]] && OPTS+="​ --events"​
  
     # dump     # dump
Line 84: Line 85:
   done   done
 fi fi
 +
 </​code>​ </​code>​
mysql_backup_simple.txt · Last modified: 2015/04/04 10:39 by kyxap