Shell script to backup a directory in CentOS to AWS bucket called backups

Abhilash S
intertoons
Published in
1 min readFeb 2, 2023

--

This script first checks if the AWS CLI is installed, and if not, installs it using yum. It then uses the aws s3 sync command to backup the directory to the S3 bucket. Finally, it checks the exit code of the previous command to ensure it was successful and outputs a message indicating the status of the backup.

Photo by Gabriel Heinzer on Unsplash

Here is a sample shell script that backs up a directory in CentOS to an AWS S3 bucket:

#!/bin/bash

# Specify the directory to backup
backup_dir="/path/to/directory"

# Specify the S3 bucket name
bucket_name="backups"

# Install the AWS CLI if not already installed
if ! [ -x "$(command -v aws)" ]; then
yum install aws-cli -y
fi

# Sync the directory to the S3 bucket
aws s3 sync "$backup_dir" "s3://$bucket_name"

# Check the exit code of the previous command to ensure it was successful
if [ $? -eq 0 ]; then
echo "Backup of directory $backup_dir to S3 bucket $bucket_name completed successfully."
else
echo "Backup of directory $backup_dir to S3 bucket $bucket_name failed."
fi

read how to install aws from our blog

--

--

Abhilash S
intertoons

Solutions Architect @ Intertoons , Over 16 years of Collective experiences In Windows , Linux Developments , Redhat, Google Certified Professional