Last active
August 29, 2015 14:14
-
-
Save cesc1989/4dd0ab893047afb06507 to your computer and use it in GitHub Desktop.
Bash script to migrate/update Drupal website from development environment to production
This file contains 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 | |
# ESTE ARCHIVO TIENE POR PROPOSITO FACILITAR LA MIGRACION DE UN SITIO EN LOCAL | |
# A UN SITIO ANDANDO EN LINODE. SOLO PARA EL CASO DE ACTUALIZACIONES | |
#DECLARACIÓN DE VARIABLES NECESARIAS | |
greentext="\033[32m" | |
bold="\033[1m" | |
normal="\033[0m" | |
bkpdate=$(date +"%Y%m%d%s") | |
#Variable para saber si estoy en development o production | |
whereami=$(whoami) | |
if [ $whereami == "vagrant" ]; then | |
rootDbPass="root" | |
else | |
rootDbPass="Superman123." | |
fi | |
#CREAR DIRECTORIOS DE LA WEB | |
echo "Escribe el nombre del sitio web a configurar" | |
read siteName | |
#CAPTURA DE DATOS PARA LA BASE DE DATOS Y USUARIOS CON TODOS LOS PRIVILEGIOS | |
echo -e "Escribe el nombre de la base de datos para "$greentext"$siteName"$normal | |
read database_name | |
echo -e "El nombre de la base de datos es "$greentext"$database_name"$normal | |
#EJECUTAR COMANDOS PARA LA CREACION DE LA BASE DE DATOS | |
echo "Realizando operaciones en la base de datos" | |
echo -e "Creando backup de la base de datos"$greentext"$database_name"$normal | |
mysqldump -uroot -p$rootDbPass $database_name > "$siteName".bkp.sql | |
echo "Operación de borrado de base de datos" | |
#borramos la base de datos | |
mysql -uroot -p$rootDbPass -e "DROP DATABASE $database_name;" | |
#la volvemos a crear para hacer la migración | |
echo -e "Operación de creación de la base de datos "$greentext"$database_name"$normal | |
mysql -uroot -p$rootDbPass -e "CREATE DATABASE $database_name;" | |
#IMPORTAR ARCHIVO.SQL A BASE DE DATOS | |
echo "Escribe el nombre del archivo de backup de la base de datos" | |
read databaseBackupFile | |
mysql -uroot -p$rootDbPass $database_name < $databaseBackupFile | |
#MOVER Y DESCOMPRIMIR ARCHIVO TAR.GZ CON EL SITIO | |
echo "Escribe el nombre del archivo compreso del sitio" | |
read compressFileName | |
echo "Cambiando a la ruta del sitio a migrar" | |
cd /var/www/$siteName/public_html/ | |
#COMPRIMIR EL CONTENIDO ACTUAL | |
echo "Realizando backup del sitio actual" | |
sudo tar -zcf "$bkpdate"_backup.tar.gz . | |
#MOVER EL CONTENIDO ACTUAL COMPRESO A LA CARPETA BACKUPS | |
sudo mv "$bkpdate"_backup.tar.gz ../backups | |
#ELIMINAR EL CONTENIDO ACTUAL | |
cd .. | |
sudo chmod a+w -R /var/www/$siteName/public_html/ | |
sudo rm -Rf public_html/* | |
#MOVER | |
echo -e "Moviendo el archivo compreso a la carpeta "$greentext"$siteName"$normal | |
if [ $whereami == "vagrant" ]; then | |
sudo cp /var/www/$compressFileName /var/www/$siteName/public_html/ | |
else | |
sudo cp ~/$compressFileName /var/www/$siteName/public_html/ | |
fi | |
#DESCOMPRIMIR | |
cd /var/www/$siteName/public_html/ | |
echo "Descomprimiendo archivo del sitio" | |
sudo tar -xzf $compressFileName . | |
#PARA EDITAR ACHIVO SETTINGS.PHP DE DRUPAL | |
sudo mv sites/default/settings.php sites/default/local.settings.php | |
sudo mv sites/default/server.settings.php sites/default/settings.php | |
#CORRECCION DE PERMISOS EN CARPETA CTOOLS | |
echo -e "Cambiando propietario de todo el contenido en "$greentext"$siteName"$normal":\n user => cesc \t group => httpd_group \n" | |
chown -R cesc:www-data . | |
echo -e "Cambiando permisos de todos los directorios en "$greentext"$siteName"$normal" a "rwxr-x---"...\n" | |
find . -type d -exec chmod u=rwx,g=rx,o= '{}' \; | |
echo -e "Cambiando permisos de todos los archivos en "$greentext"$siteName"$normal" a "rw-r-----"...\n" | |
find . -type f -exec chmod u=rw,g=r,o= '{}' \; | |
echo -e "Cambiando permisos de la carpeta files en "$greentext"$siteName"$normal" /sites a "rwxrwx---"...\n" | |
cd sites | |
find . -type d -name files -exec chmod ug=rwx,o= '{}' \; | |
echo -e "Cambiando permisos de todos los archivos en files/ "$siteName/sites" a "rw-rw----"...\n" | |
echo -e "Cambiando permisos de todos los directorios en "files" de "$greentext"$siteName"$normal" a "rwxrwx---"...\n" | |
for x in ./*/files; do | |
find ${x} -type d -exec chmod ug=rwx,o= '{}' \; | |
find ${x} -type f -exec chmod ug=rw,o= '{}' \; | |
done | |
echo "El proceso ha terminado" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment