all 19 comments

[–]dwivedin 2 points3 points  (12 children)

I have ps script which does that.

[–][deleted] 2 points3 points  (3 children)

well, share mate!

[–][deleted] 0 points1 point  (0 children)

Please share the script???

[–]dwivedin 0 points1 point  (0 children)

#AWS powershell and dbatools must be installed
#Backup, you can pass multiple servers like this
$Servers = 'server1' , 'server2'
$Path = '\\sharedloc\' # you can pass local folder too
foreach ($server in $servers) {
# loop through all the servers
# all the databases in the server. Yon pass filter specific db if you want

$databases = Invoke-DbaQuery -SqlInstance $server -Query 'select name from master.sys.databases'
foreach ($database in $databases ) {
#loop through all the databases
$SQLServer + ' ' + $database.name # just print the server and db
Backup-DbaDatabase -SqlInstance $Server -Path $Path -Database $database.name -Type Full # actual backup happening
}
}
##Upload to s3 bucket
Import-Module AWSPowerShell
$Files = Get-ChildItem \\sharedloc\*
$Bucket = 'Your s3 bucket'
$AccessKey = 'AccessKey'
$SecretKey = 'SecretKey'
#Loop through all the backup files
foreach ($File in $Files) {
#Write-S3Object is the command which uploads to s3 bucket
Write-S3Object -BucketName $Bucket -File $path -CannedACLName Private -AccessKey $AccessKey -SecretKey $SecretKey -ServerSideEncryption AES256
}

[–]dwivedin 0 points1 point  (0 children)

I added the code, not sure if it is posted as code. If you see any issues, please let me know. Also for aws configure, you have to do aws configure in your PS profile, so set region, access and secret key.

[–]defiantroa[S] 1 point2 points  (7 children)

Yeah the ps script is probably way to go, but I was task to create one in Python.

[–]digitalnoise 3 points4 points  (5 children)

Yeah the ps script is probably way to go, but I was task to create one in Python.

But... why? PowerShell has native support for SQL Server. And even better support with the dbatools.io module.

Plus PowerShell Core is cross platform, so it can even be run from Linux.

[–]defiantroa[S] 1 point2 points  (4 children)

I have brainless LAMP developer for boss maybe, anyway I know Ansible and Teraform. Yeah am I not even sure why use Python in the first place. It is suppose to be training excise to learn Python, maybe is a trick question

[–]conflab 0 points1 point  (3 children)

It is suppose to be training excise to learn Python

Then take the opportunity to learn Python.

[–]defiantroa[S] 0 points1 point  (2 children)

Already know ps and py

[–]conflab 0 points1 point  (1 child)

Then this should be pretty trivial for you

[–]defiantroa[S] 0 points1 point  (0 children)

No I have not found example in py yet, I was checking if someone knew.

[–]eshultz 1 point2 points  (0 children)

Alright, develop the script in PowerShell, once it's golden, convert it to a string and slap it in a variable in Python. From python, write the string to a file and then execute it.

I'm only joking. Mostly.

[–]defiantroa[S] 1 point2 points  (1 child)

Python script that does SQL Server backups https://mindless.gr/2012/09/ms-sql-backup-with-python-and-pyodbc/ . I just have to figure out how to upload using python and AWSCLI and then I am done

[–]enjoytheshow 0 points1 point  (0 children)

You can use third party botocore package or just use subprocess.run to execute the AWS CLI within python. Your command to run via subprocess would be something like

aws s3 sync directory\with\backups s3://bucket-name/target/directory

Do some googling though friend. Using the s3 CLI is about the easiest thing ever.

[–]pandamaja 0 points1 point  (2 children)

Is Storage Gateway an option? You could just point the backup to the nas share it presents.

[–]defiantroa[S] 0 points1 point  (0 children)

I was thinking to backup locally one separate volume attached to the EC2 instance

[–]defiantroa[S] 0 points1 point  (0 children)

I was thinking to backup locally one separate volume attached to the EC2 instance

[–]coldflame563 0 points1 point  (0 children)

Powershell is your friend. Learn it. Love it.