mysql 数据库表备份和还原

时间:2022-07-11 08:22:33
#!/bin/bash
#ip地址
HOST=localhost
#用户名
USER=claim
#密码
PASSWORD=Pw123456!
#数据库名称
DATABASE=claim_oauth2
#备份路径
BACKUP_PATH=/opt/devbacksql/devoauth2/
#备份的表
table="oauth_client_details sys_auth sys_role sys_users sys_user_role sys_role_auth third_party"
#日志路径
logfile=/opt/devsql/oauth2.log

#当前日期
DATE=`date '+%Y%m%d'`
echo $DATE

#连接mysql
#mysql -h $HOST -u $USER -p$PASSWORD -s claim_oauth2<<EOF
#进入到备份目录
#cd $BACKUP_PATH
#遍历数据中的表
#for table in $DATABASE
#do
 # echo $table
  #获取表名
  #table=$(mysql -h $HOST -u $USER -p$PASSWORD -s $DATABASE -e "show tables;"|sed '1d')
  for tb in $table
  do
    #备份文件名
    DUMPNAME=""$tb"_"$DATE".sql"
    mysqldump -h $HOST -u $USER -p$PASSWORD $DATABASE $tb> $BACKUP_PATH$DUMPNAME
    #if [ $? = 0 ]
    #then
    #  echo "$DUMPNAME backup Successful!">>$logfile
    #else
    #  echo "$DUMPNAME backup fail!" >>$logfile
    #fi
  done
#done
#if [ $? = 0 ]
#  then
#    echo "backup all tables Successful!">>$logfile
#  else
#    echo "backup all tables fail!" >>$logfile
#fi
exit 1

 返回数据

#!/bin/bash
#ip地址
HOST=localhost
#用户名
USER=claim_prod
#密码
PASSWORD=Password123!
#数据库名称
DATABASE=claim_oauth2_prod
#备份路径
BACKUP_PATH=/opt/devbacksql/devoauth2/
for j in `ls $BACKUP_PATH`
do
  echo $j
  mysql -u$USER -p$PASSWORD $DATABASE < $BACKUP_PATH$j
done

[root@insure devsql]# mysql -u claim -pPw123456!
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

解决办法:在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程