Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shell script that performs a complete backup and keeps the files locally and also in AWS S3
#!/bin/sh
#
# Setup: crontab -e
#
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
# * * * * * command --arg1 --arg2 file1 file2 2>&1
#
# 0 1 * * * ./backup-project-x.sh
#
RESET='\e[0m'
RED='\e[31m'
GREEN='\e[32m'
YELLOW='\e[33m'
#----------------------------------------------------------------------------------------------------------------------
# DEFINITIONS
#----------------------------------------------------------------------------------------------------------------------
# Application name
project_name="ProjectX"
# Application path
project_path="/var/www/ProjectX"
# Empty string "" for standalone applications
project_database="db_projectx"
# Where local snapshots will be stored
target="/media/BACKUP/ProjectX"
# AWS S3 bucket name
aws_bucket="s3://privated-bucket-name/"
#----------------------------------------------------------------------------------------------------------------------
# VARIABLES
#----------------------------------------------------------------------------------------------------------------------
date=$(date +"%Y%m%d%H%M%S")
hostname=$(hostname -s)
archive_file_application="$hostname-application-$project_name-$date.tgz"
if [ -z "$project_database" ]
then
archive_file_database=""
else
archive_file_database="$hostname-mysql-$project_database-$date.sql.gz"
fi
#----------------------------------------------------------------------------------------------------------------------
# CHECKPOINT: MAKE SURE TARGET FOLDER EXISTS
#----------------------------------------------------------------------------------------------------------------------
mkdir -p $target
#----------------------------------------------------------------------------------------------------------------------
# CLEAN UP: REMOVE OLD SNAPSHOTS
#----------------------------------------------------------------------------------------------------------------------
echo "┌─────────────────────────────────────────────────────────────────────────────┐"
echo "│ REMOVING OLDER SNAPSHOTS FROM LOCAL FOLDER │"
echo "└─────────────────────────────────────────────────────────────────────────────┘"
echo ""
echo " ❗ Removing files from [ ${YELLOW}$target${RESET} ]"
find $target/ -mtime +14 -exec rm {} \;
echo ""
echo " 🟢 Task done!"
echo ""
#----------------------------------------------------------------------------------------------------------------------
# BACKUP: APPLICATION
#----------------------------------------------------------------------------------------------------------------------
echo "┌─────────────────────────────────────────────────────────────────────────────┐"
echo "│ LOCALLY BACKUP THE APPLICATION │"
echo "└─────────────────────────────────────────────────────────────────────────────┘"
echo ""
echo " 📦 Backing up [ ${YELLOW}$project_path${RESET} ] to [ ${YELLOW}$target/$archive_file_application${RESET} ]"
tar czf $target/$archive_file_application $project_path 2>/dev/null
echo ""
echo " 🟢 Task done!"
echo ""
#----------------------------------------------------------------------------------------------------------------------
# BACKUP: DATABASE
#----------------------------------------------------------------------------------------------------------------------
echo "┌─────────────────────────────────────────────────────────────────────────────┐"
echo "│ LOCALLY BACKUP THE DATABASE │"
echo "└─────────────────────────────────────────────────────────────────────────────┘"
echo ""
if [ -z "$project_database" ]
then
echo " ⚠️ Current project does not have any database to backup!"
else
echo " 📦 Backing up [ ${YELLOW}$project_database${RESET} ] to [ ${YELLOW}$target/$archive_file_database${RESET} ]"
mysqldump --login-path=local --triggers --routines $project_database | gzip -9 -c > $target/$archive_file_database
fi
echo ""
echo " 🟢 Task done!"
echo ""
#----------------------------------------------------------------------------------------------------------------------
# UPLOAD TO AWS S3
#----------------------------------------------------------------------------------------------------------------------
echo "┌─────────────────────────────────────────────────────────────────────────────┐"
echo "│ UPLOAD BACKUPS TO AWS S3 │"
echo "└─────────────────────────────────────────────────────────────────────────────┘"
echo ""
echo " 🚀 Uploading [ ${YELLOW}$target/$archive_file_application${RESET} ] to [ ${YELLOW}AWS S3${RESET} ]"
aws s3 cp --quiet $target/$archive_file_application $aws_bucket
if [ -z "$project_database" ]
then
echo " ⚠️ Current project does not have any database to backup!"
else
echo " 🚀 Uploading [ ${YELLOW}$target/$archive_file_database${RESET} ] to [ ${YELLOW}AWS S3${RESET} ]"
aws s3 cp --quiet $target/$archive_file_database $aws_bucket
fi
echo ""
echo " 🟢 Task done!"
echo ""
#----------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment