Script to update a local project from remote

I have a few local projects that need to be refreshed from the live site on a regular basis so file name references and data are complete for development work. Wrote myself a quick n’ dirty script. This happens to be for a Drupal project but you can use it for anything version controlled in git and using MySQL. Replace the variables as needed.

# update.sh
# updates the local repo with code, database, files

echo "What do you want to update?

[1] Code
[2] Database
[3] Files
[4] All";

read update

# ----- COMMANDS ----- #

remote_host='YOUR.HOST.NAME.OR.IP'
connection_info="REMOTEUSER@$remote_host"

# Code update
code() {
  echo "Updating code";
  git pull
}

# Database update
database() {
  echo "Updating database";
  backup_file='latest.sql'
  # Get a compressed copy of the remote database
  ssh $connection_info "mysqldump --no-tablespaces -u $MYSQL_USERNAME -p$MYSQL_PASSWORD $DB_NAME | gzip -9" > $backup_file.gz
  # Decompress the backup
  gunzip $backup_file.gz
  # If you have collation issues, use in-place sed to swap out to local's settings
  #sed -i '' 's/utf8mb4_0900_ai_ci/utf8_unicode_ci/g' $backup_file
  #sed -i '' 's/utf8mb4_general_ci/utf8_unicode_ci/g' $backup_file
  #sed -i '' 's/utf8mb4/utf8/g' $backup_file
  #sed -i '' 's/CHARSET=utf8mb4/CHARSET=utf8/g' $backup_file
  # Import the backup file
  mysql -u $LOCAL_MYSQL_USERNAME -p$LOCAL_MYSQL_PASSWORD $LOCAL_DB_NAME < $backup_file
  # Remove the backup file for tidiness
  rm $backup_file
}

# Files update
files() {
  echo "Updating files";
  path_to_files_dir='./$PATH_TO_FILES_DIR_IN_WEBROOT'
  rsync -avr -e ssh $connection_info:/$PATH_TO_WEBROOT/$path_to_files_dir/files $path_to_files_dir --exclude=css --exclude=js --exclude=styles --exclude=php
}

# ----- PROCESS INPUT ----- #

case $update in
  1)
    code ;;
  2)
    database ;;
  3)
    files ;;
  4)
    code; database; files ;;

  # Invalid input
  *) echo Invalid option ; exit 1 ;;
esac