Created
November 23, 2021 11:40
-
-
Save khavari/1086550bebee25229d934e3df077e7b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### Destination folder where backups are stored. | |
destination_root_path=/var/www/mysql_dump | |
date_dir=$(date +"%Y-%m-%d") | |
destination_dir=$destination_root_path/$date_dir | |
current_date=$(date +"%F") | |
### MySQL Configuration | |
db_host="localhost" | |
db_user="root" | |
db_pass="123" | |
### Create directory with parent recursive | |
mkdir -p $destination_dir | |
DATABASES=$(mysql -h $db_host -u $db_user -p$db_pass -e "SHOW DATABASES;" | tr -d "| " | grep -v "Database" | grep -v "sys$" | grep -v "test$") | |
[ ! -d $destination_dir ] && mkdir -p $destination_dir | |
for database_name in $DATABASES; do | |
gz_file_name="${destination_dir}/$database_name.sql.gz" | |
file_date= | |
# Be sure to make one backup per day | |
[ -f $gz_file_name ] && file_date=$(date -r $gz_file_name +"%F") | |
[ "$file_date" == "$current_date" ] && continue | |
[ -f $gz_file_name ] && mv "$gz_file_name" "${gz_file_name}.old" | |
mysqldump --single-transaction --routines --quick -h $db_host -u $db_user -p$db_pass -B $database_name | gzip > "$gz_file_name" | |
sudo chown khavari:khavari "$gz_file_name" | |
rm -f "${gz_file_name}.old" | |
done | |
#tar czfv $destination_root_path/farayad.dump.$date_dir.tar.gz $destination_dir | |
# Delete current date directory | |
#rm -rf "${destination_dir}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment