Atilla Tanrikulu

I am an experienced software engineer and architect living in Germany. I’m passionate about distributed scalable enterprise web-based microservices/applications and delivering great user experiences. I have created some amazing enterprise-level applications that many people have used and hopefully enjoyed.

Articles

Java Quick Reference Apache Kafka Tutorial Guvenli Kod Gelistirme Making an Enterprise Scale Angular Project Step by Step Nightly SQL Server Database Backup with command line batch file and windows scheduler AOP Framework without proxy pattern IdentityServer Nedir Middleware Pattern With Csharp And Javascript Docker most used commands Online Proje Dokumantasyonu, Docker, Nginx, mdwiki How to use Github Pages for static websites Inheritance with JavaScript, EC6 (ECMAScript 6, ECMAScript 2015) Object oriented javascript and Inheritance Singleton Pattern with Javascript Factory Pattern with Javascript Open terminal here mac os x service IdentityServer4-Angular-6-integration JMater notlari, kurulum ve kullanim Learn Jekyll in 12 Steps Make Mac Application with Automater from sh script Make spotlight index markdown or code files OAuth 2.0 Nedir (RFC6749) Using Custom CSS and Custom JavaScript to an Angular Project Cross Platform Desktop Application With .Net Core 2x and Angular 6x front-end projects with nodejs gulp bower yeoman and angularjs Host Asp.Net Core on Linux with Apache Redis kurulumu ve ayarlari Useful Mac OS Apps Choosing internet connection on multiple interface windows Name Server Kurulumu How to define domain name for your dynamic IP SQL table data compare, and prepare insert satements Useful Git Commands TFS ile Otomatik deployment yapmak Spring Boot Tutorial Sql server icin maliyetli sorgularin tespit edilmesi Arama Motoru Optimizasyonu (SEO) My installed mac apps

Nightly SQL Server Database Backup with command line batch file and windows scheduler

  • We create windows scheduled task then select running period for Backup.bat file.
  • Backup.bat file performs database backup then transfers backup file to another servers.
  • Backup.bat file also performs nightly backup that is named with current date.
  • Backup.sql called from Backup.bat file.
  • Restore.bat file performs restoration on target server. Restore.bat file schaduled on target server and it will have ran after backup operation.

Backup.bat

:: Delete old backup files
del /Q /F D:\Backups\nightly\mydatabase.bak >> log.txt

:: Locate SQL server SQLCMD.exe tool 
"C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\SQLCMD.EXE" -S HOSTNAME -i "Backup.sql" >> log.txt
echo Backup is starting: %date% %time%  >> log.txt

:: Distribute other servers

:: Target Server: Staging
net use m: \\192.168.1.10\d\backups /U:HOSTNAME1\administrator password /p:Yes >>log.txt
C:\Windows\System32\xcopy "D:\Backup\nightly\mydatabase.bak" m: /E /Y >> log.txt

:: Target Server: Test
:: Target server can restore it nightly
:: Target server can execute some scripts after restoration e.g : update-same-password-to-all-user.sql, manipulate-confidential-data.sql
net use m: \\192.168.1.11\d\backups /U:HOSTNAME2\administrator password /p:Yes >>log.txt
C:\Windows\System32\xcopy "D:\Backup\nightly\mydatabase.bak" m: /E /Y >> log.txt

:: Target Server: Backup Server
net use R: \\192.168.1.12\backups /U:HOSTNAME3\administrator password /p:Yes >>log.txt
echo f|C:\Windows\System32\xcopy "D:\Backup\nightly\mydatabase.bak" R:\mydatabase_%date:~6,4%%date:~3,2%%date:~0,2%.bak /E /Y /I >> log.txt

echo completed >> log.txt

Backup.sql


DECLARE @dest nvarchar(255)

SET @dest = 'D:\Backups\nightly\mydatabase.bak'
BACKUP DATABASE [mydatabase] TO  DISK =@dest WITH NOFORMAT, COMPRESSION ,NOINIT,  NAME = N'mydatabase-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'mydatabase' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'mydatabase' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''mydatabase'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM  DISK =@dest WITH  FILE = @backupSetId,  NOUNLOAD,  NOREWIND
GO

Restore.bat

echo off
cls
echo -- RESTORE DATABASE --
set BACKUPFILENAME=mydatabase.bak
set DATABASENAME=mydatabase
set SERVERNAME=HOSTNAME2
sqlcmd -E -S %SERVERNAME% -d master -Q "ALTER DATABASE [%DATABASENAME%] SET SINGLE_USER WITH ROLLBACK IMMEDIATE"

:: WARNING - delete the database, suits me
sqlcmd -E -S %SERVERNAME% -d master -Q "IF EXISTS (SELECT * FROM sysdatabases WHERE name=N'%DATABASENAME%' ) DROP DATABASE [%DATABASENAME%]"
sqlcmd -E -S %SERVERNAME% -d master -Q "CREATE DATABASE [%DATABASENAME%]"


:: restore
sqlcmd -E -S %SERVERNAME% -d master -Q "RESTORE DATABASE [%DATABASENAME%] FROM DISK = N'F:\backups\%BACKUPFILENAME%' WITH  FILE = 1,  MOVE N'mydatabase' TO N'F:\data\mydb.mdf',  MOVE N'mydatabase_log' TO N'F:\data\mydb_1.ldf', NOUNLOAD,  REPLACE,  STATS = 10"
::sqlcmd -E -S %SERVERNAME% -d master -Q "RESTORE DATABASE [%DATABASENAME%] FROM DISK = N'F:\backups\%BACKUPFILENAME%' WITH  REPLACE"

sqlcmd -E -S %SERVERNAME% -d master -Q "ALTER DATABASE [%DATABASENAME%] SET MULTI_USER"
sqlcmd -E -S %SERVERNAME% -d master -Q "use mydatabase"
sqlcmd -E -S %SERVERNAME% -d master -Q "GO"

:: Executing custom sql statements
sqlcmd -E -S %SERVERNAME% -d master -Q "UPDATE mydatabase.dbo.Users set Password='098f6bcd4621d373cade4e832627b4f6'"
::sqlcmd -E -S %SERVERNAME% -d master -Q "truncate table mydatabase.dbo.BigLogTable"
sqlcmd -E -S %SERVERNAME% -d master -Q "update mydatabase.dbo.Config set Val = 'new config' where Id = 'DD46010A-9581-4096-9406-BEEE87A16B8A'"
sqlcmd -E -S %SERVERNAME% -d master -Q "update mydatabase.dbo.Sabit set Val = new config' where Id = 'BF0BCE43-74CF-435F-A787-6A77BCC6A4DA'"

:: Create Test Users
sqlcmd -E -S %SERVERNAME% -d mydatabase -Q "CREATE USER testuser1 FOR LOGIN [testuser1]"
sqlcmd -E -S %SERVERNAME% -d mydatabase -Q "EXEC sp_addrolemember 'db_datareader', 'testuser1'"
sqlcmd -E -S %SERVERNAME% -d mydatabase -Q "ALTER LOGIN testuser1 WITH PASSWORD='DTCFdtcf1*'"


Date: 2018-07-15 10:20:00 +0000