From c2d4a6d9f3a6078845ff8297f9b604d351a74b64 Mon Sep 17 00:00:00 2001 From: Bohung Date: Fri, 8 Jul 2022 18:13:39 +0800 Subject: [PATCH] Add move site script! --- README.md | 5 +++ move_site.sh | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 move_site.sh diff --git a/README.md b/README.md index 810ccec..fabfda5 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,11 @@ $ exit; exit # 第一次 exit 退回 root 帳號,第二次 exit 退回剛登 ```sh $ wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/install_orbit_environment.sh -O install_orbit_environment.sh $ source install_orbit_environment.sh #請使用要執行網站的user(須為具有sudo權限之帳號)來執行,執行後會需要輸入user密碼 + +# 轉移網站 +$ wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/move_site.sh +# 請依照您的需求替換以下這行的變數成實際的值 +$ bash -i move_site.sh $ip $port $user $remote_pass $domain $your_local_password ## 以下其他內容均不需執行,可直接進行架站或移機 ``` diff --git a/move_site.sh b/move_site.sh new file mode 100644 index 0000000..2471fd3 --- /dev/null +++ b/move_site.sh @@ -0,0 +1,102 @@ +#!/bin/bash +if [ -z "$6" ]; then + echo "Usage: $0 ip port username password example.com user_pass"; + exit 1; +fi +ip="$1"; +port="$2"; +user="$3"; +pass="$4"; +domain="$5"; +user_pass="$6" +domain_escape=`echo $domain|sed 's/\./\\\./g'`; +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"`; +remote_sudo_command="echo $pass|sudo -S -p ''" +alias sudo_command="echo $user_pass|sudo -S -p ''" +if [ -z "$(which sshpass)" ]; then + sudo_command apt install sshpass -y; +fi +function escape_slash(){ + echo "$1"|sed 's/\//\\\//g' +} +if [ "$?" != 0 ]; then + echo "Please check the remote server is reachable"; + unalias sudo_command; + exit 1; +else + if [ -z "$nginx_file" ]; then + echo "$domain not found in nginx file!"; + echo "Please Check ip and domain is correct"; + else + 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'"`; + 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; + exit 1; + 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` + new_ssl_dir="ssl_files/$(basename org_ssl_dir)" + $ssh_command "$remote_sudo_command cp -r -f -L $org_ssl_dir $root_path/ssl_files/." + 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 + local_store_path="$HOME/orbit_sites/$db_name" + mkdir -p "$local_store_path" + $scp_command:$root_path/* $local_store_path/. + 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) --drop + if [[ ! -z $match_ssl ]]; then + if [[ ! -z $org_ssl_dir ]]; then + sudo_command mkdir -p $org_ssl_dir + 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 + sudo_command sudo cp -r $new_ssl_dir/* $org_ssl_dir/. + sudo_command sudo cp -r $new_ssl_dir/renewal/* /etc/letsencrypt/renewal/. + 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" + 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 + unalias sudo_command + bundle install + rm -f tmp/unicorn.sock + bundle exec unicorn_rails -c config/unicorn.rb -E production + echo "Finish moving and installing site!" + fi +fi \ No newline at end of file