timenow=$(date \+\%Y\%m\%d) #common variables: retention_days="31" #after this value your dumps will be automatically pruned basedir="/mnt/backup/_MYSQLDUMP/" #leave a trailing "/" . basedirectory where you will save the backups, the folders up to mysql_basedir must be manually created #specific variables: mysql_basedir="MYSQL_PRD/" #leave a trailing "/" . directory where your backup will be saved (in $basedir) mysql_host="CHANGEME" #your mysql/mariadb db host mysql_user="bck_user" #your mysql/mariadb READONLY user mysql_pass="CHANGEME" #your mysql/mariadb READONLY user pass mysql_suff="CHANGEME_" #leave a trailing unserscore. suffix for the folder that will be created like this: /basedir/mysql_basedir/mysql_suff_timenow mysql_dir=$basedir$mysql_basedir$mysql_suff$timenow mkdir $mysql_dir cd $mysql_dir 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 find $basedir$mysql_basedir -type d -mtime +$retention_days -name $mysql_suff* -exec rm -rf {} \;