#!/bin/bash
#if [[ "$EUID" -ne 0 ]]; then
#	echo "Sorry, you need to run this as root"
#	exit
#fi

# This script is meant to be used in the origin server



# install rsync if not already available
if ! hash rsync 2>/dev/null; then
    sudo apt-get update
    sudo apt-get install -y rsync
fi



if [[ -z $destination_ip ]]; then
    echo
    echo "Please, specify the DESTINATION server IP address"
    read -p "Destination server IP: " destination_ip
fi



while [[ -z $action ]]; do
    echo
    echo "What do you want to do?"
    echo
    echo "   1) Setup authentication for a new remote server"
    echo "   2) Sync a local site to a remote server"
    echo "   3) Schedule a site Sync to remote server"
    echo "   4) Unschedule a site Sync to remote server"
    read -p "Action: " action
    until [[ -z "$action" || "$action" =~ ^[1-4]$ ]]; do
    	echo "$action: invalid selection."
    	read -p "Action: " action
    done
done



if [[ $action == "auth" || $action == "1" ]]; then
#    if ssh -o ConnectTimeout=30 -o StrictHostKeyChecking=no -o PasswordAuthentication=no $sshuser@$destination_ip "echo" &>/dev/null; then
#        echo "Authentication is already set up for $destination_ip"
#        exit
#    fi

    # create ssh key pair if it doesn't exist. do nothing if it already exists
    localuser=`sh -c 'echo ${SUDO_USER:-$USER}'`
    homedir=`grep $localuser /etc/passwd|cut -d':' -f6`
    cat /dev/zero | ssh-keygen -f "$homedir/.ssh/id_rsa" -q -N "" >/dev/null 2>&1
    # make it available with nginx
    sudo cp ~/.ssh/id_rsa.pub /var/www/html/97317319160494330146381820240308.pub 2>/dev/null
    echo "Authentication has been set up in this origin server.
Run 82-destination.sh in the destination server now."
    exit
fi



if [[ $action == "site-sync" || $action == "2" ]]; then
    if [ -z $sshuser ]
    then
        read -p "SSH user to login on destination server: " sshuser
    fi

    localuser=`sh -c 'echo ${SUDO_USER:-$USER}'`
    if ! ssh -o ConnectTimeout=30 -o StrictHostKeyChecking=no -o PasswordAuthentication=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "echo" &>/dev/null; then
        echo "Authentication must be set up first for $destination_ip"
        exit
    fi

    # if host was connectable, ask user what site does he want to copy
    while [[ -z $domain ]]; do
        clear
        echo "Please, select which site you want to copy"
        ls /var/www | grep -v html | nl
        echo
        read -p "Select site: " site_number
        number_of_sites=$(ls /var/www | grep -v html | wc -l)
        until [[ "$site_number" =~ ^[0-9]+$ && "$site_number" -le "$number_of_sites" ]]; do
        	echo "$site_number: invalid selection."
        	read -p "Select site: " site_number
        done
        domain=$(ls /var/www | grep -v html | sed -n "$site_number"p)
    done
    user_name=$(echo $domain | cut -c1-32)

    # error checking
    if [[ ! -d /var/www/$domain ]]; then
        echo "$domain files are not present. Aborting!"
        exit
    fi
    if [[ ! -e /etc/nginx/sites-enabled/$domain ]]; then
        echo "$domain NGINX configuration is not present. Aborting!"
        exit
    fi

    # once a site is selected, start the copy process
    # remove leftover. if we reached this point, the SSH authentication is now configured
    sudo rm -f /var/www/html/97317319160494330146381820240308.pub

    # export user
	echo "Exporting users..."
    sudo grep $user_name /etc/passwd > ~/passwd.prod
    sudo grep $user_name /etc/passwd | awk -F: '{print $1}' | sudo grep -f - /etc/shadow > ~/shadow.prod
    # sync user to destination
    rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/passwd.prod $sshuser@$destination_ip:~/passwd.prod
    rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/shadow.prod $sshuser@$destination_ip:~/shadow.prod
    sudo rm -f ~/passwd.prod ~/shadow.prod
    # set up users in the destination
	echo "Setting up users on destination server..."
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo grep -v $user_name /etc/passwd > ~/passwd.new && sudo cat ~/passwd.prod >> ~/passwd.new && sudo cppw ~/passwd.new"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "echo $user_name | sudo grep -vf - /etc/shadow > ~/shadow.new && sudo cat ~/shadow.prod >> ~/shadow.new && sudo cppw -s ~/shadow.new"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo rm -f ~/passwd.prod ~/passwd.new ~/shadow.prod ~/shadow.new"

    # dump mysql stuff
	echo "Exporting database. This could take a while..."
    mysql_db=$(grep DB_NAME /var/www/$domain/html/wp-config.php | cut -d "'" -f 4)
    mysql_user=$(grep DB_USER /var/www/$domain/html/wp-config.php | cut -d "'" -f 4)
    mysql_pass=$(grep DB_PASSWORD /var/www/$domain/html/wp-config.php | cut -d "'" -f 4)
    cat > ~/dbimport.sql <<QUERY
