mirror of
https://gh.wpcy.net/https://github.com/CaptainCore/captaincore.git
synced 2026-04-25 13:40:34 +08:00
132 lines
4.8 KiB
Bash
Executable file
132 lines
4.8 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
#
|
|
# Rollback theme, plugin or file from a Quicksave on a site.
|
|
#
|
|
# `captaincore rollback <site> <commit>`
|
|
#
|
|
# [--plugin=<plugin>]
|
|
#
|
|
# [--theme=<theme>]
|
|
#
|
|
# [--file=<file>]
|
|
#
|
|
# [--all]
|
|
#
|
|
|
|
if [ ${#@} -ne 2 ]; then
|
|
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify a <site> and <commit>."
|
|
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
|
|
commit=$2
|
|
|
|
run_command() {
|
|
|
|
# Requires <site> and <commit>
|
|
if [[ $site == "" ]] || [[ $commit == "" ]]; then
|
|
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify a site and a commit."
|
|
return 1
|
|
fi
|
|
|
|
# Requires <site> and <commit>
|
|
if [[ $FLAG_PLUGIN == "" ]] && [[ $FLAG_THEME == "" ]] && [[ $FLAG_ALL == "" ]] && [[ $FLAG_FILE == "" ]]; then
|
|
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify a plugin, theme, file or --all."
|
|
return 1
|
|
fi
|
|
|
|
# Extract environment
|
|
if [[ "$site" == *"-staging"* ]]; then
|
|
environment=staging
|
|
else
|
|
environment=production
|
|
fi
|
|
|
|
# Load site info
|
|
IFS=$'\n'$'\r'; for line in $(captaincore site get $site --bash --captain-id=$CAPTAIN_ID); do declare "$line"; done
|
|
|
|
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
|
|
|
|
# Append trailing slash if home_directory exist
|
|
if [ "$home_directory" != "" ]; then
|
|
home_directory="${home_directory}/"
|
|
fi
|
|
|
|
if [[ "$FLAG_VERSION" == "previous" ]]; then
|
|
cd $path/${site}_${site_id}/${environment}/quicksave/
|
|
commit=$( git log -n 1 $commit --pretty=format:"%P" )
|
|
fi
|
|
|
|
if [[ "$FLAG_PLUGIN" != "" ]]; then
|
|
|
|
echo "Rolling back plugin $FLAG_PLUGIN"
|
|
cd $path/${site}_${site_id}/${environment}/quicksave/
|
|
mkdir -p ~/Tmp/restore/$commit/
|
|
git archive --format=zip $commit:plugins/$FLAG_PLUGIN/ > ~/Tmp/restore/$commit/$FLAG_PLUGIN.zip
|
|
cat ~/Tmp/restore/$commit/$FLAG_PLUGIN.zip | captaincore ssh ${site}-${environment} --command="cat > $FLAG_PLUGIN.zip" --captain-id=$CAPTAIN_ID
|
|
captaincore ssh ${site}-${environment} --command="wp plugin install $FLAG_PLUGIN.zip --force --skip-plugins --skip-themes; rm $FLAG_PLUGIN.zip" --captain-id=$CAPTAIN_ID
|
|
rm -rf ~/Tmp/restore/$commit/
|
|
|
|
fi
|
|
|
|
if [[ "$FLAG_THEME" != "" ]]; then
|
|
|
|
echo "Rolling back theme $FLAG_THEME"
|
|
cd $path/${site}_${site_id}/${environment}/quicksave/
|
|
mkdir -p ~/Tmp/restore/$commit/
|
|
git archive --format=zip $commit:themes/$FLAG_THEME/ > ~/Tmp/restore/$commit/$FLAG_THEME.zip
|
|
cat ~/Tmp/restore/$commit/$FLAG_THEME.zip | captaincore ssh ${site}-${environment} --command="cat > $FLAG_THEME.zip" --captain-id=$CAPTAIN_ID
|
|
captaincore ssh ${site}-${environment} --command="wp theme install $FLAG_THEME.zip --force --skip-plugins --skip-themes; rm $FLAG_THEME.zip" --captain-id=$CAPTAIN_ID
|
|
rm -rf ~/Tmp/restore/$commit/
|
|
|
|
fi
|
|
|
|
if [[ "$FLAG_FILE" != "" ]]; then
|
|
|
|
file_name=${FLAG_FILE##*/}
|
|
file_path=${FLAG_FILE//$file_name/}
|
|
echo "Rolling back file '$FLAG_FILE'"
|
|
cd $path/${site}_${site_id}/${environment}/quicksave/
|
|
mkdir -p ~/Tmp/restore/$commit/$file_path
|
|
git show $commit:$FLAG_FILE > ~/Tmp/restore/$commit/$FLAG_FILE
|
|
if [[ "$home_directory" != "" ]]; then
|
|
home_directory="${home_directory}/"
|
|
fi
|
|
rclone copyto ~/Tmp/restore/$commit/$FLAG_FILE ${environment}:$home_directory${wp_content}/$FLAG_FILE --config="$rclone_config_file"
|
|
rm -rf ~/Tmp/restore/$commit/
|
|
|
|
fi
|
|
|
|
if [[ "$FLAG_ALL" == "true" ]]; then
|
|
|
|
echo "Rolling back entire quicksave"
|
|
cd $path/${site}_${site_id}/${environment}/quicksave/
|
|
mkdir -p ~/Tmp/restore/$commit/
|
|
git archive --format=zip $commit: > ~/Tmp/restore/$commit/quicksave_${commit}_all.zip
|
|
cat ~/Tmp/restore/$commit/quicksave_${commit}_all.zip | captaincore ssh ${site}-${environment} --command="cat > quicksave_${commit}_all.zip" --captain-id=$CAPTAIN_ID
|
|
captaincore ssh ${site}-${environment} --command="wp maintenance-mode activate; echo 'Restoring ${wp_content}/themes/'; rm -rf ${wp_content}/themes/; unzip -q -o quicksave_${commit}_all.zip 'themes/*' -d ${wp_content}/; echo 'Restoring ${wp_content}/plugins/'; rm -rf ${wp_content}/plugins/; unzip -q -o quicksave_${commit}_all.zip 'plugins/*' -d ${wp_content}/; echo 'Restoring ${wp_content}/mu-plugins/'; rm -rf ${wp_content}/mu-plugins/; unzip -q -o quicksave_${commit}_all.zip 'mu-plugins/*' -d ${wp_content}/; wp maintenance-mode deactivate" --captain-id=$CAPTAIN_ID
|
|
rm -rf ~/Tmp/restore/$commit/
|
|
|
|
fi
|
|
|
|
}
|
|
run_command
|