Linux automatically backs up MySQL database script code

  • 2020-06-01 11:12:06
  • OfStack

In the script, you can set the list of database tables to be backed up, and the backup files will be compressed through gzip. It is important to note that this script is only suitable for data 1 environments with low requirements.

#!/bin/bash 
mysql_pwd="password"
mysql_dump="/usr/local/mysql/bin/mysqldump"
cur_year=$(date +"%Y") 
cur_month=$(date +"%m") 
cur_day=$(date +"%d") 
dump_path="/usr/backup/mysql/$cur_year-$cur_month/$cur_day"
arr_tables=( 
"table_1"
"table_2"
"table_3"
) 
if [ ! -d "$dump_path" ]; then
mkdir -p "$dump_path"
fi
for cur_table in ${arr_tables[*]}; do
$mysql_dump -uroot -p$mysql_pwd --opt mydb $cur_table | gzip > $dump_path/$cur_table.sql.gz 
done

Related articles: