Update move site script for more options.

This commit is contained in:
BoHung Chiu 2022-07-14 21:33:25 +08:00
parent 867708250d
commit 2235968164
2 changed files with 174 additions and 47 deletions

View File

@ -1,6 +1,10 @@
#!/bin/bash
if [ -z "$6" ]; then
echo "Usage: $0 ip port username password example.com user_pass";
echo "You can pass custom site name and db name and server name!";
echo "Usage: $0 ip port username password example.com user_pass new_site_name new_db_name server_name_with_port";
echo "Example:"
echo "Usage: $0 127.0.0.1 22 Myuser Mypass example.com CurrentUserPassword xxxx xxxx new.example.com:3000";
history -c
exit 1;
fi
@ -9,11 +13,32 @@ port="$2";
user="$3";
pass="$4";
domain="$5";
user_pass="$6"
user_pass="$6";
new_site_name="$7";
new_db_name="$8";
if [[ ! -z "$9" ]]; then
new_server_name="$(echo "$9"|sed 's/:.\+//g')";
if [[ ! -z "$(echo "$9"|grep ':')" ]]; then
new_server_port="$(echo "$9"|sed 's/[^:]\+://g')";
fi
fi
domain_escape=`echo $domain|sed 's/\./\\\./g'`;
shopt -s dotglob;
if [[ "$ip" == "127.0.0.1" ]] || [[ "$ip" == "$local_ip" ]]; then
ssh_command="bash -l -c"
scp_command="cp -r -P "
use_local_command="true"
echo "Local command!"
else
ssh_command="sshpass -p $pass ssh $user@$ip -p $port";
scp_command="sshpass -p $pass scp -r -P $port $user@$ip";
nginx_file=`$ssh_command "grep -e $'\s''$domain_escape' /etc/nginx/orbit_sites/* -l"`;
scp_command="sshpass -p $pass scp -r -P $port $user@$ip:";
scan_ssh_keys=`ssh-keyscan -H $ip -p $port`
if [ -z "$(grep -w "$scan_ssh_keys" ~/.ssh/known_hosts)" ]; then
echo "$scan_ssh_keys" >> ~/.ssh/known_hosts
fi
use_local_command=""
fi
nginx_file=`$ssh_command "grep -e $'\s''$domain_escape' /etc/nginx/orbit_sites/* -l"|xargs|awk '{print $1}'`;
remote_sudo_command="echo $pass|sudo -S -p ''"
alias sudo_command="echo $user_pass|sudo -S -p ''"
if [ -z "$(which sshpass)" ]; then
@ -22,11 +47,17 @@ fi
function escape_slash(){
echo "$1"|sed 's/\//\\\//g'
}
if [ "$?" != 0 ]; then
echo "Please check the remote server is reachable";
function exit_command(){
cd $org_pwd
unalias sudo_command;
history -c
exit 1;
exit_code="$1";
if [[ -z "$exit_code" ]]; then
exit_code="0";
fi
history -c && exit $exit_code;
}
if [ "$?" != 0 ]; then
exit_command 1;
else
if [ -z "$nginx_file" ]; then
echo "$domain not found in nginx file!";
@ -36,10 +67,7 @@ else
echo "root_path: $root_path";
db_name=`$ssh_command "grep -w 'database' '$root_path/config/mongoid.yml' -m1|sed 's/database://g'| xargs"`
if [ -z $db_name ]; then
echo "There was some error when detecting Database!"
unalias sudo_command;
history -c
exit 1;
exit_command 1;
fi
echo "Database name: $db_name";
date_str=`date "+%Y%m%d"`
@ -66,14 +94,25 @@ else
fi
$ssh_command "$remote_sudo_command chown $user:$user $root_path/ssl_files -R"
fi
local_store_path="$HOME/orbit_sites/$db_name"
if [ -z "$new_site_name" ]; then
new_site_name="$db_name"
fi
if [ -z "$new_db_name" ]; then
new_db_name="$db_name"
fi
local_store_path="$HOME/orbit_sites/$new_site_name"
mkdir -p "$local_store_path"
$scp_command:$root_path/* $local_store_path/.
if [[ -z "$use_local_command" ]]; then
$scp_command$root_path/* $local_store_path/.
else
$scp_command$root_path/* $local_store_path/.
fi
cd $local_store_path
sudo_command cp -f "$(basename $nginx_file)" /etc/nginx/orbit_sites/.
new_nginx_file="/etc/nginx/orbit_sites/$(basename $nginx_file)"
mongorestore -d $db_name $(basename $mongo_output)/$db_name --drop
if [[ ! -z $match_ssl ]]; then
new_nginx_file="/etc/nginx/orbit_sites/$new_site_name"
sudo_command cp -f "$(basename $nginx_file)" $new_nginx_file
mongorestore -d $new_db_name $(basename $mongo_output)/$db_name --drop
sed "s/\(database:\s\+\).\+/\1${new_db_name}/g" -i "$local_store_path/config/mongoid.yml"
if [[ -z "$use_local_command" ]] && [[ ! -z "$match_ssl" ]]; then
if [[ ! -z $org_ssl_dir ]]; then
sudo_command mkdir -p $org_ssl_dir
if [ -z "$(which certbot)" ]; then
@ -89,6 +128,29 @@ else
echo "Finish changing ssl setting!"
fi
sudo_command sed "s/$(escape_slash $root_path)/$(escape_slash $local_store_path)/g" -i "$new_nginx_file"
old_sock_name=`sudo_command grep -w 'upstream' $new_nginx_file|sed 's/\(upstream\s\+\)\([^ ]\+\)/\2/g'|sed 's/\s*{//g'|xargs`
new_sock_name="${new_site_name}_sock"
sudo_command sed "s/\(upstream\s\+\)[^ ]\+/\1${new_sock_name}/g" -i "$new_nginx_file"
sudo_command sed "s/proxy_pass\s\+http:\/\/${old_sock_name}/proxy_pass http:\/\/${new_sock_name}/g" -i "$new_nginx_file"
if [[ ! -z $new_server_name ]]; then
match_listen_line=`sudo_command grep -E '(^|[^#]+)listen' $new_nginx_file -n|grep -v 'ssl'|cut -d : -f 1|xargs|awk '{print $1}'`
if [[ -z "$match_listen_line" ]]; then
match_listen_line=`sudo_command grep -E '(^|[^#]+)listen' $new_nginx_file -n|cut -d : -f 1|xargs|awk '{print $1}'`
if [[ ! -z "$match_listen_line" ]]; then
sudo_command sed $match_listen_line,+0"s/\([^#]\+\)listen.\+/\0\n\1listen $new_server_port;/g" -i $new_nginx_file
match_listen_line=$(($match_listen_line+1))
fi
else
sudo_command sed $match_listen_line,+0"s/\([^#]\+\)listen.\+/\1listen $new_server_port;/g" -i $new_nginx_file
fi
if [[ -z "$match_listen_line" ]]; then
echo "You need to edit nginx file: $new_nginx_file by yourself!"
else
sudo_command sed "s/server_name\([^;]\+\)/server_name\1 $new_server_name/g" -i $new_nginx_file
sudo_command sed "s/\(\$host\s*=\s*\)$domain_escape/\1$new_server_name/g" -i $new_nginx_file
sudo_command sed "s/\(return\s\+30\(1\|2\)\s\+https:\/\/\)$domain_escape/\1$new_server_name/g" -i $new_nginx_file
fi
fi
sudo_command nginx -t
if [ "$?" == "0" ];then
sudo_command service nginx restart;
@ -96,11 +158,10 @@ else
echo "Nginx setting has some problem!";
echo "Please restart nginx by yourself!";
fi
unalias sudo_command
bundle install
rm -f tmp/unicorn.sock
rm -f tmp/unicorn.sock tmp/pids/unicorn.pid
bundle exec unicorn_rails -c config/unicorn.rb -E production
echo "Finish moving and installing site!"
history -c
exit_command 0;
fi
fi

View File

@ -1,20 +1,50 @@
#!/bin/bash
echo "Remote Host IP: ";
read -r ip;
org_pwd="$(pwd)"
local_ip=`ip route get 8.8.8.8|xargs|awk '{print $7}'`;
if [[ "$ip" != "127.0.0.1" ]] && [[ "$ip" != "$local_ip" ]]; then
echo "Remote Host port: ";
read -r port;
echo "Remote Host user: ";
read -r user;
echo "Remote Host password: ";
read -r pass;
fi
echo "Target Domain: ";
read -r domain;
echo "Current user password for sudoer: ";
read -r user_pass;
echo "New Site name(ex: orbit_xxxx, can leave blank for auto): ";
read -r new_site_name;
echo "New Database name(ex: orbit_xxxx, can leave blank for auto): ";
read -r new_db_name;
echo "Server Name with port(ex: new.example.com:3000, can leave blank for not change): ";
read -r tmp;
if [[ ! -z "$tmp" ]]; then
new_server_name="$(echo "$tmp"|sed 's/:.\+//g')";
if [[ ! -z "$(echo "$tmp"|grep ':')" ]]; then
new_server_port="$(echo "$tmp"|sed 's/[^:]\+://g')";
fi
fi
tmp="";
domain_escape=`echo $domain|sed 's/\./\\\./g'`;
shopt -s dotglob;
if [[ "$ip" == "127.0.0.1" ]] || [[ "$ip" == "$local_ip" ]]; then
ssh_command="bash -l -c"
scp_command="cp -r -P "
use_local_command="true"
echo "Local command!"
else
ssh_command="sshpass -p $pass ssh $user@$ip -p $port";
scp_command="sshpass -p $pass scp -r -P $port $user@$ip";
nginx_file=`$ssh_command "grep -e $'\s''$domain_escape' /etc/nginx/orbit_sites/* -l"`;
scp_command="sshpass -p $pass scp -r -P $port $user@$ip:";
scan_ssh_keys=`ssh-keyscan -H $ip -p $port`
if [ -z "$(grep -w "$scan_ssh_keys" ~/.ssh/known_hosts)" ]; then
echo "$scan_ssh_keys" >> ~/.ssh/known_hosts
fi
use_local_command=""
fi
nginx_file=`$ssh_command "grep -e $'\s''$domain_escape' /etc/nginx/orbit_sites/* -l"|xargs|awk '{print $1}'`;
remote_sudo_command="echo $pass|sudo -S -p ''"
alias sudo_command="echo $user_pass|sudo -S -p ''"
if [ -z "$(which sshpass)" ]; then
@ -23,11 +53,17 @@ fi
function escape_slash(){
echo "$1"|sed 's/\//\\\//g'
}
if [ "$?" != 0 ]; then
echo "Please check the remote server is reachable";
function exit_command(){
cd $org_pwd
unalias sudo_command;
history -c
exit 1;
exit_code="$1";
if [[ -z "$exit_code" ]]; then
exit_code="0";
fi
history -c && exit $exit_code;
}
if [ "$?" != 0 ]; then
exit_command 1;
else
if [ -z "$nginx_file" ]; then
echo "$domain not found in nginx file!";
@ -37,10 +73,7 @@ else
echo "root_path: $root_path";
db_name=`$ssh_command "grep -w 'database' '$root_path/config/mongoid.yml' -m1|sed 's/database://g'| xargs"`
if [ -z $db_name ]; then
echo "There was some error when detecting Database!"
unalias sudo_command;
history -c
exit 1;
exit_command 1;
fi
echo "Database name: $db_name";
date_str=`date "+%Y%m%d"`
@ -67,14 +100,25 @@ else
fi
$ssh_command "$remote_sudo_command chown $user:$user $root_path/ssl_files -R"
fi
local_store_path="$HOME/orbit_sites/$db_name"
if [ -z "$new_site_name" ]; then
new_site_name="$db_name"
fi
if [ -z "$new_db_name" ]; then
new_db_name="$db_name"
fi
local_store_path="$HOME/orbit_sites/$new_site_name"
mkdir -p "$local_store_path"
$scp_command:$root_path/* $local_store_path/.
if [[ -z "$use_local_command" ]]; then
$scp_command$root_path/* $local_store_path/.
else
$scp_command$root_path/* $local_store_path/.
fi
cd $local_store_path
sudo_command cp -f "$(basename $nginx_file)" /etc/nginx/orbit_sites/.
new_nginx_file="/etc/nginx/orbit_sites/$(basename $nginx_file)"
mongorestore -d $db_name $(basename $mongo_output)/$db_name --drop
if [[ ! -z $match_ssl ]]; then
new_nginx_file="/etc/nginx/orbit_sites/$new_site_name"
sudo_command cp -f "$(basename $nginx_file)" $new_nginx_file
mongorestore -d $new_db_name $(basename $mongo_output)/$db_name --drop
sed "s/\(database:\s\+\).\+/\1${new_db_name}/g" -i "$local_store_path/config/mongoid.yml"
if [[ -z "$use_local_command" ]] && [[ ! -z "$match_ssl" ]]; then
if [[ ! -z $org_ssl_dir ]]; then
sudo_command mkdir -p $org_ssl_dir
if [ -z "$(which certbot)" ]; then
@ -90,6 +134,29 @@ else
echo "Finish changing ssl setting!"
fi
sudo_command sed "s/$(escape_slash $root_path)/$(escape_slash $local_store_path)/g" -i "$new_nginx_file"
old_sock_name=`sudo_command grep -w 'upstream' $new_nginx_file|sed 's/\(upstream\s\+\)\([^ ]\+\)/\2/g'|sed 's/\s*{//g'|xargs`
new_sock_name="${new_site_name}_sock"
sudo_command sed "s/\(upstream\s\+\)[^ ]\+/\1${new_sock_name}/g" -i "$new_nginx_file"
sudo_command sed "s/proxy_pass\s\+http:\/\/${old_sock_name}/proxy_pass http:\/\/${new_sock_name}/g" -i "$new_nginx_file"
if [[ ! -z $new_server_name ]]; then
match_listen_line=`sudo_command grep -E '(^|[^#]+)listen' $new_nginx_file -n|grep -v 'ssl'|cut -d : -f 1|xargs|awk '{print $1}'`
if [[ -z "$match_listen_line" ]]; then
match_listen_line=`sudo_command grep -E '(^|[^#]+)listen' $new_nginx_file -n|cut -d : -f 1|xargs|awk '{print $1}'`
if [[ ! -z "$match_listen_line" ]]; then
sudo_command sed $match_listen_line,+0"s/\([^#]\+\)listen.\+/\0\n\1listen $new_server_port;/g" -i $new_nginx_file
match_listen_line=$(($match_listen_line+1))
fi
else
sudo_command sed $match_listen_line,+0"s/\([^#]\+\)listen.\+/\1listen $new_server_port;/g" -i $new_nginx_file
fi
if [[ -z "$match_listen_line" ]]; then
echo "You need to edit nginx file: $new_nginx_file by yourself!"
else
sudo_command sed "s/server_name\([^;]\+\)/server_name\1 $new_server_name/g" -i $new_nginx_file
sudo_command sed "s/\(\$host\s*=\s*\)$domain_escape/\1$new_server_name/g" -i $new_nginx_file
sudo_command sed "s/\(return\s\+30\(1\|2\)\s\+https:\/\/\)$domain_escape/\1$new_server_name/g" -i $new_nginx_file
fi
fi
sudo_command nginx -t
if [ "$?" == "0" ];then
sudo_command service nginx restart;
@ -97,11 +164,10 @@ else
echo "Nginx setting has some problem!";
echo "Please restart nginx by yourself!";
fi
unalias sudo_command
bundle install
rm -f tmp/unicorn.sock
rm -f tmp/unicorn.sock tmp/pids/unicorn.pid
bundle exec unicorn_rails -c config/unicorn.rb -E production
echo "Finish moving and installing site!"
history -c
exit_command 0;
fi
fi