mysql_backup_split.sh 1.1 KB

12345678910111213141516171819
  1. timenow=$(date \+\%Y\%m\%d)
  2. #common variables:
  3. retention_days="31" #after this value your dumps will be automatically pruned
  4. basedir="/mnt/backup/_MYSQLDUMP/" #leave a trailing "/" . basedirectory where you will save the backups, the folders up to mysql_basedir must be manually created
  5. #specific variables:
  6. mysql_basedir="MYSQL_PRD/" #leave a trailing "/" . directory where your backup will be saved (in $basedir)
  7. mysql_host="CHANGEME" #your mysql/mariadb db host
  8. mysql_user="bck_user" #your mysql/mariadb READONLY user
  9. mysql_pass="CHANGEME" #your mysql/mariadb READONLY user pass
  10. mysql_suff="CHANGEME_" #leave a trailing unserscore. suffix for the folder that will be created like this: /basedir/mysql_basedir/mysql_suff_timenow
  11. mysql_dir=$basedir$mysql_basedir$mysql_suff$timenow
  12. mkdir $mysql_dir
  13. cd $mysql_dir
  14. mysql -h $mysql_host -u $mysql_user -p$mysql_pass -N -e 'show databases' | while read dbname; do mysqldump -h $mysql_host -u $mysql_user -p$mysql_pass --compress "$dbname" | gzip -c > "$dbname".sql.gz; done
  15. find $basedir$mysql_basedir -type d -mtime +$retention_days -name $mysql_suff* -exec rm -rf {} \;