September 11, 2023 at 11:52 am
All,
I'm new to Powershell so this could be a simple error.
I'm using a SQL agent job to backup a database to a UNC path and then rename it.
The step type is 'Powershell"
The backup is done using Backup-SqlDatabase and works fine. It backups up directly to the UNC path
The rename is done as follows:
Rename-Item -Path "\\path\filename.txt" -NewName "test.bak"
I get the error "Invalid path '\\path\filename.txt'
I've used \\path\filename.txt in this post instead of the real path.
The path is valid and if I run the command from Powershell ISE it works fine.
Should it be possible to use rename-item, with a unc path, from a SQL Agent job? If so I'd appreciate any thoughts on what might be causing the error? I've checked permissions and it doesn't look to be that.
Thanks
September 11, 2023 at 3:12 pm
As a random guess, does the SQL Agent Service account have access to the file you are trying to rename? If I had to guess I would say It is something with permissions on the file/folder.
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
September 11, 2023 at 7:48 pm
As a bit of a sidebar, renaming backup files destroys the ability to use the backup information stored in MSDB for research. If you want to name something, it should be named as a part of the BACKUP command.
And, to be honest, I see no reason at all to have to use PowerShell for anything having to do with backups or restores.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 11, 2023 at 8:14 pm
The ONLY reason I see to use powershell for a backup process would be if you needed to move the file to a location that the SQL agent service didn't have access to, but then it would be being run outside of SQL anyway (ie windows scheduled task).
But, like Jeff, I never rename my backups and only move them from the backup location to move them to long term storage backup and that is an automated process handled by our IT team (ie not me).
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
September 11, 2023 at 8:57 pm
The ONLY reason I see to use powershell for a backup process would be if you needed to move the file to a location that the SQL agent service didn't have access to, but then it would be being run outside of SQL anyway (ie windows scheduled task).
But, like Jeff, I never rename my backups and only move them from the backup location to move them to long term storage backup and that is an automated process handled by our IT team (ie not me).
Additionally - I would use PowerShell to kick off Robocopy to copy the backup file(s) to a shared folder that is not on the local server. If the backup directly to the UNC path takes too long - backing up to local and copying the file can be a better option.
The only time I would use something outside SQL Server to perform backups is if the Edition does not include the agent.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
September 14, 2023 at 6:26 pm
All,
I'm new to Powershell so this could be a simple error.
I'm using a SQL agent job to backup a database to a UNC path and then rename it.
The step type is 'Powershell"
The backup is done using Backup-SqlDatabase and works fine. It backups up directly to the UNC path
The rename is done as follows:
Rename-Item -Path "\\path\filename.txt" -NewName "test.bak"
I get the error "Invalid path '\\path\filename.txt'
I've used \\path\filename.txt in this post instead of the real path.
The path is valid and if I run the command from Powershell ISE it works fine.
Should it be possible to use rename-item, with a unc path, from a SQL Agent job? If so I'd appreciate any thoughts on what might be causing the error? I've checked permissions and it doesn't look to be that.
Thanks
I'm really curious about the reasons you chose to:
Instead of:
Can you tell us why you chose this method?
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
September 15, 2023 at 1:13 am
as_1234 wrote:All,
I'm new to Powershell so this could be a simple error.
I'm using a SQL agent job to backup a database to a UNC path and then rename it.
The step type is 'Powershell"
The backup is done using Backup-SqlDatabase and works fine. It backups up directly to the UNC path
The rename is done as follows:
Rename-Item -Path "\\path\filename.txt" -NewName "test.bak"
I get the error "Invalid path '\\path\filename.txt'
I've used \\path\filename.txt in this post instead of the real path.
The path is valid and if I run the command from Powershell ISE it works fine.
Should it be possible to use rename-item, with a unc path, from a SQL Agent job? If so I'd appreciate any thoughts on what might be causing the error? I've checked permissions and it doesn't look to be that.
Thanks
I'm really curious about the reasons you chose to:
- create a SQL Job
- calls PowerShell to do a backup
- Calls PowerShell to rename the file
Instead of:
- Execute SQL to backup database to the filename you want
Can you tell us why you chose this method?
He didn't answer that question when I asked it earlier in this thread, either. I don't understand why people even consider doing it with PowerShell. It's just another unneeded complexity.
I remember when PoSh first came out... It seems like everybody and their bother used PoSh to create a centralize backup system for all their servers... without realizing the absolute hell their exploding log files on all the databases on all their servers would cause if that central system couldn't get to one of the servers or if it outright failed for a period of time. 🙁
--Jeff Moden
Change is inevitable... Change for the better is not.
September 15, 2023 at 10:59 am
All,
Thank you for your help and apologies for not replying. Another issue has taken my attention and I haven't had chance to reply.
I will reply to everyone's questions soon, probably tomorrow.
Regarding the original issue - I think it's a permission issue that I overlooked. I will update the topic when I know.
Thanks
September 25, 2023 at 12:29 pm
All,
Firstly apologies for posting a question and not replying. That wasn't my intention. Some unexpected issues took my time.
The reason for renaming is that the backup will be picked up by another process which has no access to SQL server and I want to avoid the situation where that process picks up the backup file before it completes. Therefore my plan is the following will happen:
Create backup to a temporary name
Rename to a final name
The other process is only looking for the final name
Currently Powershell backups the database to a UNC path using Backup-SqlDatabase with no problem at all. However the renane-item says that it can't find the file. The account that SQL agent uses can access the file if I logon to Windows so I'm thinking it's either:
Powershell is treating something in the path as escape characters. I've tried using the -LiteralPath parameter to avoid that
There is a security context difference between Backup-SqlDatabase and rename-item which I haven't found yet. I've tried using move-item with the same issue.
I think I've answered everyone's questions. Apologies if I missed any. I'll also post if I find the answer.
September 25, 2023 at 1:00 pm
It sounds like you need to look at mirrored backups.
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply