Bash Script to Back Up and EC2 Instance
When you're managing EC2 instances in AWS, backups aren't optional, but a critical part of managing your cloud infrastructure. If you’re updating a server, applying patches, or making big configuration changes, having a fresh backup could save you from hours of troubleshooting if something goes wrong. Today, I’m going to walk you through a simple Bash script that uses the AWS CLI to create a backup of your EC2 instance without logging into the AWS Management Console. Whether you're managing one server or scaling up to dozens, this simple script can save you a lot of time and headaches down the road. Index What This Script Does How to Run the EC2 Backup Script Open Your Terminal Create a New Script File Copy and Paste the Script into the File Save and Exit the File Make the Script Executable Run the Script Business Use Case Summary What This Script Does: Sets variables - Defines your instance ID and region Generates a backup name - Creates a timestamped name like Backup-2025-04-14-10-45-30 Creates an AMI - Calls aws ec2 create-image command using the AWS CLI Set --no-reboot flag - Creates an AMI without rebooting the instance Creates a Success message - Prints confirmation message at the end Important Notes: This is a very simple script to demonstrate the possibilities of using bash scripting for backups, so it doesn't do everything: ✅ --no-reboot: Skips rebooting the instance for faster backup. If you want a more consistent backup, remove --no-reboot. ✅ This only backs up the instance and its root volume. If you have additional EBS volumes attached, you’ll want to create snapshots for those too and that's a separate script. ✅ The script doesn't automatically clean up old backups. You can later write another script to delete old AMIs if you want. How to Run the EC2 Backup Script Checklist Before Running the Script: Make sure AWS CLI is installed - Check the version by running aws --version Configure AWS CLI - Configure it by running aws configure (set the access key, secret access key, and region) Make sure your AWS CLI region matches your EC2 instance region 1. Open Your Terminal If you're on Linux or Mac ➔ open Terminal. If you're on Windows ➔ open PowerShell, Command Prompt, or Git Bash (recommended). 2. Create a New Script File Create a .sh Bash script file where you want it. Command: nano backup-ec2.sh ✅ This opens a new empty file for editing. 3. Copy and Paste the Script Into the File Paste this content: #!/bin/bash # Check if an instance ID was provided if [ -z "$1" ]; then echo "Usage: $0 " exit 1 fi # Set your variables INSTANCE_ID="$1" # Get the instance ID from the first argument REGION="us-east-1" #

When you're managing EC2 instances in AWS, backups aren't optional, but a critical part of managing your cloud infrastructure. If you’re updating a server, applying patches, or making big configuration changes, having a fresh backup could save you from hours of troubleshooting if something goes wrong.
Today, I’m going to walk you through a simple Bash script that uses the AWS CLI to create a backup of your EC2 instance without logging into the AWS Management Console. Whether you're managing one server or scaling up to dozens, this simple script can save you a lot of time and headaches down the road.
Index
- What This Script Does
- How to Run the EC2 Backup Script
- Open Your Terminal
- Create a New Script File
- Copy and Paste the Script into the File
- Save and Exit the File
- Make the Script Executable
- Run the Script
- Business Use Case
- Summary
What This Script Does:
- Sets variables - Defines your instance ID and region
- Generates a backup name - Creates a timestamped name like
Backup-2025-04-14-10-45-30
- Creates an AMI - Calls
aws ec2 create-image
command using the AWS CLI - Set
--no-reboot flag
- Creates an AMI without rebooting the instance - Creates a Success message - Prints confirmation message at the end
Important Notes:
This is a very simple script to demonstrate the possibilities of using bash scripting for backups, so it doesn't do everything:
✅ --no-reboot: Skips rebooting the instance for faster backup.
If you want a more consistent backup, remove --no-reboot
.
✅ This only backs up the instance and its root volume.
If you have additional EBS volumes attached, you’ll want to create snapshots for those too and that's a separate script.
✅ The script doesn't automatically clean up old backups. You can later write another script to delete old AMIs if you want.
How to Run the EC2 Backup Script
Checklist Before Running the Script:
- Make sure AWS CLI is installed - Check the version by running
aws --version
- Configure AWS CLI - Configure it by running
aws configure
(set the access key, secret access key, and region) - Make sure your AWS CLI region matches your EC2 instance region
1. Open Your Terminal
If you're on Linux or Mac ➔ open Terminal.
If you're on Windows ➔ open PowerShell, Command Prompt, or Git Bash (recommended).
2. Create a New Script File
Create a .sh
Bash script file where you want it.
Command: nano backup-ec2.sh
✅ This opens a new empty file for editing.
3. Copy and Paste the Script Into the File
Paste this content:
#!/bin/bash
# Check if an instance ID was provided
if [ -z "$1" ]; then
echo "Usage: $0 "
exit 1
fi
# Set your variables
INSTANCE_ID="$1" # Get the instance ID from the first argument
REGION="us-east-1" # <-- Replace with your AWS Region
BACKUP_NAME="Backup-$(date +%F-%H-%M-%S)" # Backup name with timestamp
# Create the AMI
aws ec2 create-image \
--instance-id $INSTANCE_ID \
--name "$BACKUP_NAME" \
--no-reboot \
--region $REGION
# Output success message
echo "Backup started for Instance ID $INSTANCE_ID with AMI name $BACKUP_NAME"
✅ Make sure you replace: REGION with your correct AWS region (like us-west-2, us-east-1)
4. Save and Exit the File
If using vim: Press Esc, type :wq
, and press Enter.
5. Make the Script Executable
You need to give permission to run the script
Command:
chmod +x backup-ec2.sh
Linux Note:
- When the script file, backup-ec2.sh was created, it was not an executable file shown listed as
-rw-rw-r--
. - After running the command,
chmod +x backup-ec2.sh
, the script is now executable indicated by-rwxrwxr-x
and the script file is highlighted in green **to show that it has **execute permissions (x) set for the owner, group and others.
✅ Now the script can be executed.
6. Run the Script and Pass the Instance ID
Now just run it!
Command:
./backup-ec2.sh i-00cb2dd8e85b84fe7
Two things about this script:
- The
$1
on the lineINSTANCE_ID="$1"
allows you to pass the EC2 instance ID as an argument when you run the script so you don't have to add the instance ID to the script - Will generate an Imageid number
- If you don't give an instance ID when running script, it will display a helpful message reminding you the instance ID is missing.
✅ The script will:
Call AWS CLI.
Create an AMI backup of your EC2 instance.
Print a success message:
Backup started **for **Instance ID **i-0123456789abcdef0** **with **AMI name **Backup**-2024-04-14-17-59-23
7. Business Use Case
If you have an EC2 instance with Amazon Linux, Apache, custom applications and configurations when you create an Amazon Machine Image (AMI) all of that is captured. Later, you can launch new servers identical to that setup anywhere in the AWS cloud infrastructure. Most importantly, having an up-to-date Amazon Machine Images (AMIs) ensures you can quickly recover if something goes wrong.
8. Summary
There are more complex scripts you can write for backing up resources, but I just wanted to illustrate the power of scripting using Linux commands. Automation is the name of the game, and cloud engineers are always looking for clean, fast and efficient ways automate to maintain consistency, minimize downtime risks, and streamline their disaster recovery processes.
Connect with me on LinkedIn to comment or share your experiences with using the AWS CLI.
#30DaysLinuxChallenge #RedHatEnterpriseLinux
#CloudWhistler #CloudEngineer #Linux
#DevOps #RedHat #OpenSource
#CloudComputing #WomenInTech