2022-07-11 00:20:47 +00:00
|
|
|
#!/bin/bash
|
|
|
|
echo "Remote Host IP: ";
|
|
|
|
read -r ip;
|
2022-07-14 13:33:25 +00:00
|
|
|
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
|
2022-07-11 00:20:47 +00:00
|
|
|
echo "Target Domain: ";
|
|
|
|
read -r domain;
|
|
|
|
echo "Current user password for sudoer: ";
|
|
|
|
read -r user_pass;
|
2022-07-14 13:33:25 +00:00
|
|
|
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')";
|
2022-09-06 14:50:31 +00:00
|
|
|
if [[ -z "$new_server_port" ]]; then
|
|
|
|
new_server_port="80";
|
|
|
|
fi
|
2022-10-25 02:43:05 +00:00
|
|
|
else
|
|
|
|
new_server_port="80";
|
2022-07-14 13:33:25 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
tmp="";
|
2022-07-11 00:20:47 +00:00
|
|
|
domain_escape=`echo $domain|sed 's/\./\\\./g'`;
|
2022-07-14 13:33:25 +00:00
|
|
|
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:";
|
|
|
|
scan_ssh_keys=`ssh-keyscan -H $ip -p $port`
|
2022-10-25 02:43:05 +00:00
|
|
|
if [[ ! -e ~/.ssh/known_hosts ]] || [[ -z "$(grep -w "$scan_ssh_keys" ~/.ssh/known_hosts)" ]]; then
|
2022-07-14 13:33:25 +00:00
|
|
|
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}'`;
|
2023-04-20 06:16:39 +00:00
|
|
|
remote_sudo_command="echo '$pass'|sudo -S -p ' '"
|
|
|
|
alias sudo_command="echo '$user_pass'|sudo -S -p ' '"
|
2022-07-11 00:20:47 +00:00
|
|
|
if [ -z "$(which sshpass)" ]; then
|
|
|
|
sudo_command apt install sshpass -y;
|
|
|
|
fi
|
|
|
|
function escape_slash(){
|
|
|
|
echo "$1"|sed 's/\//\\\//g'
|
|
|
|
}
|
2022-07-14 13:33:25 +00:00
|
|
|
function exit_command(){
|
|
|
|
cd $org_pwd
|
2022-07-11 00:20:47 +00:00
|
|
|
unalias sudo_command;
|
2022-07-14 13:33:25 +00:00
|
|
|
exit_code="$1";
|
|
|
|
if [[ -z "$exit_code" ]]; then
|
|
|
|
exit_code="0";
|
|
|
|
fi
|
|
|
|
history -c && exit $exit_code;
|
|
|
|
}
|
|
|
|
if [ "$?" != 0 ]; then
|
|
|
|
exit_command 1;
|
2022-07-11 00:20:47 +00:00
|
|
|
else
|
|
|
|
if [ -z "$nginx_file" ]; then
|
|
|
|
echo "$domain not found in nginx file!";
|
|
|
|
echo "Please Check ip and domain is correct";
|
|
|
|
else
|
2022-09-24 03:59:23 +00:00
|
|
|
root_path=`$ssh_command "cat '$nginx_file'|grep -E '(^|^\s+)root'|sed -E 's/(^|^\s+)root\s+//'|tr ';' ' '|awk '{print \\$1}'|sed 's/\/public$//g'|xargs|awk '{print \\$1}'"`;
|
2022-07-11 00:20:47 +00:00
|
|
|
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
|
2022-07-14 13:33:25 +00:00
|
|
|
exit_command 1;
|
2022-07-11 00:20:47 +00:00
|
|
|
fi
|
|
|
|
echo "Database name: $db_name";
|
|
|
|
date_str=`date "+%Y%m%d"`
|
|
|
|
mongo_output="$root_path/dump_$date_str"
|
|
|
|
$ssh_command "mongodump -d $db_name -o '$mongo_output'"
|
|
|
|
$ssh_command "cp -f $nginx_file '$root_path/.'"
|
|
|
|
match_ssl=`$ssh_command "cat '$nginx_file'|grep -E '443\s+ssl'"`
|
|
|
|
if [[ ! -z $match_ssl ]]; then
|
|
|
|
echo "SSL Detected!"
|
|
|
|
ssl_certificate_file=`$ssh_command "grep -E '^(|\s+)ssl_certificate\s' $nginx_file|sed -E 's/^(|\s+)ssl_certificate(|_key)//g'|sed -E 's/;.*//g'|xargs|awk '{print \\$1}'"`
|
|
|
|
ssl_certificate_key_file=`$ssh_command "grep -E '^(|\s+)ssl_certificate_key\s' $nginx_file|sed -E 's/^(|\s+)ssl_certificate(|_key)//g'|sed -E 's/;.*//g'|xargs|awk '{print \\$1}'"`
|
|
|
|
$ssh_command "mkdir -p $root_path/ssl_files"
|
|
|
|
if [[ "$ssl_certificate_file" == *"/letsencrypt/"* ]]; then
|
|
|
|
echo "Use certbot!"
|
|
|
|
real_domain=`basename $(dirname $ssl_certificate_file)`
|
|
|
|
$ssh_command "mkdir -p $root_path/ssl_files/renewal"
|
|
|
|
$ssh_command "$remote_sudo_command cp -L /etc/letsencrypt/renewal/$real_domain.conf $root_path/ssl_files/renewal/."
|
|
|
|
org_ssl_dir=`dirname $ssl_certificate_file`
|
2022-09-24 04:13:43 +00:00
|
|
|
new_ssl_dir="ssl_files/$(basename $org_ssl_dir)"
|
2022-07-11 00:20:47 +00:00
|
|
|
$ssh_command "$remote_sudo_command cp -r -f -L $org_ssl_dir $root_path/ssl_files/."
|
2022-09-25 07:56:23 +00:00
|
|
|
certbot_file_idx=`$ssh_command "$remote_sudo_command readlink /etc/letsencrypt/live/$real_domain/cert.pem"|xargs basename|sed -E 's/cert([^.]+)\.pem/\1/g'|xargs`
|
2022-07-11 00:20:47 +00:00
|
|
|
else
|
|
|
|
$ssh_command "$remote_sudo_command cp -f $ssl_certificate_file $root_path/ssl_files/."
|
|
|
|
$ssh_command "$remote_sudo_command cp -f $ssl_certificate_key_file $root_path/ssl_files/."
|
|
|
|
fi
|
|
|
|
$ssh_command "$remote_sudo_command chown $user:$user $root_path/ssl_files -R"
|
|
|
|
fi
|
2022-07-14 13:33:25 +00:00
|
|
|
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"
|
2022-07-11 00:20:47 +00:00
|
|
|
mkdir -p "$local_store_path"
|
2022-09-24 03:59:23 +00:00
|
|
|
$scp_command$root_path/* $local_store_path/. 2>>/dev/null
|
|
|
|
$scp_command$root_path/.[^.]* $local_store_path/. 2>>/dev/null
|
2022-09-25 08:54:27 +00:00
|
|
|
if [[ ! -z "$org_ssl_dir" ]]; then
|
|
|
|
if [ -z "$(which certbot)" ]; then
|
|
|
|
wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/install_certbot.sh
|
|
|
|
sudo_command bash ./install_certbot.sh
|
|
|
|
fi
|
|
|
|
fi
|
2022-07-11 00:20:47 +00:00
|
|
|
cd $local_store_path
|
2022-07-14 13:33:25 +00:00
|
|
|
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
|
2022-09-25 08:17:18 +00:00
|
|
|
if [[ ! -z "$org_ssl_dir" ]]; then
|
2022-09-25 03:49:39 +00:00
|
|
|
sudo_command mkdir -p $org_ssl_dir
|
|
|
|
sudo_command mkdir -p /etc/letsencrypt/renewal
|
2022-09-24 03:59:23 +00:00
|
|
|
sudo_command cp -r $new_ssl_dir/* $org_ssl_dir/.
|
2023-04-20 06:16:39 +00:00
|
|
|
if [[ "$(dirname $org_ssl_dir)" == "/etc/letsencrypt/live" ]] && [[ ! -z "$(sudo_command find $org_ssl_dir -name '*.pem' 2>>/dev/null || echo '')" ]]; then
|
2022-09-25 03:49:39 +00:00
|
|
|
sudo_command mkdir -p /etc/letsencrypt/archive/$real_domain
|
|
|
|
sudo_command rm -f /etc/letsencrypt/archive/$real_domain/*.pem
|
2023-04-20 06:32:52 +00:00
|
|
|
sudo_command find $org_ssl_dir -name '*.pem' -exec mv {} /etc/letsencrypt/archive/$real_domain/. \;
|
2022-09-25 03:49:39 +00:00
|
|
|
if [[ ! -z "$certbot_file_idx" ]]; then
|
|
|
|
sudo_command bash -l -c "find '/etc/letsencrypt/archive/$real_domain' -regex '[^0-9]+\.pem' -printf '%p\0'| perl -0 -l0 -pe 'print \$_; s/($(echo /etc/letsencrypt/archive/$real_domain/|sed 's/[\.\/]/\\\0/g')[^\d]+)\.pem/\${1}'$certbot_file_idx'\.pem/'| xargs -0 -n 2 mv"
|
|
|
|
sudo_command bash -l -c "cd $org_ssl_dir && find '../../archive/$real_domain/' -regex '[^0-9]+$certbot_file_idx\.pem' -printf '%p\0'| perl -0 -l0 -pe 'print \$_; s/$(echo ../../archive/$real_domain/|sed 's/[\.\/]/\\\0/g')([^\d]+)$certbot_file_idx\.pem/\${1}\.pem/'| xargs -0 -n 2 ln -s"
|
|
|
|
else
|
|
|
|
sudo_command bash -l -c "cd $org_ssl_dir && ln -s ../../archive/$real_domain/*.pem ."
|
|
|
|
fi
|
|
|
|
fi
|
2022-09-25 08:01:09 +00:00
|
|
|
sudo_command cp -r ssl_files/renewal/* /etc/letsencrypt/renewal/.
|
2022-09-25 03:49:39 +00:00
|
|
|
if [[ -e /etc/letsencrypt/renewal/$real_domain.conf ]]; then #Fix certbot account
|
|
|
|
letsencrypt_server_name=`sudo_command ls /etc/letsencrypt/accounts/|xargs|awk '{print $1}'`
|
|
|
|
if [[ ! -z "$letsencrypt_server_name" ]]; then
|
|
|
|
sudo_command sed -E 's/server\s*=.*/server = https:\/\/'$letsencrypt_server_name'\/directory/g' -i /etc/letsencrypt/renewal/$real_domain.conf
|
|
|
|
letsencrypt_account_id=`sudo_command ls /etc/letsencrypt/accounts/$letsencrypt_server_name/directory|xargs|awk '{print $1}'`
|
|
|
|
if [[ ! -z "$letsencrypt_account_id" ]]; then
|
|
|
|
sudo_command sed -E 's/account\s*=.*/account = '$letsencrypt_account_id'/g' -i /etc/letsencrypt/renewal/$real_domain.conf
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2022-09-25 08:17:18 +00:00
|
|
|
sudo_command chmod 700 /etc/letsencrypt/archive -R
|
|
|
|
sudo_command chmod 700 /etc/letsencrypt/live -R
|
|
|
|
rm -rf ssl_files
|
2022-07-11 00:20:47 +00:00
|
|
|
else
|
|
|
|
sed "s/$(escape_slash $ssl_certificate_file)/$(escape_slash $root_path)\/ssl_files\/$(basename $ssl_certificate_file)/g" -i $new_nginx_file
|
|
|
|
sed "s/$(escape_slash $ssl_certificate_key_file)/$(escape_slash $root_path)\/ssl_files\/$(basename $ssl_certificate_key_file)/g" -i $new_nginx_file
|
|
|
|
fi
|
|
|
|
echo "Finish changing ssl setting!"
|
|
|
|
fi
|
|
|
|
sudo_command sed "s/$(escape_slash $root_path)/$(escape_slash $local_store_path)/g" -i "$new_nginx_file"
|
2022-07-14 13:33:25 +00:00
|
|
|
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
|
2023-04-20 06:16:39 +00:00
|
|
|
sudo_command sed "s/$(echo $ip|sed 's/\./\\\./g')/$(echo $local_ip|sed 's/\./\\\./g')/g" -i $new_nginx_file
|
2022-07-11 00:20:47 +00:00
|
|
|
sudo_command nginx -t
|
|
|
|
if [ "$?" == "0" ];then
|
|
|
|
sudo_command service nginx restart;
|
|
|
|
else
|
|
|
|
echo "Nginx setting has some problem!";
|
|
|
|
echo "Please restart nginx by yourself!";
|
|
|
|
fi
|
2022-09-06 14:50:31 +00:00
|
|
|
rm -f Gemfile.lock
|
2022-09-24 04:28:01 +00:00
|
|
|
ruby_ver=`basename $(readlink -n $rvm_prefix/rvm/rubies/default)|cut -d '-' -f 2`
|
|
|
|
if [[ ! -z "$ruby_ver" ]]; then
|
|
|
|
echo "$ruby_ver" > .ruby-version
|
|
|
|
fi
|
2022-07-11 00:20:47 +00:00
|
|
|
bundle install
|
2022-07-14 13:33:25 +00:00
|
|
|
rm -f tmp/unicorn.sock tmp/pids/unicorn.pid
|
2022-09-06 14:50:31 +00:00
|
|
|
wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/start_site.sh -O start_site.sh
|
|
|
|
wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/close_site.sh -O close_site.sh
|
|
|
|
bash ./start_site.sh production
|
2022-07-11 00:20:47 +00:00
|
|
|
echo "Finish moving and installing site!"
|
2022-09-06 14:50:31 +00:00
|
|
|
echo "Site Path: $local_store_path"
|
2022-07-14 13:33:25 +00:00
|
|
|
exit_command 0;
|
2022-07-11 00:20:47 +00:00
|
|
|
fi
|
|
|
|
fi
|