【linux/sqlserver/shell】 linux 版本SQL Server 数据库备份与还原

安装sqlserver数据库

   微软的数据库sqlserver 一直以来都是windows版本的,但最近微软推出了基于linux 版本的sqlserver数据库,详细的安装步骤如下:参考官网
     https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat?view=sql-server-2017

参照官网安装数据库和sqlcmd工具:我的环境为centos7.4 版本,2G内存的一个虚拟机。

 

备份

   使用sqlcmd自带的功能: 命令行sqlcmd  -?  可以查看sqlcmd 命令参数,  /opt/mssql/bin/sqlservr --help 查看sqlserver参数如下所示

[root@localhost ~]# sqlcmd -?
Microsoft (R) SQL Server Command Line Tool
Version 17.2.0000.1 Linux
Copyright (c) 2012 Microsoft. All rights reserved.

usage: sqlcmd            [-U login id]          [-P password]
  [-S server or Dsn if -D is provided] 
  [-H hostname]          [-E trusted connection]
  [-N Encrypt Connection][-C Trust Server Certificate]
  [-d use database name] [-l login timeout]     [-t query timeout]
  [-h headers]           [-s colseparator]      [-w screen width]
  [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
  [-c cmdend]
  [-q "cmdline query"]   [-Q "cmdline query" and exit]
  [-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]
  [-u unicode output]    [-r[0|1] msgs to stderr]
  [-i inputfile]         [-o outputfile]
  [-k[1|2] remove[replace] control characters]
  [-y variable length type display width]
  [-Y fixed length type display width]
  [-p[1] print statistics[colon format]]
  [-R use client regional setting]
  [-K application intent]
  [-M multisubnet failover]
  [-b On error batch abort]
  [-D Dsn flag, indicate -S is Dsn] 
  [-X[1] disable commands, startup script, environment variables [and exit]]
  [-x disable variable substitution]
  [-? show syntax summary]
[root@localhost ~]# /opt/mssql/bin/sqlservr --help
usage: sqlservr [OPTIONS...]

Configuration options:
  -T<#>                     Enable a traceflag
  -y<#>                     Enable dump when server encounters specified error
  -k<#>                     Checkpoint speed (in MB/sec)

Administrative options:
  --accept-eula             Accept the SQL Server EULA
  --pid                Set server product key
  --reset-sa-password       Reset system administrator password. Password should
                            be specified in the SQLSERVR_SA_PASSWORD environment
                            variable.
  -f                        Minimal configuration mode
  -m                        Single user administration mode
  -K                        Force regeneration of Service Master Key
  --setup                   Set basic configuration settings and then shutdown.
  --force-setup             Same as --setup, but also reinitialize master and model databases.

General options:
  -v                        Show program version
  --help                    Display this help information
[root@localhost ~]# 

备份命令:

sqlcmd   执行sql语句格式

sqlcmd -U SA  -P password -Q“SQL语句”  执行完退出

备份语句  例如备份 数据库名为test

sqlcmd -U SA  -P password -Q“BACKUP DATABASE test TO  DISK='/tmp/test.bak' with format ,init”

下面有个脚本 backup.sh   

 1 备份 全库  入参 all  (tempDB除外);

 2 备份单库 入参数据库名;

3  备份后统一整理到/tmp/sqlserver 目录下,然后打包 打入管道发送到存储端。

脚本如下:

#!/bin/bash
echo $1
echo $2
ALL="all"
password="Aa123456"
user="SA"

mkdir /tmp/sqlserver
if [ "$1" == "$ALL" ]; then
   sqlcmd -U  SA  -P $password  -Q "Select 'backup ' + name   from sysdatabases" >/tmp/sql.txt
   cat /tmp/sql.txt | grep backup | awk '{print $2}'|  while read database ; do
      echo $database;
      if [ "$database" != "tempdb" ];then
         sqlcmd -U  $user  -P $password  -Q "BACKUP DATABASE $database TO DISK='/tmp/$database.bak' with format, init"
         mv  /tmp/$database.bak /tmp/sqlserver
      else
         echo "skip  tempdb"
      fi
   done
fi

if [ "$1" != "$ALL" ] && [ "$1" != "" ]; then
   sqlcmd -U  $user  -P $password  -Q "BACKUP DATABASE $1 TO DISK='/tmp/$1.bak' with format, init"
   mv /tmp/$1.bak /tmp/sqlserver
   echo "not all"
fi

if [ "$1" == "" ]; then
   echo "null"
fi

tar -cf /tmp/sqlserver.bak /tmp/sqlserver
cat /tmp/sqlserver.bak >/opt/pipe

rm -rf /tmp/sql.txt
rm -rf /tmp/sqlserver
rm -rf /tmp/sqlserver.bak

还原

使用sqlcmd 命令还原  例如还原数据库test

sqlcmd -U SA -P password  -Q "RESTORE DATABASE  test FROM  DISK='/tmp/sqlserver/test.bak' WITH REPLACE"

但 master 数据库特殊,需要在单用户模式下备份 ,方法停止sqlserver数据库,已单用户模式启动数据库

/opt/mssql/bin/sqlservr  -m"SQLCMD"   

然后在执行还原命令即可;

参考还原脚本:

1 没有入参,根据备份时/tmp/sqlserver 目录下备份的数据库 来还原;

2 备份时的tar 文件,放到/ 目录 ,解压 还原后 /tmp/sqlserver 目录下会存在备份的数据库,逐个还原。

restore.sh  如下所示


#!/bin/bash
password="Aa123456"
user="SA"
cd /
tar -vxf /F*.bkp

ls /tmp/sqlserver | awk '{print}' | awk -F'.' '{print $1}' |  while read dbname; do
   echo "数据库"
   echo $dbname
   if [ "$dbname" != "master" ]; then
      sqlcmd -U $user -P $password -Q"RESTORE DATABASE $dbname FROM DISK='/tmp/sqlserver/$dbname.bak' WITH REPLACE"
   fi
done

if [  -f "/tmp/sqlserver/master.bak" ]; then
   echo "存在master 库-----------------------"
   #设置单用户模式
   systemctl stop mssql-server
   pid=`netstat -anp | grep 1433 | head -1 | awk '{print $7}' | awk -F'/' '{print $1}'`
   echo "进程号"
   echo $pid
   kill -9 $pid
   #killall sqlservr
   /opt/mssql/bin/sqlservr -m"SQLCMD" &
   sleep 5
   sqlcmd -U $user -P $password -Q"RESTORE DATABASE master FROM DISK='/tmp/sqlserver/master.bak' WITH REPLACE"
   sleep 5
   /opt/mssql/bin/sqlservr  &
fi

rm -rf *.bkp
rm -rf /tmp/sqlserver

注意 

1 还原时,建议不参考官网的启动方式启动数据库,先systemclt  stop  mssql-server 停止sqlserver服务,以/opt/mssql/sqlservr  &  的方式启动;

2 不能备份还原tempDB  这个库不支持sqlcmd备份,这个库也不影响其他库的备份和还原;

3 还原时 ,默认 sqlservr 进程开启,否则会失败;

 

哪里写的有误,欢迎指正,谢谢!

 

版权所有,转载请注明出处,https://blog.csdn.net/weixin_40747106/article/details/81451628

 

你可能感兴趣的:(Linux)