Auto backup of MSSQL database on SQL express 2012

Rajeev Mishra 1 Reputation point
2021-10-19T05:46:29.79+00:00

Hi

I want to schedule auto back up of my SQL database in sql express 2012 edition. For that I used batch file and it works on SQL server 2017 but it says login failed when it executes on express edition. Batch Code is as under:

@Echo off
cls

set SqlServer=LAPTOP-GN1OEU2R
set InstanceName=SQLEXPRESS
set Username=sa
set Password=Olwtswl@123
set Database=StatconNoida
set LocalFolder=D:\SQLBackups

rem ************************************
rem * Don't touch anything below here. *
rem ************************************

echo Getting current date and time...
echo.
for /f %%a in ('sqlcmd -S %SqlServer% -U %Username% -P %Password% -Q "SET NOCOUNT ON select ltrim(convert(date, getdate()))" -h -1') do set CurrentDate=%%a
for /f %%a in ('sqlcmd -S %SqlServer% -U %Username% -P %Password% -Q "SET NOCOUNT ON select right('00' + ltrim(datepart(hour, getdate())), 2)" -h -1') do set CurrentHour=%%a
for /f %%a in ('sqlcmd -S %SqlServer% -U %Username% -P %Password% -Q "SET NOCOUNT ON select right('00' + ltrim(datepart(minute, getdate())), 2)" -h -1') do set CurrentMinute=%%a
for /f %%a in ('sqlcmd -S %SqlServer% -U %Username% -P %Password% -Q "SET NOCOUNT ON select right('00' + ltrim(datepart(second, getdate())), 2)" -h -1') do set CurrentSecond=%%a

echo.
echo Backing up database to %LocalFolder%
echo.
SqlCmd -S %SqlServer% -U %Username% -P %Password% -Q "Backup Database %Database% To Disk='%LocalFolder%\%Database%-%CurrentDate%_%CurrentHour%%CurrentMinute%%CurrentSecond%.bak'"

echo.
echo.

Thanks in advance

Developer technologies Transact-SQL
SQL Server Other
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2021-10-19T05:58:41.24+00:00

    but it says login failed when it executes on express edition

    Has nothing to do with the edition.
    You use SQL login for authentication; is your SQL Server running in "Mixed Mode" = SQL + Windows auth enabled? Default is Windows auth only.


  2. YufeiShao-msft 7,146 Reputation points
    2021-10-19T07:15:52.443+00:00

    Hi @Rajeev Mishra ,

    Please try to use mixed authentication. On SSMS, server properties-> security undere Server authentication, select SQL Server and Windows Authentication mode

    you backup of all databases in the local named instance of SQLEXPRESS by using a SQLLogin and its password.

    There is a doucment about schedule and automate backups of SQL Server databases in SQL Server Express, you can refer to:
    https://learn.microsoft.com/en-us/troubleshoot/sql/admin/schedule-automate-backup-database

    -------
    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


  3. Tom Phillips 17,771 Reputation points
    2021-10-21T11:29:05.437+00:00

    Forget about the script and figure out your login.

    Open a command prompt and run:

    sqlcmd -S SqlServer -U Username -P Password

    Until it works.

    Also, please post the exact message you are receiving.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.