Bash "Rotating" Mysql backup -


i'm quite novice in shellscripting far, i'm learning , getting quite comfortable in writing shellscripts, have alot learn.

the snippet @ bottom of page creates backups of users databases.

  1. the script checks first if there more 7 compressed .gz files in folder.
  2. fetches oldest gz archive , removes it.
  3. checks latest .sql backup , compresses gzip
  4. creates new .sql dump

the script run once day

i know there 1 small "error" script, "logger" doesn't work. log messages not show in syslog when run cronjob manually, don't know why.

how write differently?

#!/bin/bash  backuppath=/var/mysql_backup/ mkdir -p $backuppath if [ $(ls -l $backuppath*.gz 2>/dev/null | wc -l) -gt 7 ];         old=$(find $backuppath -type f -name '*.gz' -printf '%t+ %p\n' | sort | head -n 1 | awk '{print $2}')         rm $old # removing oldest compressed archive         logger -p local3.info -t mysql_backup "removing oldest compressed .gz" fi  if [ $(ls -l $backuppath*.sql 2>/dev/null | wc -l) -eq 1 ];         latestlog=$(find $backuppath -type f -name '*.sql' -printf '%p\n' | head -n 1)         gzip $latestlog         logger -p local3.info -t mysql_backup "compressing recent .sql file" fi  /usr/bin/mysqldump -u db_user -p'password' --single-transaction  --all-databases > $backuppath$(date "+%y.%m.%d-%h.%m.%s")_dump.sql 


Comments