Connect with me
Everyone is aware that whether you are using on-premise or public cloud you need backup of your Virtual Machines or cloud instances.
This script creates the application consistent snapshot of the EBS volume attached with the EC2 Instance by freezing the filesystem like EXT4 and XFS.
The frozen of file system will happen for few seconds and as a precaution the “/” volume will not be attempted to freeze as the critical OS I/O operations will be happening.
So if you are running any application, it is recommended to deploy on non-root volume which is the best practise followed by many companies.
ec2-consistentbackup –description=’My First backup’ –tags=”user=asvignesh;”
Let’s see how the authentication will happen while taking the snapshot
This script actually calls the aws binary which can be installed using the below command
sudo apt install python3-pip && pip3 install –upgrade awscli
As a best practice I don’t recommend the sharing of Access key and secret key to the script, so attach an IAM policy is recommended.
Below is the script and the privilege required.
First create a IAM Policy with the above JSON ( IAM Role.json )
Create a IAM Role with the created policy
After creating the policy, Create a new IAM role for EC2 service with the created Policy
Attach IAM policy to the EC2 Instance
After the creation of Role and Policy, associate the create role and policy with the EC2 instance which you want to create a consistent backup.
- Right Click on the Target EC2 Instance
- Click on Attach/Replace IAM Role
- Choose the IAM Role
Prepare the Guest instance for the backup
ubuntu@ip-172-31-38-177:~$ sudo apt install awscli Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: docutils-common libjbig0 libpaper-utils libpaper1 libtiff5 libwebp6 libwebpdemux2 libwebpmux3 python3-botocore python3-dateutil python3-docutils python3-jmespath python3-olefile python3-pil python3-pygments python3-roman python3-rsa python3-s3transfer sgml-base xml-core Suggested packages: docutils-doc fonts-linuxlibertine | ttf-linux-libertine texlive-lang-french texlive-latex-base texlive-latex-recommended python-pil-doc python3-pil-dbg ttf-bitstream-vera sgml-base-doc debhelper The following NEW packages will be installed: awscli docutils-common libjbig0 libpaper-utils libpaper1 libtiff5 libwebp6 libwebpdemux2 libwebpmux3 python3-botocore python3-dateutil python3-docutils python3-jmespath python3-olefile python3-pil python3-pygments python3-roman python3-rsa python3-s3transfer sgml-base xml-core 0 upgraded, 21 newly installed, 0 to remove and 0 not upgraded. Need to get 4299 kB of archives. After this operation, 39.9 MB of additional disk space will be used. Do you want to continue? [Y/n] Y
Copy the Shell file to the instance and give executable permission to that file.
Then run the command
ubuntu@ip-:~$ ec2-consistentbackup.sh –description=’My First backup’ –tags=”user=asvignesh;”
The command will freeze the file system and create a snapshot of all EBS volume attach with the EC2 Instance with the description and the tags mentioned.
You can schedule the backup to run every 3 hours with this cron job
crontab -e0 */3 * * * ec2-consistentbackup.sh &> /dev/null
Also published on Medium.