mysql daily backup 7 days


mkdir -vp /backups/databases
vi /some/path/make_db_back.sh

#!/bin/bash

week_day=`date +%u`
back_dir=/backups/databases/$week_day

if [ -d "$back_dir" ]; then
rm -rvf $back_dir
fi

mkdir $back_dir
mysql -s -e 'show databases' | egrep -v "mysql|information_schema|performance_schema" > /root/db_list
cat /root/db_list | while read db; do mysqldump -h localhost --single-transaction --events $db > $back_dir/$db.$(date +'%F').sql; sleep 5 ; done
gzip $back_dir/*.sql
rm -f /root/db_list

Run every day cron:

crontab -e
0 10 * * * /some/path/make_db_back.sh

Leave a Reply

Your email address will not be published. Required fields are marked *