Monday, April 15, 2024

Backup website and database via script part-1

 Hello everyone,

I've been looking for a way to backup my website and have been searching for it lately. On it, I discovered the Maulik Darji blog. In order to help produce a copy of specific folders and files from a specified path, he writes a script that zips the folders and copies them to the backup folder at the present location that is labeled with the date and time.

We must supply the folder or file names that we wish to backup to this script.
Thank you very much for the job, but we have a specific demand where we need to identify all folder names in backup, but we want to exclude some directories. We also required the SQL database backup in addition to that.

I divided the work on the following points, which I will discuss in this blog post in two parts:

  1. The Maulik Darji script has to be changed so that I pass the folder name I wish to exclude rather than the inclusion folder list.
  2. I need to discover a simple script or method for backing up my SQL database.
  3. In order to make both programs easy to run in a single script, I must finally integrate them.

Website Backup:

So I got to work, and after making a few changes, I wrote the script that can backup a website. The destination folder is where we now run the script, and that is where we need to supply the location from which we need to create a backup. The script is as follows:

#Change the site name frome below location or set any source path
$sourcePath = "C:\inetpub\wwwroot\sc93local"
#destination path is set as current folder
$destinationPath = get-location
#set the folders and file names in the array below. 
$exclude = @('App_Data','temp')
#Folder will be created with seconds as well. 
$folderName = (Get-Date).tostring("yyyy-MM-dd-hh-mm-ss")
Add-Type -assembly "system.io.compression.filesystem"
New-Item -itemType Directory -Path $destinationPath -Name $folderName
$newDestinationPath = "$($destinationPath)\$($folderName)"
write-host $newDestinationPath
$source = Get-ChildItem -Path $sourcePath -Filter "*" -Exclude $exclude
Foreach ($s in $source)
{
        if ($s.gettype() -eq [System.IO.DirectoryInfo])
        {
            $destination = Join-path -path $newDestinationPath -ChildPath "$($s.name).zip"
            If(Test-path $destination) 
            {
                Remove-item $destination
            }
            [io.compression.zipfile]::CreateFromDirectory($s.fullname, $destination)
        }
        else
        {
            $source = Join-path -path $sourcePath -ChildPath "$($s.name)" 
            $destination = Join-path -path $newDestinationPath -ChildPath "$($s.name)"
            Copy-Item -Path $source  -Destination $newDestinationPath
        }   
}

DataBase Backup:

For the purpose of database backup I used the script below to create a database backup. To do so, create a file called website backup.sql and insert the following code into it:

 

DECLARE @name VARCHAR(50) -- database name 
DECLARE @path VARCHAR(256) -- path for backup files 
DECLARE @fileName VARCHAR(256) -- filename for backup 
DECLARE @fileDate VARCHAR(20) -- used for file name

-- specify database backup directory
SET @path = 'C:\Personal\Tricks\DB backup\backup\' 

-- specify filename format
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) + REPLACE(CONVERT(VARCHAR(20),GETDATE(),108),':','')

DECLARE db_cursor CURSOR FOR 
SELECT name
FROM master.dbo.sysdatabases
WHERE name IN ('sc93_Core','sc93_Web', 'sc93_Master')

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @name  

WHILE @@FETCH_STATUS = 0  
BEGIN  
       SET @fileName = @path + @name + '_' + @fileDate + '.BAK' 
       BACKUP DATABASE @name TO DISK = @fileName 

       FETCH NEXT FROM db_cursor INTO @name  
END  

CLOSE db_cursor  
DEALLOCATE db_cursor

Then create a one-batch file named backup.bat and write the below code, which will target backup.sql:

sqlcmd -S 7AG1057Q00 -U sa -P Sitecore@123 -v path = "'C:\Personal\Tricks\DB backup\backup\'" -i "website backup.sql"

@pause

 

All you have to do is execute the script mentioned above to generate a database backup.

We'll combine the website and database backup scripts in our upcoming blog post.

I hope you enjoy this Sitecore blog. Stay tuned for more Sitecore related articles.

Till that happy Sitecoring :)

Please leave your comments or share this article if it’s useful for you.