mirror of
https://gh.wpcy.net/https://github.com/CaptainCore/captaincore.git
synced 2026-04-23 04:04:38 +08:00
140 lines
No EOL
5.5 KiB
Bash
140 lines
No EOL
5.5 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
#
|
|
# Generates quicksave for plugins and themes changes for one or more sites.
|
|
#
|
|
# `captaincore quicksave generate <site>`
|
|
#
|
|
# [<site>...]
|
|
# One or more sites.
|
|
#
|
|
# [@<target>]
|
|
# Target groups of sites like @all @production or @staging.
|
|
#
|
|
# [--parallel=<number>]
|
|
# Number of Quicksaves at same time
|
|
#
|
|
# [--force]
|
|
# Force even if no changes were made.
|
|
#
|
|
# [--debug]
|
|
# Debug mode
|
|
#
|
|
|
|
if [ ${#@} -ne 1 ]; then
|
|
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify 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
|
|
|
|
# Load site configs
|
|
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"
|
|
exit
|
|
fi
|
|
|
|
rclone_config_file="$path/${site}_${site_id}/rclone.conf"
|
|
if [ ! -f "$rclone_config_file" ]; then
|
|
captaincore site key-generate $site --captain-id=$CAPTAIN_ID
|
|
fi
|
|
|
|
# Lookup rclone
|
|
remote_check=$( rclone config show $environment --config="$rclone_config_file" )
|
|
|
|
if [[ $remote_check == *"Couldn't find type of fs"* ]]; then
|
|
echo "$(date +'%Y-%m-%d %H:%M') Generating rclone configs for $site"
|
|
captaincore site key-generate $site --captain-id=$CAPTAIN_ID
|
|
fi
|
|
|
|
# Captures FTP errors in $ftp_output and file listing to log file
|
|
ftp_output=$( { rclone lsd ${environment}:$home_directory --config="$rclone_config_file" ; } 2>&1 )
|
|
ftp_search_for_wordpress=$( echo "$ftp_output" | perl -wnE'say for /wp-admin/g' )
|
|
|
|
# Handle FTP errors
|
|
if [[ $ftp_search_for_wordpress != "wp-admin"* ]]; then
|
|
echo "Can't locate WordPress for ${site}-${environment}"
|
|
exit
|
|
fi
|
|
|
|
# Append trailing slash if home_directory exist
|
|
if [ "$home_directory" != "" ]; then
|
|
home_directory="${home_directory}/"
|
|
fi
|
|
|
|
quicksave_id_before=$( captaincore quicksave latest $site-$environment --field=hash --captain-id=$CAPTAIN_ID )
|
|
echo "$(date +'%Y-%m-%d %H:%M') Begin quicksave for ${site}-${environment}"
|
|
|
|
site_path="${site}_${site_id}/${environment}"
|
|
|
|
# Updates themes/plugins before Quicksave (Selective backup site locally)
|
|
rclone sync ${environment}:$home_directory${wp_content}/themes/ $path/$site_path/backup/${wp_content}/themes/ --exclude .DS_Store --exclude *timthumb.txt --config="$rclone_config_file"
|
|
rclone sync ${environment}:$home_directory${wp_content}/mu-plugins/ $path/$site_path/backup/${wp_content}/mu-plugins/ --exclude .DS_Store --exclude *timthumb.txt --config="$rclone_config_file"
|
|
rclone sync ${environment}:$home_directory${wp_content}/plugins/ $path/$site_path/backup/${wp_content}/plugins/ --exclude .DS_Store --exclude *timthumb.txt --config="$rclone_config_file"
|
|
|
|
# Sync to git repo
|
|
mkdir -p $path/$site_path/quicksave/mu-plugins
|
|
mkdir -p $path/$site_path/quicksave/plugins
|
|
mkdir -p $path/$site_path/quicksave/themes
|
|
mkdir -p $path/$site_path/quicksave/versions
|
|
|
|
cd $path/$site_path/quicksave/
|
|
|
|
# Create new git repo if needed
|
|
if [ ! -d ".git" ]; then
|
|
git init
|
|
fi
|
|
|
|
rsync -a --delete --exclude=.git --exclude="log-*.txt" --exclude="*.log.txt" --exclude="*.zip" --exclude=cache.json --exclude="*.log" --exclude="*.log.txt" $path/$site_path/backup/${wp_content}/mu-plugins/ $path/$site_path/quicksave/mu-plugins
|
|
rsync -a --delete --exclude=.git --exclude="log-*.txt" --exclude="*.log.txt" --exclude="*.zip" --exclude=cache.json --exclude="*.log" --exclude="*.log.txt" $path/$site_path/backup/${wp_content}/plugins/ $path/$site_path/quicksave/plugins
|
|
rsync -a --delete --exclude=.git --exclude="log-*.txt" --exclude="*.log.txt" --exclude="*.zip" --exclude=cache.json --exclude="*.log" --exclude="*.log.txt" $path/$site_path/backup/${wp_content}/themes/ $path/$site_path/quicksave/themes
|
|
|
|
cd ${CAPTAINCORE_PATH}/data
|
|
|
|
wp eval-file ../lib/local-scripts/sync-data.php $site-${environment}
|
|
wp eval-file ../lib/local-scripts/quicksave-add.php site=$site environment=$environment
|
|
|
|
# Remove git lock if found
|
|
if [ -f "$path/$site_path/quicksave/.git/index.lock" ]; then
|
|
rm "$path/$site_path/quicksave/.git/index.lock"
|
|
fi
|
|
|
|
captaincore quicksave list-generate $site-${environment} --captain-id=$CAPTAIN_ID
|
|
quicksave_id_after=$( captaincore quicksave latest $site-$environment --field=hash --captain-id=$CAPTAIN_ID )
|
|
|
|
if [[ "$quicksave_id_before" != "$quicksave_id_after" ]]; then
|
|
cd $path/$site_path/quicksave/
|
|
echo "$(date +'%Y-%m-%d %H:%M') Backing up /quicksave/ to restic repo"
|
|
if [[ $( restic snapshots --repo rclone:$rclone_backup/${site}_${site_id}/${environment}/quicksave-repo --password-file="${CAPTAINCORE_PATH}/data/restic.key" ) == "" ]]; then
|
|
echo "Generating new restic repo ${site}_${site_id}/${environment}/quicksave-repo"
|
|
restic init --quiet --repo rclone:$rclone_backup/${site}_${site_id}/${environment}/quicksave-repo --password-file="${CAPTAINCORE_PATH}/data/restic.key"
|
|
fi
|
|
restic backup . --quiet --repo rclone:$rclone_backup/${site}_${site_id}/${environment}/quicksave-repo --exclude-file="${CAPTAINCORE_PATH}/lib/restic-excludes" --password-file="${CAPTAINCORE_PATH}/data/restic.key"
|
|
fi
|
|
|
|
if [ -f "${path}/process-${process_id}-progress.log" ]; then
|
|
echo -n "." >> ${path}/process-${process_id}-progress.log
|
|
fi
|
|
|
|
}
|
|
|
|
run_command |