sqlserver backup and restore-linux

Backup sqlserver

//创建数据库备份文件夹
mkdir -pv /usr/local/databackup
//给mussql授予/usr/local/databackup文件夹的权限
chown -R mssql:mssql /usr/local/databackup/
//登录数据库
sqlcmd -S 127.0.0.1 -U sa
//备份数据库到指定路径
1> backup database Test to disk='/usr/local/databackup/Test0411.bak'
2> go

Restore sqlserver

// 登录数据库
sqlcmd -S 127.0.0.1 -U sa
// 查出备份文件的逻辑文件名(很重要!!!不然会报错)
RESTORE FILELISTONLY FROM DISK = '/usr/local/databackup/Test0411.bak'
// 这个时候会显示两条数据,可能在Linux下数据会很乱,你找到里面的第一列LogicalName对应的值,我这里一个为standard,另一个为standard_log,standard是数据文件逻辑名,standard_log是日志文件逻辑名
 
 
// 然后进行还原
1> RESTORE DATABASE TestDB FROM DISK = '/usr/local/databackup/Test0414.bak'
2> WITH MOVE 'standard' TO '/var/opt/mssql/data/TestDB.mdf',
3> MOVE 'standard_log' TO '/var/opt/mssql/data/TestDB_Log.ldf'
4> go

Problems during the period:
1.
Msg 1834, Level 16, State 1, Server psqadb2, Line 1
cannot overwrite the file'/ var/opt/mssql/data/Test.mdf '. The file is being used by the database'Test'.
Msg 3156, Level 16, State 4, Server psqadb2, Line 1 file'standard
' cannot be restored to'/ var/opt/mssql/data/Test.mdf '. Please use the WITH MOVE option to identify the valid location of the file.
Msg 1834, Level 16, State 1, Server psqadb2, Line 1
cannot overwrite the file'/ var/opt/mssql/data/Test.ldf '. The file is being used by the database'Test'.
Msg 3156, Level 16, State 4, Server psqadb2, Line 1 file'standard_log
' cannot be restored to'/ var/opt/mssql/data/Test.ldf '. Please use the WITH MOVE option to identify the valid location of the file.
Msg 3119, Level 16, State 1, Server psqadb2, Line 1
A problem was found when planning the RESTORE statement. The previous message provides detailed information.
Msg 3013, Level 16, State 1, Server psqadb2, Line 1
RESTORE DATABASE is terminating abnormally.

Reason: This is because the WITH MOVE is not used to restore the data files and log files, resulting in the same names as the data files and log files of the original database, because the original database is still in use, resulting in an overwrite error.
Solution: Add after the restore command Just go to WITH MOVE, please refer to the above example for details

2. The logic file'Test' is not part of the database'TestDB'. Please use RESTORE FILELISTONLY to list logical file names.

Reason: This is because the logical file name is wrong, which causes move to report an error.
Solution: Use RESTORE FILELISTONLY FROM DISK='database backup file bak path' to find out the logical file name, and then modify the logical file name behind move to find out.

Guess you like

Origin blog.csdn.net/BOOM0BOOM/article/details/114658324