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.

 

Wednesday, March 13, 2024

Getting Started With Sitecore SXA Scriban

 Hi All,

We were recently required to create an SXA-based project with the requirement to use the SXA capability with as little code as possible.

I discovered that the SXA Scriban template was really helpful on that trip. The Scriban feature is really helpful, in my opinion. With the SXA Scriban template, there are a tonne of possibilities. We may generate multiple rendering variants by utilizing the Scriban template. 

LiquidJS is a technology used in the Scriban template. With that, we can use plain HTML inside the template and call object values using a syntax similar to MVC by simply placing "{{ }}" in place of dynamic values.

Ex:

 <div class="promo-text">  
   <div>  
     <div class="field-promotext">  
       {{ i_item.PromoText }}  
     </div>  
   </div>  
   <div class="field-promolink">  
     {{ i_item.PromoLink }}  
   </div>  

You need to be aware of the SXA cerating rendering variation before you go on this quest. If not, kindly click the following link to generate a rendering variation.

To begin using Scinban, you essentially need three things:

The embedding functions for the Scriban template may be found at 
https://doc.sitecore.com/xp/en/developers/sxa/93/sitecore-experience-accelerator/the-embedded-functions-for-the-scriban-template.html 

The embedded items and objects in the Scriban context can be found at 
https://doc.sitecore.com/xp/en/developers/sxa/93/sitecore-experience-accelerator/the-embedded-items-and-objects-in-the-scriban-context.html

https://doc.sitecore.com/xp/en/developers/sxa/93/sitecore-experience-accelerator/item-and-field-extensions.html is the URL for item and field extensions.


First, you need to be familiar with the terms used in the Scriban context, such as i_home, i_datasource, i_item, i_site, i_page, o_language, o_model, o_pagemode, etc. To learn more about these terms, click the link below:

https://doc.sitecore.com/xp/en/developers/sxa/93/sitecore-experience-accelerator/the-embedded-items-and-objects-in-the-scriban-context.html

After taking a brief look at the aforementioned URL, you are aware of the fundamental objects that you may utilize in your Scriban template in order to access the things that you require. I_item and o_model, which I utilized the most in our project, are my personal favorites.


We can refer to the following documents to gain access to items and field extensions:

https://doc.sitecore.com/xp/en/developers/sxa/93/sitecore-experience-accelerator/item-and-field-extensions.html

It will be very beneficial for initiating the scriban and obtaining certain fundamental values in the scriban template.

You can search for the embedded function after you have covered these two subjects. Using that, you can use the following url to perform some basic operations, such as retrieving field values, displaying photos, retrieving link values, searching for things, retrieving dictionary values, etc.

https://doc.sitecore.com/xp/en/developers/sxa/93/sitecore-experience-accelerator/the-embedded-functions-for-the-scriban-template.html 

I'm hoping this will motivate you to begin working at SXA Scriban. Please let me know if I can be of any assistance to you. I'd be glad to assist you. We also value your feedback.

I'll try to include additional blogs with Scriban example codes and other proof-of-concept websites built entirely using SXA Scriban, complete with search features, in upcoming blog articles.

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.  

Some Other References: Clone an existing SXA component to create a brand new component

https://github.com/scriban/scriban/blob/master/doc/builtins.md