其实这个脚本是在网上看到的,它是一个简单的mysql备份脚本用于线上备份,正好在这里引用到了shell数组。
只是在这里提醒一下shell中的数组使用技巧,现在拿来和大家分享:
#!/bin/bash
suffix=$(date +%m-%d-%Y)
cpath=/data/mysql/backup
dblist=(netseek mysql test)
sockpath="/data/mysql/3306/mysql.sock"
for ((i=0; i<${#dblist[@]};i++))
do
if [ -d $cpath ]
then
# direcotry exists, we're good to continue
filler="just some action to prevent syntax error"
else
#we need to make the directory
echo Creating $cpath
mkdir -p $cpath
fi
#now backup db
sqlfile=${cpath}/${dblist[$i]}_$suffix.sql.gz
mysqldump -S ${sockpath} --single-transaction --flush-logs --master-data=2 ${dblist[$i]}|gzip -c > $sqlfile
if [ $? -eq 0 ]
then
printf "%s was backed up successfully to %s\n\n" ${dblist[$i]} $sqlfile
else
printf "WARNING: An error occured while attempting to backup %s to %s\n\n" ${dblist[$i]} $sqlfile
fi
done