mirror of
https://gh.wpcy.net/https://github.com/CaptainCore/captaincore.git
synced 2026-04-23 05:52:46 +08:00
190 lines
No EOL
6.8 KiB
Bash
190 lines
No EOL
6.8 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
#
|
|
# Snapshots one or more sites.
|
|
#
|
|
# `captaincore snapshot`
|
|
#
|
|
# [<site>...]
|
|
# One or more sites.
|
|
#
|
|
# [@<target>]
|
|
# Target groups of sites like @all @production or @staging.
|
|
#
|
|
# [--email=<email>]
|
|
# On completion, send email to
|
|
#
|
|
# [--filter=<filter-option>]
|
|
# Filter options include one or more of the following: database, themes, plugins, uploads, everything-else. Example --filter=database,themes,plugins will generate a zip with only the database, themes and plugins. Without filter a snapshot will include everything.
|
|
#
|
|
# [--skip-backup]
|
|
# Skips backup and makes snapshot based on local backup
|
|
#
|
|
# [--skip-remote]
|
|
# Skips sending to rclone remote
|
|
#
|
|
# [--delete-after-snapshot]
|
|
# Deletes local and remote copies of site
|
|
#
|
|
# [--notes=<notes>]
|
|
# Add description for the snapshot
|
|
#
|
|
# [--user_id=<user_id>]
|
|
# Tracks who made the snapshot
|
|
#
|
|
|
|
if [ ${#@} -ne 1 ]; then
|
|
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Requires a <site>"
|
|
exit
|
|
fi
|
|
|
|
while read config; do
|
|
if [[ "$config" == "Error:"* ]]; then
|
|
continue
|
|
fi
|
|
declare "$config"
|
|
done <<< "$(php ${CAPTAINCORE_PATH}/lib/local-scripts/configs.php fetch)"
|
|
|
|
site=$1
|
|
|
|
run_command() {
|
|
|
|
# Extract environment
|
|
if [[ "$site" == *"-staging"* ]]; then
|
|
environment=staging
|
|
else
|
|
environment=production
|
|
fi
|
|
|
|
while read site_configs; do declare "$site_configs"; done <<< "$(captaincore site get $site --bash --captain-id=$CAPTAIN_ID)"
|
|
|
|
# Return error if domain not found
|
|
if [[ "$domain" == "" ]] || [[ "$site" == "" ]]; then
|
|
echo "Can't locate website for site $site"
|
|
return 1
|
|
fi
|
|
|
|
if [[ "$site" == *"-staging" ]]; then
|
|
site=${site//-staging/}
|
|
use_staging=true
|
|
fi
|
|
|
|
# if [[ "$skip_backup" != "true" ]]; then
|
|
# Generate fresh backup
|
|
# captaincore backup generate $site --skip-remote --captain-id=$CAPTAIN_ID
|
|
# fi
|
|
|
|
# Generates snapshot archive
|
|
timedate="$(date +'%Y-%m-%d-%H-%M-%S')-$( php -r 'echo bin2hex(openssl_random_pseudo_bytes( 4 ));' )"
|
|
snapshot_path="$site-$environment-$timedate"
|
|
snapshot_name="$site-$environment-$timedate.zip"
|
|
cd $path/${site}_${site_id}/
|
|
|
|
if [[ $DELETE_AFTER_SNAPSHOT == true ]]; then
|
|
cd $path/
|
|
if [ ! -d "${site}_${site_id}/" ]; then
|
|
echo "Skipping snapshot, can't find $path/${site}_${site_id}/"
|
|
exit
|
|
fi
|
|
zip --test -q -r $snapshot_name ${site}_${site_id}/ && echo "Completed generating $snapshot_name" || echo "Failed generating $snapshot_name"
|
|
fi
|
|
|
|
if [[ $DELETE_AFTER_SNAPSHOT != true ]]; then
|
|
ln -s $environment/backup/ $snapshot_path # create directory shortcut for naming purposes
|
|
|
|
if [[ "$FLAG_FILTER" != "" ]]; then
|
|
|
|
# Turn filter in array
|
|
filters=($( echo $FLAG_FILTER | tr , " "))
|
|
|
|
for option in ${filters[@]}; do
|
|
|
|
if [[ $option == "database" ]]; then
|
|
zip -r $snapshot_name $snapshot_path/database-backup.sql
|
|
fi
|
|
|
|
if [[ $option == "themes" ]]; then
|
|
zip --test -q -r $snapshot_name $snapshot_path/wp-content/themes/ && echo "Completed adding /wp-content/themes/ to $snapshot_name" || echo "Failed generating $snapshot_name"
|
|
fi
|
|
|
|
if [[ $option == "plugins" ]]; then
|
|
zip --test -q -r $snapshot_name $snapshot_path/wp-content/plugins/ && echo "Completed adding /wp-content/plugins/ to $snapshot_name" || echo "Failed generating $snapshot_name"
|
|
fi
|
|
|
|
if [[ $option == "uploads" ]]; then
|
|
zip --test -q -r $snapshot_name $snapshot_path/wp-content/uploads/ && echo "Completed adding /wp-content/uploads/ to $snapshot_name" || echo "Failed generating $snapshot_name"
|
|
fi
|
|
|
|
if [[ $option == "everything-else" ]]; then
|
|
zip --test -q -r $snapshot_name $snapshot_path/ --exclude=$snapshot_path/database-backup.sql --exclude=$snapshot_path/wp-content/themes/\* --exclude=$snapshot_path/wp-content/plugins/\* --exclude=$snapshot_path/wp-content/uploads/\* --exclude=$snapshot_path/_wpeprivate/\* --exclude=\*.git\* --exclude=$snapshot_path/wp-content/advanced-cache.php --exclude=$snapshot_path/wp-content/mu-plugins/mu-plugin.php --exclude=$snapshot_path/wp-content/mu-plugins/kinsta-mu-plugins.php --exclude=$snapshot_path/wp-content/mu-plugins/kinsta-mu-plugins/\* --exclude=$snapshot_path/wp-content/mu-plugins/slt-force-strong-passwords.php --exclude=$snapshot_path/wp-content/mu-plugins/force-strong-passwords/\* --exclude=$snapshot_path/wp-content/mu-plugins/wpengine-common/\* && echo "Completed adding everything else to $snapshot_name" || echo "Failed generating $snapshot_name"
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
if [[ "$FLAG_FILTER" == "" ]]; then
|
|
# Add everything to ZIP
|
|
zip --test -q -r $snapshot_name $snapshot_path/ --exclude=$snapshot_path/_wpeprivate/\* --exclude=\*.git\* --exclude=$snapshot_path/wp-content/advanced-cache.php --exclude=$snapshot_path/wp-content/mu-plugins/mu-plugin.php --exclude=$snapshot_path/wp-content/mu-plugins/kinsta-mu-plugins.php --exclude=$snapshot_path/wp-content/mu-plugins/kinsta-mu-plugins/\* --exclude=$snapshot_path/wp-content/mu-plugins/slt-force-strong-passwords.php --exclude=$snapshot_path/wp-content/mu-plugins/force-strong-passwords/\* --exclude=$snapshot_path/wp-content/mu-plugins/wpengine-common/\*
|
|
fi
|
|
|
|
rm $snapshot_path # remove directory shortcut
|
|
mkdir -p ${environment}/snapshots
|
|
mv $snapshot_name ${environment}/snapshots
|
|
cd ${environment}/snapshots
|
|
fi
|
|
|
|
# Grab snapshot size in bytes
|
|
if [[ "$OSTYPE" == "linux-gnu" ]]; then
|
|
# Begin folder size in bytes without apparent-size flag
|
|
snapshot_size=`du -s --block-size=1 $snapshot_name`
|
|
snapshot_size=`echo $snapshot_size | cut -d' ' -f 1`
|
|
|
|
elif [[ "$OSTYPE" == "darwin"* ]]; then
|
|
# Calculate folder size in bytes http://superuser.com/questions/22460/how-do-i-get-the-size-of-a-linux-or-mac-os-x-directory-from-the-command-line
|
|
snapshot_size=`find $snapshot_name -type f -print0 | xargs -0 stat -f%z`
|
|
fi
|
|
|
|
if [[ $SKIP_REMOTE != true ]]; then
|
|
|
|
# Moves snapshot to Backblaze archive folder
|
|
rclone move $snapshot_name $rclone_snapshot/
|
|
|
|
if [[ "$debug" == "true" ]]; then
|
|
read -r -d '' VAR << EOM
|
|
{
|
|
"command":"snapshot",
|
|
"user_id":"$FLAG_USER_ID",
|
|
"site_id":"$site_id",
|
|
"environment":"$environment",
|
|
"storage":"$snapshot_size",
|
|
"archive":"${site}-${environment}-${timedate}.zip",
|
|
"email":"$FLAG_EMAIL",
|
|
"notes":"$FLAG_NOTES",
|
|
"token":"$token"
|
|
}
|
|
EOM
|
|
echo $VAR
|
|
continue
|
|
fi
|
|
|
|
cd ${CAPTAINCORE_PATH}/data
|
|
wp eval-file ../lib/local-scripts/snapshot-add.php site_id=$site_id environment=$environment user_id=$FLAG_USER_ID storage=$snapshot_size archive="${site}-${environment}-${timedate}.zip" email=$FLAG_EMAIL notes="$FLAG_NOTES"
|
|
|
|
fi
|
|
|
|
if [[ $DELETE_AFTER_SNAPSHOT == true ]]; then
|
|
|
|
echo "Removing files after snapshot"
|
|
|
|
# Removes directory from backup server
|
|
rm -rf $path/${site}_${site_id}/
|
|
|
|
# Remove from remote storage
|
|
rclone purge --fast-list $rclone_backup/${site}_${site_id}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
run_command |