how to restore Sql server backup using batch file - sql-server

Below is my batch file which is taking backup but showing an exception "unexcepted argument" on execution of restore command. Pl help me solving this issue, TIA.
One more doubt can I take backup of remote server on my local folder, here I am taking backup on remote machine itself.
set SqlServer=.
set InstanceName=MSSQLSERVER
set Username=xxx
set Password=xxxx
set Database=abc
set LocalFolder=C:\db_backups
SqlCmd -S %SqlServer% -U %Username% -P %Password% -Q "Backup Database %Database% To Disk='%LocalFolder%\%Database%-%CurrentDate%.bak'"
SqlCmd -S %SqlServer% -U %Username% -P %Password% –Q "RESTORE DATABASE [test] FROM DISK='%LocalFolder%\%Database%-%CurrentDate%.bak'"

Related

Restore SQL Server database within docker container error

I tried to restore an SQL Server database within a docker container (SQL Server 2017), but it always shows the error
The operating system returned the error '1(Incorrect function.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on '/var/opt/mssql/data/release_129x.mdf'.
File 'EIS_411' cannot be restored to '/var/opt/mssql/data/release_129x.mdf'. Use WITH MOVE to identify a valid location for the file.
The operating system returned the error '1(Incorrect function.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on '/var/opt/mssql/data/release_129x.ldf'.
File 'EIS_411_log' cannot be restored to '/var/opt/mssql/data/release_129x.ldf'. Use WITH MOVE to identify a valid location for the file.
This is the command which I run:
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=365653wz" -p 1401:1433 -v D:\docker\mssql:/var/opt/mssql --name mssql -d mcr.microsoft.com/mssql/server:2017-latest
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '365653wz' -Q 'RESTORE FILELISTONLY FROM DISK = "/var/opt/mssql/EIS_1293.bak"' | tr -s ' ' | cut -d ' ' -f 1-2
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "365653wz" -Q 'RESTORE DATABASE EIS_1293 FROM DISK = "/var/opt/mssql/EIS_1293.bak" WITH MOVE "EIS_411" TO "/var/opt/mssql/data/release_129x.mdf" , MOVE "EIS_411_log" TO "/var/opt/mssql/data/release_129x.ldf"'
I have tried adding the full permission to the folder, but it still does not work.

sqlcmd using windows authentication in automation script

I am using below command in bat file to get some information from the list of sql instance,i am using -E to login as windows authentication when i manually triggering the bat file.
Could anyone help how to configure it on SQLJOB.i am sure that - E will not work on SQLjob ..
Thanks in advance!!
FOR /F %%G IN (E:\serverlist\serverlist.txt) DO (echo %%G
sqlcmd -S %%G -iE:\serverlist\some.sql -h-1 -W -w1000 -s "," -m1 -E >>E:\output\result.txt
)
using proxy account helped to resolve the issue.just add the sqlcmd command under a job step with Run as 'Proxy account'
try this.
sqlcmd -S .\sqlexpress -E -i "c:\test.sql" -o "c:\output.txt"
(This command would execute a SQL script on a local SQLExpress server using Windows Authentication)
sqlcmd -S mysql -U sqluser -P sqlpassword -i "c:\test.sql" -o "c:\output.txt"
(This command would execute a SQL script on a SQL server using SQL authentication)

Restore plain text postgresql using batch file

I want to restore a plain postgres backup, im doing it via psql.exe using this commands:
cd "C:\Program Files (x86)\PostgreSQL\9.0\bin\" <br>
psql -h 192.168.10.160 -p 5432 db_name postgres
then cmd displays db_name=# ,and I type, \i 'C:/Backup.sql' and the restore is done
Is there a way to do it using a batch file?
Yes, use the -f option to read from a file:
psql -h 192.168.10.160 -p 5432 -f C:\Backup.sql db_name postgres
When I do this I set the pgdump to include the database name in the sql before all the create tables
\connect mydatabasename

Running bulk of SQL Scripts in Sybase through batch

I was using below code(as .bat file) to recursively execute the bulk of .sql files, having SQL SERVER 2008 R2 as backend:
for /R %%G in (*.sql) do sqlcmd /S [Database Server] /d [Database name] -U [Username] - P[Password] -i"%%G"
pause
Now, i i have to execute bulk of sql scripts but this time Sybase as backend.
Please suggest me what modification should i do to make it run for 'Sybase'!!
The connection string for Sybase is very similar
isql -U [username] -P [password] -S [servername] -D [dbname] -i [scriptname]
So your script will look something like:
for /R %%G in (*.sql) do isql -S ServerName -D DbName -U Username -P Password -i"%%G"
pause
It should require minimal changes to get it to work on Sybase vs SQLServer.

Drop DB using MSBuild even if in use

I am using the following command in my MSBuild file to drop a database
sqlcmd -E -S <ServerName> -Q "DROP DATABASE <DBName>"
But sometimes I get the error
Cannot drop database because it is currently in use.
What command should I use so that the DB is dropped even if it is in use?
You can set your database to SINGLE_USER to drop all existing connections.
sqlcmd -E -S <ServerName> -Q "ALTER DATABASE <DBName> SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
sqlcmd -E -S <ServerName> -Q "DROP DATABASE <DBName>"
This is preferred to looping over each of the connections and dropping them one by one, because some applications will immediately reconnect.
To kill all connections to your database. See Kill All Active Connections To A Database
Now issue your DROP DATABASE command after the above procedure.
Please use something like this:
sqlcmd -S servername\instance -Q"use master;DROP DATABASE yourdatabase"
Try this:
sqlcmd -S .\MAPS -Q "RESTORE DATABASE Awards FROM DISK = 'C:\Awards_Project
\Awards_FULL.bak' WITH REPLACE, MOVE 'Awards' to 'C:\Awards_Project\Awards.mdf',
MOVE 'Awards_log' to 'C:\Awards_Project\Awards.ldf'"
You could alternatively restart the sql service and then perform the drop e.g. in a myfile.bat
#echo off
REM Requires administrative rights to restart service
REM open powershell and execute command in administrative mode
powershell -Command "Start-Process 'cmd' -Verb RunAs -ArgumentList '/c net stop MSSQLSERVER && net start MSSQLSERVER && exit'"
echo Please wait ~10 seconds for sql service to restart.
Timeout /t 10 /nobreak
echo Dropping database.
sqlcmd -S localhost -Q "drop database [database Name]"
You cannot drop a database that is in use. Check link for ref

Resources