C# .NET - creating sql db back from web application

Asked By SVK N on 19-Jun-12 10:54 PM
"BACKUP DATABASE db1  TO DISK='" + backfolder + "db1"  + ".bak' WITH FORMAT

backfolder is the path given in webconfig

if i give unc path as \\servername\d\bkfolder\
i get error as cannot  open backup device.
backup terminated
[)ia6l0 iii replied to SVK N on 20-Jun-12 10:10 PM
If you run this command in an ASP.Net web application, it runs under the context of the Identity of the application under the Application Pool. Change the application pool to run under a privileged account which has permissions to the UNC Share folder. 

If you let the application pool run under the Network Service account, then provide the read/write permissions to the UNC share folder for the network service account. 

Also note that the SQL Server service can run under a different set of account - local/network. This account needs to let SQL Server create backups on the UNC.  This is valid in the case, you are not creating backups from ASP.Net web application.

This should resolve the issue.

Hope this helps.
Vikram Singh Saini replied to SVK N on 22-Jun-12 05:29 AM
As it is web application , so I would recommend you to use impersonation (for admin account which have right for taking backup) for the page from which you are trying to run the backup code.

For e.g. you have the code in Default.aspx.cs for backup. So for it impersonate in web.config as (within configuration tags):

<location path="Default.aspx" allowOverride="false">
            <identity impersonate="true" userName="AdminUser" password="AdminPassword"/>

SVK N replied to Vikram Singh Saini on 23-Jul-12 03:25 AM
i got it done using the following sql stmt
BACKUP DATABASE db1  TO DISK='" + backfolder + "db1.bak' WITH FORMAT";

backfolder =  frm webconfig
<add key="backup" value="d:\backup\" />

Vikram Singh Saini replied to SVK N on 23-Jul-12 03:53 AM
Nice to know.