1、mysql数据库备份脚本
#!/bin/bash
username="root"
password="GDnxFd@123#!"
backupToBasePath=/data1/mysqldump_databases_backup/
if [ ! -d ${backupToBasePath} ];then (mkdir ${backupToBasePath}) fi
retentionDays=30
databases=(information_schema
am
cm
hue
metastore
mysql
nav_as
nav_ms
oozie
performance_schema
rm
sentry
sys
t_demo
t_resource
t_tempo)
datetime=`date +%Y_%m_%d`
for db in ${databases[@]};do
if [ ! -d ${backupToBasePath}/${db} ];then (mkdir ${backupToBasePath}/${db}) fi
echo "mysqldump -u${username} -p${password} ${db} -d > ${backupToBasePath}/${db}/${datetime}_structure_${db}.sql"
(mysqldump -u${username} -p${password} ${db} -d > ${backupToBasePath}/${db}/${datetime}_structure_${db}.sql)
echo "mysqldump -u${username} -p${password} ${db} -t > ${backupToBasePath}/${db}/${datetime}_data_${db}.sql"
(mysqldump -u${username} -p${password} ${db} -t > ${backupToBasePath}/${db}/${datetime}_data_${db}.sql)
done
echo "find ${backupToBasePath} -mtime +${retentionDays} -name \"*.sql\" -exec rm -rf {} \;"
(find ${backupToBasePath} -mtime +${retentionDays} -name "*.sql" -exec rm -rf {} \;)
2、cron表达式
2.1 每天23点执行一次
crontab -l
crontab -e
0 23 * * * sh mysqldump_databases_backup.sh &
:wq