Skip to content

Instantly share code, notes, and snippets.

@csaborio001
Created July 21, 2019 09:33
Show Gist options
  • Save csaborio001/0aa19a27f4d2317ab9f6cfe0bf71c8ba to your computer and use it in GitHub Desktop.
Save csaborio001/0aa19a27f4d2317ab9f6cfe0bf71c8ba to your computer and use it in GitHub Desktop.
#!/bin/bash
# Useful Extensions for Visual Studio
# https://marketplace.visualstudio.com/items?itemName=rogalmic.bash-debug
# https://marketplace.visualstudio.com/items?itemName=timonwong.shellcheck
# THESE VALUES ARE THE ONLY ONES YOU SHOULD CHANGE
WWW_DIR="/Users/csaborio/WebSites2"
BACKUP_DIR="/Users/csaborio/backups"
DB_USER="root"
DB_PASS="root"
# DO NOT EDIT ANYTHING BELOW THIS LINE
# SCRIPT FUNCTIONS
function is_full_backup() {
DAY_OF_THE_WEEK=$(date +"%u");
# Only do full backups on Tuesday, Thursday, Saturday
if [ $(($DAY_OF_THE_WEEK % 2)) == 0 ]; then
return 1;
fi
}
function report_command_output() {
TIME_NOW=$(date +"%Y-%m-%d-%H%M")
if [ $1 -ne 0 ]; then
echo "$TIME_NOW - ERROR in operation : $2";
echo "Exiting loop"; break;
return 1;
else
echo "$TIME_NOW - OK in operation : $2";
return 0;
fi
}
# Check to see if the full backup switch is specified
if [ -n "$1" ] # If there is something in the first parameter
then
if [ $1 = '-forced' ] # If parameter's name is plugins
then
backup_type='forced';
fi
fi
# If there is something in the second parameter
if [ -n "$2" ]
then
specified_domain_name=("$2")
fi
# Check to see if the forced backup is only for one domain
# CONSTANTS, DO NOT CHANGE
NOW=$(date +"%Y-%m-%d-%H%M")
YEAR=$(date +"%Y");
MONTH=$(date +"%m");
# Figure out if we are backing up all domains or if a domain was specified as an argument.
if [ $specified_domain_name ]; then
echo "Domain was specified, only one domain will be backed up: $specified_domain_name";
directory_array=($(find $WWW_DIR -type d -maxdepth 1 -name $specified_domain_name));
report_command_output $? "Getting the specified domain name $specified_domain_name"
else
# Get all directories inside the WWW directory
echo "Backing up all WordPress installations found in $WWW_DIR"
directory_array=($(find $WWW_DIR -type d -maxdepth 1))
report_command_output $? "Getting the list of all domains inside $WWW_DIR"
fi
# Loop thru each one of the directories.
for website_root in "${directory_array[@]}";
do (
# Is there a _content directory? (to see if it's a WordPress install)
if [ -d "$website_root/_content" ]; then
# Obtain the domain name from the full path
DOMAIN=$(basename $website_root)
report_command_output $? "Obtaining domain name from the full path using basename."
echo "Backing up $DOMAIN"
DB_NAME=$DOMAIN
PLUGIN_DIR="$website_root/_content/plugins"
UPLOADS_DIR="$website_root/_content/uploads"
THEMES_DIR="$website_root/_content/themes"
WP_CONFIG="$website_root/wp-config.php"
# Output Options
FINAL_BACKUP_FILE_PATH="$BACKUP_DIR/$DOMAIN/$YEAR/$MONTH"
DB_FILE="$DOMAIN.$NOW.sql"
TAR_FILE="$DOMAIN.$NOW.tar"
# Try and create output directory
mkdir -p $FINAL_BACKUP_FILE_PATH
report_command_output $? "Creating output backup directory $FINAL_BACKUP_FILE_PATH"
# Dump DB File
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_FILE
report_command_output $? "Dumping database file $DB_FILE"
# Encrypt mysqldump
gpg -r christian@saborio.org --encrypt-files $BACKUP_DIR/$DB_FILE
report_command_output $? "Encrypting Datase dump."
# Function call to see if we're doing a full backup or just a DB dump.
is_full_backup
# This is how we check the return value of the function in BASH ¯\_(ツ)_/¯
if [ $? == 1 ] || [ "$backup_type" == 'forced' ]; then
echo "Performing full $backup_type backup."
tar -cf $FINAL_BACKUP_FILE_PATH/$TAR_FILE $WP_CONFIG $PLUGIN_DIR $UPLOADS_DIR $THEMES_DIR $BACKUP_DIR/$DB_FILE.gpg
report_command_output $? "Creating the tar archive $FINAL_BACKUP_FILE_PATH/$TAR_FILE"
else # Just include the DB file in the tar archive.
echo "Backing only the database"
tar -cf $FINAL_BACKUP_FILE_PATH/$TAR_FILE $BACKUP_DIR/$DB_FILE.gpg
report_command_output $? "Creating the tar archive $FINAL_BACKUP_FILE_PATH/$TAR_FILE"
fi
# Creating Gzip file
gzip -9 $FINAL_BACKUP_FILE_PATH/$TAR_FILE
report_command_output $? "Creating the zip file $FINAL_BACKUP_FILE_PATH/$TAR_FILE.gzip"
# Clean Up
echo "Cleaning Up."
rm $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE.gpg
fi
);
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment