Home » AWS Lightsail Configure Windows server auto backup

AWS Lightsail Configure Windows server auto backup

Amazone AWS

by Salman Chawhan
0 comment

1st Download AWS Cli Exe file
https://docs.aws.amazon.com/cli/latest/userguide/install-windows.html

After Install AWS Cli Open Command Prompt
Run –> aws configure
and enter IMA User AWS Access Key ID and AWS Secret Access Key

2 Method for create backup

1 Winnows Batch File
2 Windows Ps File (Power shell)

1] Using Batch File
Open text file ad bellow code

@echo off
for /f “tokens=*” %%a in (‘
“wmic path Win32_LocalTime get year,month,day /value|findstr =”
‘) do @set %%a
echo %year%%month%%day%

aws lightsail create-instance-snapshot –instance-name servername –instance-snapshot-name %year%%month%%day%Mail365always –region region-name

Replace
servername –> You Instance Name
region-name –> ex: ap-southeast-1

Save file .bat
after save file double click on file (Your code will run and Instance snapshot start)

2] Windows Ps File (Power shell)

Create .ps1 file
add Bellow Code in that ps file

$ErrorActionPreference = “stop”
try {
<#
The try block will only catch errors in PS execution.
To check for errors in the response from the AWS service, can parse the response info and check..
Also, can look at CloudWatch API for more advanced options to keep an eye on the snapshot creation and removal processes#>

<#These are some parameters for email authentication with office365, if the script errors, it will send an email. Password can be stored as encrypted string somewhere as needed#>
$emailUsername = “some.email@somecompany.com”
$emailTo = “some.other.email@somecompany.com”
$emailassword = “TypeOrGetFromYourFavoriteEncryptionMethod”
$cred = New-Object System.Management.Automation.PSCredential ($emailUsername,(ConvertTo-SecureString $emailassword -AsPlainText -Force))

<#The default config of the script would remove all snapshots that are older then 5 days ago. NumberOfDaysToKeep variable value can be changed as needed#>
$NumberOfDaysToKeep = 5
<#The InstanceName is needed to identify which instance to snapshot as well as used for snaphot name base#>
$InstanceName = “MyInstance123”

<#First, issue the command to create a new snapshot, append a timestamp to snapshot name#>
$CurrentTimestamp = Get-Date -Format “yyyyMMdd-HHmm”
$InstanceSnapshotName = $InstanceName + “-” + $CurrentTimestamp
<#Create a snapshot#>
New-LSInstanceSnapshot -InstanceName $InstanceName -InstanceSnapshotName $InstanceSnapshotName

<#Next, check if there are older instances that need to be removed, to save space..

#Calculate actual date threashold to use to remove snapshots that are older than that#>
$MinDate = ([datetime]::Today).AddDays($NumberOfDaysToKeep * -1)

<#Get all instance snapshots, sort them descending by created date (can delete based on number of instances, not dates..)#>
$CurrentSnapshots = Get-LSInstanceSnapshotList | Sort-Object -Property CreatedAt -Descending

<#Iterate through the instances, and remove any instance that is older then the threashold min date#>
foreach ($SnapshotItem in $CurrentSnapshots) {
if ($SnapshotItem.CreatedAt -le $MinDate)
{
Remove-LSInstanceSnapshot -InstanceSnapshotName $SnapshotItem.Name -Force
}
}
}

catch
{
$msg = $Error[0].Exception.Message
<#Write the error to the console in case this is run interactively and also send an email..#>
Write-Host $msg
Send-MailMessage -From $emailUsername -To $emailTo -Subject “LS Snapshot script errored” -Port 587 -Body $msg -SmtpServer “smtp.office365.com” -UseSsl -Credential $cred
exit 1
}

Replace demo data with your actual server details
Also, you can add SMTP details for email notification

Leave a Comment

three × four =

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00