AWS SSH: Difference between revisions
Jump to navigation
Jump to search
(3 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
* mysqld | * mysqld | ||
* httpd | * httpd | ||
Example usage: | Example usage: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 36: | Line 37: | ||
NOTE: Requires both JQ and AWS CLI are already installed !!! | NOTE: Requires both JQ and AWS CLI are already installed !!! | ||
NOTE: This may no longer be needed as Lightsail now has a built-in snapshot automation. | |||
Retrieve or create AWS Access Key & Secret Key from the account that has permissions on the LightSail instance. | Retrieve or create AWS Access Key & Secret Key from the account that has permissions on the LightSail instance. | ||
Line 105: | Line 108: | ||
===Make file executable=== | ===Make file executable=== | ||
<syntaxhighlight lang="bash"> | |||
sudo chmod 755 lightsail.backup.sh | sudo chmod 755 lightsail.backup.sh | ||
</syntaxhighlight> | |||
===Add Crontab entry=== | ===Add Crontab entry=== |
Latest revision as of 11:49, 28 October 2020
Working with AWS through Shell Sessions
MySQL EVENT Scheduler
Enable event scheduler (etc/my.cnf) to run this event:
CREATE EVENT clear_router ON SCHEDULE EVERY 1 WEEK STARTS CURRENT_DATE + INTERVAL 6 - WEEKDAY(CURRENT_DATE) DAY DO TRUNCATE ft_form_21;
This should truncate (clear) table ft_form_21
on Sunday.
Start / Stop / Restart Services
Login via ssh
sudo service <service_name> [start | stop | restart | status]
Example services:
- mysqld
- httpd
Example usage:
sudo service mysqld restart
LetsEncrypt Scheduled Task
sudo -i
crontab -e
Add the following (press to insert new lines)
0 4 1,15 * * /home/ec2-user/certbot-auto renew
Snapshot Script
NOTE: Requires both JQ and AWS CLI are already installed !!!
NOTE: This may no longer be needed as Lightsail now has a built-in snapshot automation.
Retrieve or create AWS Access Key & Secret Key from the account that has permissions on the LightSail instance.
Configure the instance via CLI
aws configure
Supply Access & Secret Key, region (us-east-1), output (json)
Test
aws lightsail get-instance-snapshots
Create File
Create new file 'lightsail.backup.sh' in ec2-user home directory (or sub-directory) and add code below
#!/bin/bash
######################################
## CREATE A NEW SNAPSHOT BACKUP ##
######################################
## This script takes in three arguments the name of your instance, the region, and the number of snapshots to keep.
NameOfYourInstance=$1
NameOfYourBackup=$NameOfYourInstance
Region=$2
aws lightsail create-instance-snapshot --instance-snapshot-name ${NameOfYourBackup}-$(date +%Y-%m-%d_%H.%M) --instance-name $NameOfYourInstance --region $Region
## Delay before initiating clean up of old snapshots
sleep 30
###############################################
## DELETE OLD SNAPSHOTS + RETAIN SNAPSHOTS ##
###############################################
# Set number of snapshots you'd like to keep in your account
snapshotsToKeep=$3
echo "Number of Instance Snapshots to keep: ${snapshotsToKeep}"
# get the total number of available Lightsail snapshots
numberOfSnapshots=$(aws lightsail get-instance-snapshots | jq '[.[] | select(.[].fromInstanceName == "'${NameOfYourInstance}'") ]| length')
echo "Number of instance snapshots: ${numberOfSnapshots}"
# get the names of all snapshots sorted from old to new
SnapshotNames=$(aws lightsail get-instance-snapshots | jq '.[] | sort_by(.createdAt) | map(select(.fromInstanceName == "'${NameOfYourInstance}'")) | .[].name')
# loop through all snapshots
while IFS= read -r line
do
let "i++"
# delete old snapshots condition
if (($i <= $numberOfSnapshots-$snapshotsToKeep))
then
snapshotToDelete=$(echo "$line" | tr -d '"')
# delete snapshot command
aws lightsail delete-instance-snapshot --instance-snapshot-name $snapshotToDelete
fi
done <<< "$SnapshotNames"
exit 1
Make file executable
sudo chmod 755 lightsail.backup.sh
Add Crontab entry
sudo crontab -e
Add new line containing:
0 0 * * * /home/ec2-user/lightsail.backup.sh <instance_name> <region> <number_of_snapshots>