mysql数据库备份脚本:
#!/bin/bash #定义变量信息 USER=root PASS=redhat12345 SOCKET=/data/3306/mysql.sock CMD="mysql -u$USER -p$PASS -S $SOCKET" DUMP="mysqldump --opt -u$USER -p$PASS -S $SOCKET" DES="/usr/src/mysqlbackup" BACKUPTIME="date +%F-%H:%M" #目录判断 if [ ! -d "$DES/`date +%F`" ];then mkdir -p "$DES/`date +%F`" fi #mysqldump备份数据库 echo "----------------------------start backup mysql-------------------------------" for database in `$CMD -e "show databases" | sed ‘1,2d‘ | egrep -v "mysql|schema"` do $DUMP $database -B|gzip>$DES/`date +%F`/${database}.sql.gz if [ $? -eq 0 ];then echo "$database has been backup successful" else echo "$database has been not successful" fi /bin/sleep 5 done echo "---------------------------backup is end -------------------------------------"
执行结果如下:
[[email protected] mysqlbackup]# pwd /usr/src/mysqlbackup [[email protected] mysqlbackup]# ll 总用量 4 -rw-r--r-- 1 root root 805 12月 6 18:50 backup.sh [[email protected] mysqlbackup]# bash backup.sh ----------------------------start backup mysql------------------------------- dawnpro has been backup successful hangzhou has been backup successful james has been backup successful wanlong has been backup successful ---------------------------backup is end ------------------------------------- [[email protected] mysqlbackup]# ll 总用量 8 drwxr-xr-x 2 root root 4096 12月 6 18:56 2016-12-06 -rw-r--r-- 1 root root 805 12月 6 18:50 backup.sh [[email protected] mysqlbackup]# ll 2016-12-06/ 总用量 16 -rw-r--r-- 1 root root 512 12月 6 18:56 dawnpro.sql.gz -rw-r--r-- 1 root root 513 12月 6 18:56 hangzhou.sql.gz -rw-r--r-- 1 root root 511 12月 6 18:56 james.sql.gz -rw-r--r-- 1 root root 511 12月 6 18:56 wanlong.sql.gz mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | dawnpro | | hangzhou | | james | | mysql | | performance_schema | | wanlong | +--------------------+ 7 rows in set (0.00 sec)
做个简单的记录,后面有空再补充详细的注解或者对脚本进行优化
时间: 2024-12-08 14:45:50