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.


# AWS Settings

# Database parameters

# The path to the folder to be backed up

# Name of the S3 bucket and path to the folder inside the 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

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.