GRANT ALL ON $mysql_db.* TO '$mysql_user'@'localhost' IDENTIFIED BY '$mysql_pass' WITH GRANT OPTION;
FLUSH PRIVILEGES;
QUERY
    sudo mysqldump --databases $mysql_db --add-drop-database > ~/database_backup.sql
    # sync sql files to destination
	echo "Sending database to target server. This could take a while..."
    rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/dbimport.sql $sshuser@$destination_ip:~/dbimport.sql
    rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/database_backup.sql $sshuser@$destination_ip:~/database_backup.sql
    sudo rm -f ~/dbimport.sql ~/database_backup.sql
    # import in destination
	echo "Importing database at target server. This could take a while..."
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo mariadb < ~/database_backup.sql"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo mariadb < ~/dbimport.sql"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo rm -f ~/dbimport.sql ~/database_backup.sql"

    # sync files
	echo "Pushing files to target server. This could take a while..."
    sudo rsync -a --stats --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /var/www/$domain $sshuser@$destination_ip:/var/www
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/nginx/sites-enabled/*$domain $sshuser@$destination_ip:/etc/nginx/sites-enabled
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/nginx/htpasswd/*$domain $sshuser@$destination_ip:/etc/nginx/htpasswd/ > /dev/null 2>&1
    php_version=$(ls /etc/php/*/fpm/pool.d/$domain.conf | cut -d '/' -f 4)
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/php/$php_version/fpm/pool.d/$domain.conf $sshuser@$destination_ip:/etc/php/$php_version/fpm/pool.d
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo mkdir -p /etc/letsencrypt/"
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/letsencrypt/live/$domain $sshuser@$destination_ip:/etc/letsencrypt/live > /dev/null 2>&1
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/letsencrypt/archive/$domain $sshuser@$destination_ip:/etc/letsencrypt/archive > /dev/null 2>&1
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" --include="*.$domain/***" --exclude='*' /etc/letsencrypt/live/ $sshuser@$destination_ip:/etc/letsencrypt/live/ > /dev/null 2>&1
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" --include="*.$domain/***" --exclude='*' /etc/letsencrypt/archive/ $sshuser@$destination_ip:/etc/letsencrypt/archive/ > /dev/null 2>&1
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/letsencrypt/options-ssl-nginx.conf $sshuser@$destination_ip:/etc/letsencrypt > /dev/null 2>&1
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/letsencrypt/ssl-dhparams.pem $sshuser@$destination_ip:/etc/letsencrypt > /dev/null 2>&1
    sudo rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /var/spool/cron/crontabs/$domain $sshuser@$destination_ip:/var/spool/cron/crontabs > /dev/null 2>&1

    # remove ipv6only=on from our synced site if already present in a different site
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo grep -qr --exclude=*$domain ipv6only=on /etc/nginx/sites-enabled/ && sudo sed -i s/ipv6only=on//g /etc/nginx/sites-enabled/*$domain"

    # restart services
	echo "Restarting services on target server..."
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php5.6-fpm"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php7.1-fpm"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php7.2-fpm"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php7.3-fpm"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php7.4-fpm"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php8.0-fpm"
	ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php8.1-fpm"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart nginx"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart cron"

    # sync sshd config
    sudo sed -n "/Match User $user_name/,+2 p" /etc/ssh/sshd_config > ~/sshdcfg
    rsync -a --rsync-path 'sudo -i rsync' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/sshdcfg $sshuser@$destination_ip:~/sshdcfg
    sudo rm -f ~/sshdcfg
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo sed -i \"/Match User $user_name/,+2 d\" /etc/ssh/sshd_config && cat ~/sshdcfg|sudo tee -a /etc/ssh/sshd_config > /dev/null && rm -f ~/sshdcfg"
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart sshd"
	
	echo "Site Sync Completed Successfully."

fi

if [[ $action == "schedule-site-sync" || $action == "3" ]]; then
    # if host was connectable, ask user what site does he want to copy
    if [ -z $sshuser ]
    then
        read -p "SSH user to login on destination server: " sshuser
    fi

    localuser=`sh -c 'echo ${SUDO_USER:-$USER}'`
    while [[ -z $domain ]]; do
        echo "Please select which site to schedule for copying to the target server"
        ls /var/www | grep -v html | nl
        echo
        read -p "Select site: " site_number
        number_of_sites=$(ls /var/www | grep -v html | wc -l)
        until [[ "$site_number" =~ ^[0-9]+$ && "$site_number" -le "$number_of_sites" ]]; do
                echo "$site_number: invalid selection."
                read -p "Select site: " site_number
        done
        domain=$(ls /var/www | grep -v html | sed -n "$site_number"p)
    done

    if [[ -z "$site_sync_callback_url" ]]
    then
        read -p "enter callback url to get syncing update:  " site_sync_callback_url
    fi

    # once a site is selected, start the copy process
    # remove leftover. if we reached this point, the SSH authentication is now configured
    sudo rm -f /var/www/html/97317319160494330146381820240308.pub

    grep -q "$domain $destination_ip " /etc/wp-site-sync.conf
    if [ $? -ne 0 ]
    then
	    echo "$domain $destination_ip $sshuser $localuser $site_sync_callback_url" >> /etc/wp-site-sync.conf
    fi

    echo $'#!/bin/bash
while IFS=" " read -r domain destination_ip sshuser localuser site_sync_callback_url; do
    date=`date +%d-%m-%y-%H`
    exec >> /var/log/wp-sync-$date.log 2>&1

    curl -sS "$site_sync_callback_url?domain=$domain&destination_server=$destination_ip&syncstatus=start"
    echo "Exporting users..."
    user_name=$(echo $domain | cut -c1-32)
    sudo grep $user_name /etc/passwd > ~/passwd.prod
    sudo grep $user_name /etc/passwd | awk -F: '\''{print $1}'\'' | sudo grep -f - /etc/shadow > ~/shadow.prod
    # sync user to destination
    rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/passwd.prod $sshuser@$destination_ip:~/passwd.prod < /dev/null
    rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/shadow.prod $sshuser@$destination_ip:~/shadow.prod < /dev/null
    sudo rm -f ~/passwd.prod ~/shadow.prod
    # set up users in the destination
	echo "Setting up users on destination server..."
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo grep -v $user_name /etc/passwd > ~/passwd.new && sudo cat ~/passwd.prod >> ~/passwd.new && sudo cppw ~/passwd.new" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "echo $user_name |sudo grep -vf - /etc/shadow > ~/shadow.new && sudo cat ~/shadow.prod >> ~/shadow.new && sudo cppw -s ~/shadow.new" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo rm -f ~/passwd.prod ~/passwd.new ~/shadow.prod ~/shadow.new" < /dev/null

    # dump mysql stuff
    echo "Exporting database. This could take a while..."
    mysql_db=$(grep DB_NAME /var/www/$domain/html/wp-config.php | cut -d "'\''" -f 4)
    mysql_user=$(grep DB_USER /var/www/$domain/html/wp-config.php | cut -d "'\''" -f 4)
    mysql_pass=$(grep DB_PASSWORD /var/www/$domain/html/wp-config.php | cut -d "'\''" -f 4)
    cat > ~/dbimport.sql <<QUERY
GRANT ALL ON $mysql_db.* TO '\''$mysql_user'\''@'\''localhost'\'' IDENTIFIED BY '\''$mysql_pass'\'' WITH GRANT OPTION;
FLUSH PRIVILEGES;
QUERY
    sudo mysqldump --databases $mysql_db --add-drop-database > ~/database_backup.sql
    # sync sql files to destination
	echo "Sending database to target server. This could take a while..."
    rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/dbimport.sql $sshuser@$destination_ip:~/dbimport.sql < /dev/null
    rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/database_backup.sql $sshuser@$destination_ip:~/database_backup.sql < /dev/null
    sudo rm -f ~/dbimport.sql ~/database_backup.sql
    # import in destination
	echo "Importing database at target server. This could take a while..."
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo mariadb < ~/database_backup.sql" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo mariadb < ~/dbimport.sql" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo rm -f ~/dbimport.sql ~/database_backup.sql" < /dev/null

    # sync files
	echo "Pushing files to target server. This could take a while..."
    sudo rsync -a --stats --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /var/www/$domain $sshuser@$destination_ip:/var/www < /dev/null
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/nginx/sites-enabled/*$domain $sshuser@$destination_ip:/etc/nginx/sites-enabled < /dev/null
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/nginx/htpasswd/*$domain $sshuser@$destination_ip:/etc/nginx/htpasswd/ > /dev/null 2>&1 < /dev/null
    php_version=$(ls /etc/php/*/fpm/pool.d/$domain.conf | cut -d '\''/'\'' -f 4)
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/php/$php_version/fpm/pool.d/$domain.conf $sshuser@$destination_ip:/etc/php/$php_version/fpm/pool.d < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo mkdir -p /etc/letsencrypt/" < /dev/null
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/letsencrypt/live/$domain $sshuser@$destination_ip:/etc/letsencrypt/live > /dev/null 2>&1 < /dev/null
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/letsencrypt/archive/$domain $sshuser@$destination_ip:/etc/letsencrypt/archive > /dev/null 2>&1 < /dev/null
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" --include="*.$domain/***" --exclude='\''*'\'' /etc/letsencrypt/live/ $sshuser@$destination_ip:/etc/letsencrypt/live/ > /dev/null 2>&1 < /dev/null
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" --include="*.$domain/***" --exclude='\''*'\'' /etc/letsencrypt/archive/ $sshuser@$destination_ip:/etc/letsencrypt/archive/ > /dev/null 2>&1 < /dev/null
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/letsencrypt/options-ssl-nginx.conf $sshuser@$destination_ip:/etc/letsencrypt > /dev/null 2>&1 < /dev/null
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /etc/letsencrypt/ssl-dhparams.pem $sshuser@$destination_ip:/etc/letsencrypt > /dev/null 2>&1 < /dev/null
    sudo rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" /var/spool/cron/crontabs/$domain $sshuser@$destination_ip:/var/spool/cron/crontabs > /dev/null 2>&1 < /dev/null

    # remove ipv6only=on from our synced site if already present in a different site
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo grep -qr --exclude=*$domain ipv6only=on /etc/nginx/sites-enabled/ && sudo sed -i s/ipv6only=on//g /etc/nginx/sites-enabled/*$domain" < /dev/null

    # restart services
	echo "Restarting services on target server..."
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php5.6-fpm" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php7.1-fpm" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php7.2-fpm" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php7.3-fpm" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php7.4-fpm" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart php8.0-fpm" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart nginx" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart cron" < /dev/null

    # sync sshd config
    sudo sed -n "/Match User $user_name/,+2 p" /etc/ssh/sshd_config > ~/sshdcfg
    rsync -a --rsync-path '\''sudo -i rsync'\'' -e "ssh -o StrictHostKeyChecking=no -i ~$localuser/.ssh/id_rsa" ~/sshdcfg $sshuser@$destination_ip:~/sshdcfg < /dev/null
    sudo rm -f ~/sshdcfg
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo sed -i \"/Match User $user_name/,+2 d\" /etc/ssh/sshd_config && cat ~/sshdcfg |sudo tee -a /etc/ssh/sshd_config > /dev/null && rm -f ~/sshdcfg" < /dev/null
    ssh -o StrictHostKeyChecking=no -i "~$localuser/.ssh/id_rsa" $sshuser@$destination_ip "sudo systemctl restart sshd" < /dev/null

    echo "Site Sync Completed Successfully."
    sleep 3
    curl -sS "$site_sync_callback_url?domain=$domain&destination_server=$destination_ip&syncstatus=end" 
done < /etc/wp-site-sync.conf' |sudo tee /usr/local/bin/sync-website > /dev/null

    sudo chmod +x /usr/local/bin/sync-website

    crontab -l | grep -q 'sync-website' || (crontab -l 2>/dev/null; echo "0 4 * * * /usr/local/bin/sync-website > /dev/null 2>&1") | crontab -
	
	echo "Site sync has been scheduled."
fi

if [[ $action == "unschedule-site-sync" || $action == "4" ]]
then
	number_of_sites=$(cat /etc/wp-site-sync.conf | wc -l)
	if [ $number_of_sites -gt 0 ]
	then
		while [[ -z $domain ]]; do
			echo "Which site should we remove from syncing?"
			ls /var/www | grep -v html | nl
			echo
			read -p "Select site: " site_number
			number_of_sites=$(ls /var/www | grep -v html | wc -l)
			until [[ "$site_number" =~ ^[0-9]+$ && "$site_number" -le "$number_of_sites" ]]; do
				echo "$site_number: invalid selection."
				read -p "Select site: " site_number
			done
			domain=$(ls /var/www | grep -v html | sed -n "$site_number"p)
		done
		grep -q "$domain $destination_ip " /etc/wp-site-sync.conf
		if [ $? -eq 0 ]
		then
			sed -i "/$domain $destination_ip .*/d" /etc/wp-site-sync.conf
			number_of_sites=$(cat /etc/wp-site-sync.conf | wc -l)
			if [ $number_of_sites -eq 0 ]
			then
				(crontab -l 2>/dev/null | sed '/sync-website/d' ) | crontab -
			fi
			echo "Site sync job removed!"
		else
			echo "No such job configured with given domain and destination ip";
			exit
		fi
	else
		echo "No syncing job is configured as cron"
		exit
	fi
	echo "Schedule Site Sync For This Site Disabled"
fi
