Last active
July 28, 2023 12:07
-
-
Save zzf01/113ed28b69a537ef11472d98bfae227c to your computer and use it in GitHub Desktop.
Backup / Restore Shopware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| test -z "$1" && echo You have to define the PHP interpreter as first argument && exit 1 | |
| if [ ! -f "$1" ]; then | |
| if [ ! -f "$(which $1)" ]; then | |
| echo The PHP interpreter can not be found | |
| exit 1 | |
| else | |
| PHP=$(which $1) | |
| fi | |
| else | |
| PHP=$(which $1) | |
| fi | |
| test $(find -type f -path "*/bin/console" | grep -v "save" | wc -l) -gt 1 && echo Multiple instances found && exit 1 | |
| CONSOLE=$(find -type f -path "*/bin/console" | grep -v "save") | |
| test -z $CONSOLE && echo "Can not find bin/console" && exit 1 | |
| test ! -f $CONSOLE && echo "$CONSOLE is not a regular file" && exit 1 | |
| COMMAND="$PHP $CONSOLE" | |
| BASE=${CONSOLE%"/bin/console"} | |
| if [ -f $BASE/.env.local ]; then | |
| SWENV="$BASE/.env.local" | |
| elif [ -f $BASE/.env ]; then | |
| SWENV="$BASE/.env" | |
| else | |
| echo No confifuration file found | |
| exit 1 | |
| fi | |
| DBLINE=$(cat $BASE/.env | grep DATABASE_URL) | |
| DBUSER=$(echo $DBLINE | sed "s?.*//\([^:]*\):.*?\1?") | |
| DBPASS=$(echo $DBLINE | sed "s?.*//.*:\(.*\)@.*?\1?") | |
| DB=$(echo $DBLINE | sed "s?.*/\(.*\)\".*?\1?") | |
| echo Using configuration: | |
| echo ==================== | |
| echo Shopware installation path: $BASE | |
| echo Console command: $COMMAND | |
| echo DB: $DB | |
| echo DB user: $DBUSER | |
| echo DB password: $DBPASS | |
| read -n 1 -s -p "Shall I proceed? [y/n]: " action | |
| test "$action" != "y" && echo "Exiting." && exit 1 | |
| echo | |
| disablePlugins() | |
| { | |
| echo | |
| echo PLUGIN STATE BEFORE ACTION >disablePlugins.log | |
| echo ========================== >>disablePlugins.log | |
| $COMMAND plugin:list >>disablePlugins.log | |
| for f in $($COMMAND plugin:list | grep "Yes" | cut -d " " -f3); do | |
| echo Deactivating $f | |
| $COMMAND plugin:deactivate $f &>/dev/null | |
| done | |
| echo Clearing cache | |
| $COMMAND cache:clear &>/dev/null | |
| echo -e "\n\nPLUGIN STATE AFTER ACTION" >>disablePlugins.log | |
| echo ========================= >>disablePlugins.log | |
| $COMMAND plugin:list >>disablePlugins.log | |
| $COMMAND plugin:list | |
| } | |
| backup() | |
| { | |
| echo -e "\nDumping database [$DB]" | |
| mysqldump -u $DBUSER -p$DBPASS $DB >$DB.sql | |
| if [ $? -ne 0 ]; then | |
| echo "DB dump did not succeed, exiting. (mysqldump -u $DBUSER -p$DBPASS $DB >$DB.sql)" | |
| rm -f $DB.sql | |
| exit 1 | |
| fi | |
| echo "Backing up folder [$BASE]" | |
| rsync -a $BASE/ $BASE.save | |
| read -n 1 -s -p "Shall I disable all plugins for you? [y/n]: " action | |
| case $action | |
| in | |
| y) disablePlugins ;; | |
| *) echo "We are done." | |
| exit ;; | |
| esac | |
| } | |
| restore() | |
| { | |
| test ! -d "$BASE.save" && echo "Backup folder [$BASE.save] not found" && exit 1 | |
| test ! -f $DB.sql && echo "Database [$DB] not found" && exit 1 | |
| echo | |
| echo Starting restore | |
| mysql -u $DBUSER -p$DBPASS $DB < $DB.sql | |
| if [ $? -ne 0 ]; then | |
| echo "DB restore did not succeed, exiting. (mysql -u $DBUSER -p$DBPASS $DB < $DB.sql)" | |
| exit 1 | |
| fi | |
| mkdir -p restore.tmp | |
| mv $DB.sql restore.tmp | |
| mv $BASE restore.tmp | |
| mv $BASE.save $BASE | |
| echo All done. If everything is correct, please remove the folder "restore.tmp" | |
| } | |
| read -n 1 -s -p "Press 1 for backup / 2 for restore: " action | |
| case $action | |
| in | |
| 1) backup ;; | |
| 2) restore ;; | |
| *) echo "Nothing to do" | |
| exit ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment