s c
s c

Reputation: 11

How to move backup files from windows server to AWS S3 bucket? But I also want 30 days backup file locally

I have some MS SQL backup files in different directories on windows server. I want to move all the files from different DB backups to S3 bucket.

Can you please suggest what would be the best approach?

My thought is to sync the folder in S3 bucket. Then delete old files keeping last 30 days files.

aws s3 sync "D:\Program data\..." s3://bucket_path
Get-ChildItem "D:\Program data\..." | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item

So if I delete from source path does this sync command also removes the file from bucket. Or is there any way I can flag in the aws command only.

This is also my first time windows scripting. Can I schedule above command from task scheduler from single bat file? I'm asking this one is powershell script & one id aws cli script.

Any input will be very valuable. Thanks.

Upvotes: 0

Views: 906

Answers (1)

Ranadip Dutta
Ranadip Dutta

Reputation: 9133

If you are trying to accomplish everything in powershell and if you have already aws cli configured with necessary permissions, then this would do your work: (I have added comments for better understanding)

$files = get-childitem -path C:\folderpath -recurse | where-object {$_.lastwritetime.year -lt (Get-Date).AddDays(-30)} ## Capturing the files less that 30 days
Foreach ($file in $files)
{
aws s3 cp $file s3://mybucket/$file    
} ##This would iterate all the files one by one and will upload in the bucket.

## get-childitem -path C:\folderpath -recurse | where-object {$_.lastwritetime.year -lt (Get-Date).AddDays(-30)} | Remove-Item -Force ## Upto you if you want to remove the files in once shot after being uploaded

Note: It is always recommended to use try/catch in these scenarios because there could be multiple issues with file paths, file being already in use, so on and so forth. So please ensure that you have proper error handling in place before executing in production.

Upvotes: 1

Related Questions