I'm using SQL server 2005 and above and I wondering if there is a way to remove a specific backup file from the default SQL backups folder...
I can find that backups folder by using the query below:
EXEC master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer',N'BackupDirectory'
The only way that I found was using the master.dbo.xp_delete_file operation, but the problem is that its cannot remove only one file (the requested one).
Any idea?
You might use xp_cmdshell to spawn a Windows command shell and execute a given string, as in:
xp_cmdshell 'del "E:\Database Backups\Some Database Backup.bak"'
Combined with your code to determine the default backup folder, you could construct the appropriate string to delete a specific file.
Note that xp_cmdshell is locked down by default for security reasons (obviously, running any arbitrary command string would be bad).
Related
I have a backup application and some of my customers want their SQL Server databases backed up. I need SQL Server to give me compressed files with file fixed name (without timestamp). I have tried using the command 'SqlCmd -E -S...' but it does not compress the database (need to change configuration in SQL Server) which customers are not comfortable with. Also due to lack of free space on the hard disk we need it compressed. With 'SQL Enterprise Studio' the backup that takes place always has a timestamp in the name. I need the backup filename fixed eg: ABC.BAK
Do you necessarily have to use this backup application you mentioned? I'd just whip the T-SQL at it:
BACKUP DATABASE [CustomerDB] TO DISK = N'D:\ABC.BAK' WITH COMPRESSION
GO;
That'll let you define the file names as well as override the server's default compression for that backup.
You could use sqlcmd to both compress the backup and give the backup file whatever name you want. sqlcmd can take an input file to execute against a SQL Server. Here is an example of the sqlcmd you would run
sqlcmd -S localhost -E -i ./SQLQuery1.sql
Where I'm connecting to my localhost installation of SQL Server (default instance name), and a trusted connection. You know this bit already of course. The new thing is the command-line option at the end.
-i ./SQLQuery1.sql
Here is an example SQLQuery1.sql file (you can of course call it whatever you would like)
USE [master]
GO
DECLARE #Path NVARCHAR = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\Example.bak';
BACKUP DATABASE [Example] TO DISK = #Path
WITH NAME = N'Example-Full Database Backup'
,COMPRESSION;
GO
Change the #Path variable to wherever you want the backup to be written and change the name of the file at the end of the #Path variable to whatever you want to call the file.
I want to run batch file from SQL Job without using exec xp_cmdshell.
Any idea?
Thanks
You could use a SQL Server Job, otherwise i cannot think of a way you could without xp_cmdshell.
Take a look at this
I want to run batch file from SQL Job without using exec xp_cmdshell.
Any idea?
Worth to mention that you can also leverage SQLCLR.
Example: CLR Stored procedure to execute command
Some other googlable threads:
How to execute a DOS command when xp_cmdshell is disabled in SQL Server
Executing an external process() in SQLCLR Project
Such approach introduces severe risks like memory leaks, crashing of underlying .net app pool etc
Therefore another link: Security in the CLR World Inside SQL Server
Instead of running batch file, i have created power shell and ran it from SQL job. It satisfy my requirement and resolved my issue.
Do it like the picture: like this image.
The drive containing the batch file should be other than the C drive, to avoid trouble.
Add execute, read and write permissions for the user, which you are using to run the batch file, to get the username run this query: EXEC master..xp_cmdshell 'whoami', get the name after the \ sign. For example "nt service\mssqlserver". Add permission for this user: mssqlserver
Finally make sure you put the batch file on the same server as where you execute your Job.
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.
I am trying to copy a back up file from a remote system to local using
EXEC master.sys.xp_cmdshell 'XCopy src dest'
command.
The above mentioned command returns file not found error. However the XCopy command copies file from the remote system to the local system when run in command prompt of the local system. Can somebody suggest a solution?
You are trying to access a remote server. When you attempt this from the command line you are using your permissions.
However, xp_cmdshell is run with the permissions of the SQL Server service account. Of course this account should be run with minimum permissions, you may need to increase its permissions to the other server.
MSDN
Also, don't forget that when the xp_cmdshell executes under the SQL account permissions that mapped network drive letters won't be available. You must use UNC path. i.e. \\servername\sharename\path\file
I am trying to execute a perl script from xp_cmdshell.
The output of the perl script is a csv file, but when I run
EXEC master..xp_cmdshell N'perl G:\script\perl.pl';
I can't find the csv file created, though the xp_cmdshell command seems to run fine, the output is the name of file that has to be created.
I am using xp_cmdshell to create a job step to execute the perl script.
Any help would be appreciated.
Since you're running this via a SQL Agent job, it'll be much safer to disable the use of xp_cmdshell via sp_configure (ref1 | ref2) and use a CmdExec job step instead.
When configuring the job step, be sure to go to the advanced page and enable job step logging to a table.
This will allow you to better troubleshoot the issues you're having with the perl job in general, as the issue could be related to something entirely outside the context of the database engine.