FreeBSD中MySQL数据库备份到NAS服务器的shell脚本

发布时间:2019-09-29编辑:脚本学堂
本文介绍下,在freebsd系统中,将所有mysql数据库备份到nas服务器中的shell脚本,有需要的朋友参考下吧。

将所有mysql数据库备份到nas服务器,代码:
 

复制代码 代码示例:
#!/bin/sh
# 此shell脚本,备份所有的MySQL数据库到NAS服务器
# 请务必安装有 mount_smbfs
# 需要设置用户名与密码等信息
# Tested on FreeBSD 6.x and 7.x - 32 bit and 64 bit systems.
# May work on OpenBSD / NetBSD.
# -----------------------------------
### SETUP BIN PATHS ###
MYSQLADMIN=/usr/local/bin/mysqladmin
MYSQL=/usr/local/bin/mysql
LOGGER=/usr/bin/logger
MYSQLDUMP=/usr/local/bin/mysqldump
MKDIR=/bin/mkdir
CP=/bin/cp
GZIP=/usr/bin/gzip
CUT=/usr/bin/cut
AWK=/usr/bin/awk
MOUNT=/sbin/mount
GREP=/usr/bin/grep
UMOUNT=/sbin/umount
MSMBFS=/usr/sbin/mount_smbfs
HOST=/usr/bin/host
TAIL=/usr/bin/tail
SSH=/usr/bin/ssh
SCP=/usr/bin/scp
HOSTNAME=/bin/hostname
 
### SETUP MYSQL LOGIN ###
MUSER=root
MPASS='PASSWORD'
MHOST="127.0.0.1"
 
### SETUP NAS LOGIN ###
NASUSER=vivek
NASPASSWORD=myPassword
NASSERVER=nas05.vip.nixcraft.com
NASMNT=/nas05
NASSHARE=$NASUSER
NASPASSWDFILE=$HOME/.nsmbrc
#GET NAS IP
NASIP=$($HOST $NASSERVER  | $TAIL -1 | $AWK '{ print $4}')
# NAS BACKUP PATH
MBAKPATH=${NASMNT}/$(hostname -s)/mysql
NOW=$(date +"%d-%m-%Y")
TIME_FORMAT='%H_%M_%S%P'
 
mount_nas(){
 [ ! -d $NASMNT ] && $MKDIR -p $NASMNT
 $MOUNT | $GREP $NASMNT >/dev/null
 if [ $? -ne 0 ]
 then
  echo "[$NASIP:$NASUSER]"  >$NASPASSWDFILE
  echo "password=$NASPASSWORD" >>$NASPASSWDFILE
  $MSMBFS -N -I $NASSERVER //$NASUSER@$NASIP/$NASSHARE $NASMNT
 fi
}
 
umount_nas(){
 $MOUNT | $GREP $NASMNT >/dev/null
 [ $? -eq 0 ] && $UMOUNT $NASMNT
}
 
backup_mysql(){
 $LOGGER "$(basename $0) mysql: Started at $(date)"
 local DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
 local db="";
 [ ! -d $MBAKPATH/$NOW ] && $MKDIR -p $MBAKPATH/$NOW
 for db in $DBS
 do
  local tTime=$(date +"${TIME_FORMAT}")
  local FILE="${MBAKPATH}/$NOW/${db}.${tTime}.gz"
  $MYSQLDUMP -u $MUSER -h $MHOST -p"$MPASS" $db | $GZIP -9 > $FILE
  #mysql_file_hook $FILE
 done
 $LOGGER "$(basename $0) mysql: Ended at $(date)"
}
 
# process each sql database file and backup to another server via ssh
# must have ssh keys
mysql_file_hook(){
 local f="$1"
 local d=/nas/mysqlbackup/$(hostname -s)/$NOW
 $SSH someuser@www.jb200.com mkdir -p $d
 $SCP $f someuser@www.jb200.com:$d
}
 
case "$1" in
        mysql)
  mount_nas
                backup_mysql
  umount_nas
                ;;
        mount)
  mount_nas;;
        umount)
  umount_nas;;
        *)
                echo "Usage: $0 {mysql|mount|umount}"
esac