How to Restore database automatically and periodically without Jobs? - sql-server

I am allowing guest users to log onto my database and so I want to restore the database once a week to wipe it of the quest users details, etc.
I am using SQL Server 2008 R2 Express. This version does not have Jobs.
Is it possible to automatically resotre the database without the use of a job?

You could use a Windows Scheduled task, which executes sqlcmd and makes the necessary RESTORE DATABASE calls.

Use Batch File
Write the sequel Command Which will Execute the restore operation Using batch file
Then Using Task scheduler trigger automatically that batch file to run whenever neccesery.

Related

Batch file for auto backup

I have a server in which I'm running an SQL Server Express DB and an Azure blob i which I Upload each morning the backup of the SQL Server.
Now, I've been able to automate the backup via a mix of SQL query + batch file and I have it scheduled into my task scheduler to run each night at 9:00pm, but I would like to move also a copy of the backup from the server to the Azure Storage.
I've already tryed a batch file in task scheduler:
echo off
copy "Z:\Backup\SQLBackup\" "C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup\DailyStep_bck.bak"
But it doesn't work by itself, only if I run it manually.
Each day the current copy should replace the older one, I don't need retention of old bacups for now.
I've tryed also robocopy and it also doesn't work... could someone tell me what am I missin?
the task is running as administrator with the "Run wether the admin is loged in or not" option.
thanks for your help.
I summarize the solution as below.
Windows task scheduler doesn't have permissions to the mounted azure file share drive. So we need to use the path https://[Azure_account].file.core.windows.net/storage/Backup to access Azure file share. Besides, if you want to implement auto-upload, you can use azcopy.
For example
Create a sas token for the file share
Script
azcopy copy 'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup*.bak' 'https://[Azure_account].file.core.windows.net/storage/Backup/SQLBackup/[backup_file].bak?<sas token>
Create a schedule task
For more details, please refer to here and here

SQL Server to check for files in directory

I was wondering if it is possible for SQL Server to check a directory for files and run a stored procedure. I did some research and found this, but I am wondering if there is a way to do what I want WITHOUT SSIS.
EDIT: After reading my post, I realized I should have been more specific. Is there a way to AUTOMATICALLY or set SQL Server to check for files in a directory and run a stored procedure?
You can use xp_cmdshell to run file related commands. To get a directly listing:
exec xp_cmdshell 'dir *.csv';
You can also use bulk insert to load a file from disk into a table and take actions based on the loaded contents.
Normally you'd use the File Watcher Task with SSIS. But you can also use SQL Server Agent to schedule a task for periodic execution, schedule a task with Windows Task Scheduler, or configure a stored procedure to runs at startup with sp_procoption that pauses (using waitfor) between processing times.

Is it possible to upload ftp files from SQL server without xp_cmdshell?

Ideally would like to run something from a SQL query or SQL agent job to FTP upload a file to an external site, but cannot use xp_cmdshell.
Yes. You need to split your work into two separate tasks:
How to run executable or a batch program from within SQL Server without resorting to xp_cmdshell.
An example of how to do it can be found in:
https://www.mssqltips.com/sqlservertip/2014/replace-xpcmdshell-command-line-use-with-sql-server-agent/.
You should modify this example to suit your particular needs. Suggested stored procedure would:
run command passed as a parameter in created on-the-fly SQL job (indicate CmdExec subsystem)
wait for SQL job completion (query msdb.dbo.sysjobactivity) or kill the job if predefined timeout value has been reached
return results of job execution (query msdb.dbo.sysjobhistory)
delete the job
Note: Full version of SQL Server required. If you are using express version, you would have to manually define a windows scheduled task.
How to send a file via ftp using a batch program.
Please see:
How to ftp with a batch file?

Copy .bak file to another Windows server using job MS SQL Server agent

I am trying to copy a .bak file nightly from Server A to Server B.
Can I do that using SQL server Job Agent to run this every night?
I am thinking of adding the copy command as a statement within a step of a job.
Something like: 'copy "G:\source\folder\" "\target\folder\"'
inside the step and setting the type to Operating System(CmdExec).
Is there a way to do it?
is this question about the command to copy the files?
If you want to copy entire folder use robocopy instead of copy
You can make a SSIS package to do that, and then run it from the SQL agent.
However, don't use logical drives, such as G: -- if the server doesn't have the same mapping, it won't work. Use the actual named servers: \serverA\source\folder to \serverB\target\folder.
Short answer is yes. You can try SSIS package as described here or here. Another option is to use windows task scheduler (vs using SQL Server Agent) and a simple bat script to do the same thing.

How to copy MSSQLServer Database automated from one server to another

I would like to copy one database (almost all tables) to another server. So far we have done this using the standard MSSQL wizard. We would like to include this work into an automated build we have with ant.
I have found one command line tool: http://dbcopytool.codeplex.com/
Any better ideas?
Though I've not done this with Ant, I have done all of this with NAnt (and the links below are for Ant).
I would use the Sql task to issue a backup command, then a Apply/ExecOn command to issue the backup copy to the new server (Or the Copy task) , and then another Sql task to issue the restore on the new server.

Resources