在备份前,需确保系统已安装mssql-tools(包含sqlcmd命令行工具)和unixODBC-devel,用于执行T-SQL命令和数据库连接。同时,创建专用备份目录并设置正确权限,避免权限问题导致备份失败:
sudo yum install -y mssql-tools unixODBC-devel # 安装工具包
sudo mkdir -p /var/opt/mssql/backups # 创建备份目录
sudo chown mssql:mssql /var/opt/mssql/backups # 设置目录所有者为mssql用户
sudo chmod 700 /var/opt/mssql/backups # 限制目录访问权限
通过sqlcmd工具登录SQL Server,执行BACKUP DATABASE命令实现完整备份(推荐格式化备份文件并添加描述):
BACKUP DATABASE [YourDatabaseName]
TO DISK = '/var/opt/mssql/backups/YourDatabaseName_Full_$(date +\%F).bak'
WITH FORMAT, MEDIANAME = 'YourDatabaseNameBackup', NAME = 'Full Backup of YourDatabaseName', STATS = 10;
FORMAT:格式化备份设备,覆盖旧备份(首次备份需使用);MEDIANAME:备份介质名称,便于识别;STATS = 10:每完成10%显示进度;$(date +\%F):生成当前日期(如2025-09-24),实现每日备份文件自动命名。若习惯图形操作,可通过SSMS完成:
/var/opt/mssql/backups/...);通过crontab设置定时任务,实现每日自动备份(如凌晨2点执行):
sudo crontab -e
添加以下内容(替换YourDatabaseName、密码和路径):
0 2 * * * /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'your_password' -Q "BACKUP DATABASE [YourDatabaseName] TO DISK = '/var/opt/mssql/backups/YourDatabaseName_Full_$(date +\%F).bak' WITH FORMAT, STATS = 10;"
0 2 * * *:表示每日凌晨2点执行;$(date +\%F):需转义%符号(\%F),生成日期格式文件名。RESTORE HEADERONLY命令检查备份文件是否有效:RESTORE HEADERONLY FROM DISK = '/var/opt/mssql/backups/YourDatabaseName_Full_2025-09-24.bak';
确认“BackupType”(1=完整备份)、“Status”(1=有效)等信息。crontab定期删除超过30天的备份文件,避免磁盘空间耗尽:0 3 * * * find /var/opt/mssql/backups/ -name "*.bak" -mtime +30 -exec rm {} \;
还原前需停止SQL Server服务(避免文件占用),并将备份文件复制到SQL Server数据目录(如/var/opt/mssql/data/):
sudo systemctl stop mssql-server # 停止服务
sudo cp /var/opt/mssql/backups/YourDatabaseName_Full.bak /var/opt/mssql/data/ # 复制备份文件
sudo systemctl start mssql-server # 启动服务(可选,若需立即还原可保持停止)
通过sqlcmd工具执行RESTORE DATABASE命令,根据备份类型选择参数:
完整备份还原(覆盖现有数据库):
RESTORE DATABASE [YourDatabaseName]
FROM DISK = '/var/opt/mssql/data/YourDatabaseName_Full.bak'
WITH REPLACE, RECOVERY, STATS = 10;
REPLACE:覆盖现有数据库(若数据库已存在);RECOVERY:将数据库恢复到可用状态(适用于完整备份或最后一步还原)。差异备份还原(需先还原完整备份,再还原差异备份):
-- 还原完整备份(NO_RECOVERY:不恢复数据库,等待差异备份)
RESTORE DATABASE [YourDatabaseName]
FROM DISK = '/var/opt/mssql/data/YourDatabaseName_Full.bak'
WITH NORECOVERY, STATS = 10;
-- 还原差异备份(RECOVERY:恢复数据库)
RESTORE DATABASE [YourDatabaseName]
FROM DISK = '/var/opt/mssql/backups/YourDatabaseName_Diff.bak'
WITH RECOVERY, STATS = 10;
事务日志还原(需依次还原完整备份、差异备份,最后还原事务日志):
-- 还原完整备份(NO_RECOVERY)
RESTORE DATABASE [YourDatabaseName]
FROM DISK = '/var/opt/mssql/data/YourDatabaseName_Full.bak'
WITH NORECOVERY, STATS = 10;
-- 还原差异备份(NO_RECOVERY)
RESTORE DATABASE [YourDatabaseName]
FROM DISK = '/var/opt/mssql/backups/YourDatabaseName_Diff.bak'
WITH NORECOVERY, STATS = 10;
-- 还原事务日志(最后一次还原用RECOVERY)
RESTORE LOG [YourDatabaseName]
FROM DISK = '/var/opt/mssql/backups/YourDatabaseName_Log.trn'
WITH RECOVERY, STATS = 10;
REPLACE参数),根据备份类型选择“恢复状态”(如完整备份选“恢复”,差异/日志备份选“不恢复”);还原完成后,需验证数据库状态和数据完整性:
SELECT name, state_desc FROM sys.databases WHERE name = 'YourDatabaseName';
(state_desc应为ONLINE,表示数据库可用)。DBCC CHECKDB命令检查数据库一致性:DBCC CHECKDB('YourDatabaseName') WITH NO_INFOMSGS, ALL_ERRORMSGS;
若无错误输出,说明数据库结构完整。