Jump to content

Bash script for Backups and sending to AWS S3


Recommended Posts

it's example of script to do backups and sending to aws s3.

#!/bin/bash

# AWS Settings
AWS_ACCESS_KEY_ID="your_access_key"
AWS_SECRET_ACCESS_KEY="your_secret_access_key"
#AWS_DEFAULT_REGION="your_aws_region"

# Database parameters
DB_NAME="name"
DB_USER="user"
DB_PASSWORD="pass"
DB_HOST="127.0.0.1"
DB_PORT="3306"

# The path to the folder to be backed up
SOURCE_DIR="/your/path"

# Name of the S3 bucket and path to the folder inside the bucket
S3_BUCKET="name_of_yours_s3_bucket"
S3_FOLDER="name_of_yours_s3_folder_in_bucket"

# Create a backup folder with the current date and time
BACKUP_FOLDER="$(date +'%Y-%m-%d_%H-%M-%S') (FULL)"
mkdir -p "/tmp/$BACKUP_FOLDER"

# Archive the backup folder
cd "$(dirname "$SOURCE_DIR")" || exit
tar -zcvf "/tmp/$BACKUP_FOLDER/$(basename "$SOURCE_DIR").tar.gz" "$(basename "$SOURCE_DIR")"

# Dump the database
mysqldump -u"$DB_USER" -p"$DB_PASSWORD" -h "$DB_HOST" -P "$DB_PORT" "$DB_NAME" > "/tmp/$BACKUP_FOLDER/$DB_NAME.sql"

# Copy archive and database dump to S3
aws s3 cp "/tmp/$BACKUP_FOLDER/$(basename "$SOURCE_DIR").tar.gz" "s3://$S3_BUCKET/$S3_FOLDER/$BACKUP_FOLDER/"
aws s3 cp "/tmp/$BACKUP_FOLDER/$DB_NAME.sql" "s3://$S3_BUCKET/$S3_FOLDER/$BACKUP_FOLDER/"

# Clear the temporary folder
rm -rf "/tmp/$BACKUP_FOLDER"

you can change it.
for example i need to exclude some dir in my path

cd "$(dirname "$SOURCE_DIR")" || exit
tar -zcvf "/tmp/$BACKUP_FOLDER/$(basename "$SOURCE_DIR").tar.gz" --exclude='wp-content/uploads' --exclude='wp-content/backups-dup-lite' "$(basename "$SOURCE_DIR")"
Link to comment
Share on other sites

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
  • Create New...

Important Information

By using this site you automatically agree to the Privacy Policy | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.