Skip to content

Instantly share code, notes, and snippets.

@kapb14
Created April 28, 2017 16:40
Show Gist options
  • Select an option

  • Save kapb14/1c3cde2668492b8c4b8332a8d48cc511 to your computer and use it in GitHub Desktop.

Select an option

Save kapb14/1c3cde2668492b8c4b8332a8d48cc511 to your computer and use it in GitHub Desktop.
Поменять адрес MySQL сервера в конфигах Битрикс. С поиском, проверками, бэкапами, откатом изменений (если что-то пошло не так)...
#!/bin/bash
if [[ -z $1 ]]; then
NEW_DB="127.0.0.1"
else
NEW_DB="$1"
fi
DIR=$(pwd)
CFG_A="bitrix/.settings.php"
CFG_B="bitrix/php_interface/dbconn.php"
# ensure both cfgs present
if [[ -f ${DIR}/${CFG_A} ]]; then echo "[OK] CFG_A"; else echo "[FAIL] CFG_A"; exit 1; fi
if [[ -f ${DIR}/${CFG_B} ]]; then echo "[OK] CFG_B"; else echo "[FAIL] CFG_B"; exit 1; fi
# get current db address
CURRENT_DB=$(grep "^\$DBHost = " ${DIR}/${CFG_B} | awk $'{print $3}' | tr -d '"',';')
if [[ -z $CURRENT_DB ]]; then
echo "[FAIL] CURRENT_DB: $CURRENT_DB"
exit 1
else
echo "[OK] CURRENT_DB: $CURRENT_DB"
fi
# test change in cfg1
grep $CURRENT_DB ${DIR}/${CFG_A} || exit 1
sed "s/$CURRENT_DB/$NEW_DB/g" ${DIR}/${CFG_A} | grep $NEW_DB || exit 1
# test change in cfg2
grep $CURRENT_DB ${DIR}/${CFG_B} || exit 1
sed "s/$CURRENT_DB/$NEW_DB/g" ${DIR}/${CFG_B} | grep $NEW_DB || exit 1
# backup configs
(cp -v ${DIR}/${CFG_A} ${DIR}/../backup-settings.php && cp -v ${DIR}/${CFG_A} ${DIR}/../backup-dbconn.php) || exit 1
# change in cfg1 and restore from backup if it fails
if ! sed -i "s/$CURRENT_DB/$NEW_DB/g" ${DIR}/${CFG_A}; then
mv -v ${DIR}/../backup-settings.php ${DIR}/${CFG_A}
exit 1
fi
# change in cfg2 and restore from backup if it fails
if ! sed -i "s/$CURRENT_DB/$NEW_DB/g" ${DIR}/${CFG_B}; then
mv -v ${DIR}/../backup-dbconn.php ${DIR}/${CFG_B}
exit 1
fi
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment