Scripturi(shell-uri) FreeBSD utile serverelor de metin2

Avatar utilizator
[RaffaeL]
Software Developer

Software Developer
Mesaje: 3223
Membru din: 19 Mar 2016, 12:58
Server: ...
Aprecieri: 15  
Mulţumiri: 2502  
  Rezolvări prob.: 2
Discord: Copiază ID
 Contact:
Medalii

Inainte de a folosi oricare dintre aceste shell-uri, adaugati calea catre fisiere specifica serverului vostru!
Shell script restart core automat daca "pica"

Cod: Selectaţi tot

#!/bin/sh
# Simple auto-check/start script by [RaffaeL]
# The script only checks the PIDs without executing any command untill,
# if by any chance any core goes down it'll be restarted.

#Can be used within a cronjob(crontab) on let's say...30 minute as follows:
#/etc/crontab
#check the process pids and start it if it's down
#minute	hour	mday	month	wday	who	command
#
#*/30	*	*	*	*	root	/path/to/your script/checkpid.sh 
#

gamePath="/usr/metin2/server" # schimbati cu calea corecta catre cores

if [ ! -d "$gamePath" ]; then
    echo "$gamePath directory doesn't exist!"
    exit 1
fi

check_and_restart() {
    cd "$1"
    process="$2"
    if pgrep -f "$process" >/dev/null; then
        echo "$process is running"
    else
        echo "$process stopped"
        ./$process && sleep 5
        #mail
    fi
}

check_and_restart "$gamePath/db" "db"
check_and_restart "$gamePath/auth" "auth"
check_and_restart "$gamePath/channel1/first" "game"
check_and_restart "$gamePath/game99" "game"
/etc/crontab(global)

Cod: Selectaţi tot

#*/30	*	*	*	*	root	/path/to/your script/checkpid.sh 
#adaugati calea corecta catre script
Output core-restart:
Imagine
Back-up server(game/mysql/source)

Cod: Selectaţi tot

#!/bin/sh

globalPathGame="/usr/metin2/server"
globalPathSrc="/usr/metin2/src"
globalPathDB="/var/db/mysql"
backupBaseDir="/usr/backup"

generate_backup_filename() {
    local backup_type="$1"
    echo "${backup_type}_$(date +"%Y.%m.%d").tar.gz"
}

create_backup() {
    local source_path="$1"
    local backup_dir="$2"
    local backup_type="$3"

    mkdir -p "$backup_dir"

    backupFileName=$(generate_backup_filename "$backup_type")

    tar -czvf "$backup_dir/$backupFileName" -C "$source_path" .

    if [ $? -eq 0 ]; then
        echo "Backup successful. Archive: $backup_dir/$backupFileName"
    else
        echo "Backup failed."
    fi
}

backup_global() {
    create_backup "$globalPathGame" "$backupBaseDir/game.data" "game"
    create_backup "$globalPathSrc" "$backupBaseDir/src.data" "src"
    create_backup "$globalPathDB" "$backupBaseDir/db.data" "db"
}

backup_individual() {
    echo "Choose what to backup:"
    echo "1. Game"
    echo "2. Src"
    echo "3. Db"

    read -p "Enter the option number: " choice

    case $choice in
        1)
            create_backup "$globalPathGame" "$backupBaseDir/game.data" "game"
            ;;
        2)
            create_backup "$globalPathSrc" "$backupBaseDir/src.data" "src"
            ;;
        3)
            create_backup "$globalPathDB" "$backupBaseDir/db.data" "db"
            ;;
        *)
            echo "Invalid option."
            ;;
    esac
}

echo "Choose the backup type:"
echo "1. Global (for all)"
echo "2. Individual (for each separately)"

read -p "Enter the option number: " backupOption

case $backupOption in
    1)
        backup_global
        ;;
    2)
        backup_individual
        ;;
    *)
        echo "Invalid option."
        ;;
esac
Shell start server
Am adaugat verificare status MySQL si implicit auto-start daca este off(am avut multe mesaje cu aceasta problema...)

Cod: Selectaţi tot

#!/bin/sh

ROOT=/usr/metin2/server

start_mysql() {
    echo -e "\033[31mChecking MySQL status..\033[0m"

    if service mysql-server status | grep -q "is running"; then
        echo -e "\033[35mMySQL is already running.\033[0m"
    else
        echo -e "\033[31mMySQL is not running.\033[0m \033[33mStarting MySQL...\033[0m"
        service mysql-server start
        sleep 5

        if service mysql-server status | grep -q "is running"; then
            echo -e "\033[33mMySQL started successfully.\033[0m"
        else
            echo -e "\033[31mError starting MySQL.\033[0m"
        fi
    fi
}

start_server() {
    local path="$1"
    local name="$2"
	if [ "$name" = "db" ]; then
        echo -e "\033[31mStarting db..\033[0m"
    elif [ "$name" = "auth" ]; then
        echo -e "\033[31mStarting auth..\033[0m"
    else
        echo -e "\033[31mStarting $path..\033[0m"
    fi
    cd "$ROOT/$path" && ./"$name" &
    sleep 2
    clear
}

start_channels() {
    for channel in "$@"; do
        start_server "channel$channel/game2" "game"
        start_server "channel$channel/game1" "game"
        start_server "channel$channel/first" "game"
    done
}

db_start() {
    start_server "db" "db"
}

auth_start() {
    start_server "auth" "auth"
}

game99_start() {
    start_server "game99" "game"
}

start_mysql

while true; do
    echo -e "\033[33mHow many channels do you start??
    \033[36m1 - 1CH
    \033[36m2 - 2CH
    \033[36m3 - 3CH
    \033[36m4 - 4CH
    \033[31m0 - Cancel\033[0m"

    #echo -e "\033[34mYour choice?: \033[0m"
	printf "\033[34mYour choice?: \033[0m"

    read -r chs

    case $chs in
        1)
            db_start
            auth_start
            start_channels "$chs"
            game99_start
            echo -e "\033[36m \nServer started! CH$chs\033[0m"
            break
            ;;
        2)
            db_start
            auth_start
            start_channels "$chs"
            game99_start
            echo -e "\033[36m \nServer started! CH$chs\033[0m"
            break
            ;;
        3)
            db_start
            auth_start
            start_channels "$chs"
            game99_start
            echo -e "\033[36m \nServer started! CH$chs\033[0m"
            break
            ;;
        4)
            db_start
            auth_start
            start_channels "$chs"
            game99_start
            echo -e "\033[36m \nServer started! CH$chs\033[0m"
            break
            ;;
        0)
            echo -e "\033[31mOperation canceled.\033[0m"
            break
            ;;
        *)
            echo -e "\033[31mInvalid option. Please try again.\033[0m"
            ;;
    esac
done
Shell stop server

Cod: Selectaţi tot

#!/bin/sh

stop_server() {
    local process_name="$1"
    local process_path="$2"
	if [ "$process_name" = "db" ]; then
        echo -e "\033[33mStopping db..\033[0m"
    elif [ "$process_name" = "auth" ]; then
        echo -e "\033[33mStopping auth..\033[0m"
    else
        echo -e "\033[33mStopping $process_path..\033[0m"
    fi
    pkill -f "$process_name"

    if [ $? -eq 0 ]; then
	    if [ "$process_name" = "db" ]; then
            echo -e "\033[37mdb stopped successfully.\033[0m"
        elif [ "$process_name" = "auth" ]; then
            echo -e "\033[37mauth stopped successfully.\033[0m"
        else
            echo -e "\033[37m$process_path stopped successfully.\033[0m"
        fi
    else
        if [ "$process_name" = "db" ]; then
            echo -e "\033[31mdb is not running.\033[0m"
        elif [ "$process_name" = "auth" ]; then
            echo -e "\033[31mauth is not running.\033[0m"
        else
            echo -e "\033[31m$process_path is not running.\033[0m"
        fi
    fi
}

stop_channels() {
    for channel in "$@"; do
        stop_server "game" "channel$channel/first"
        stop_server "game" "channel$channel/game2"
        stop_server "game" "channel$channel/game1"
    done
}

stop_all() {
    stop_server "auth" "auth"
    stop_channels 1 2 3 4
    stop_server "game" "game99"
    stop_server "db" "db"
}

echo -e "\033[36mStopping the server..\033[0m"
stop_all
sleep 2
Shell clean server

Cod: Selectaţi tot

#!/bin/sh

ROOT="/usr/metin2/server"

clean_server() {
    local dir="$1"
    echo -e "\033[31mCleaning $dir..\033[0m"

    cd "$ROOT/$dir" || exit 1

    rm -fr log/*
    rm -f autorun.log PTS syslog syserr stdout
    rm -f game.core VERSION.txt DEV_LOG.log mob_count memory_usage_info.txt
    rm -f packet_info.txt p2p_packet_info.txt udp_packet_info.txt ProfileLog
    rm -f pid.game pid
}

clean_server "db"
clean_server "auth"
clean_server "channel1/first"
clean_server "channel1/game1"
clean_server "channel1/game2"
clean_server "channel2/first"
clean_server "channel2/game1"
clean_server "channel2/game2"
clean_server "channel3/first"
clean_server "channel3/game1"
clean_server "channel3/game2"
clean_server "channel4/first"
clean_server "channel4/game1"
clean_server "channel4/game2"
clean_server "game99"

sleep 2
echo -e "\033[32mServer cleaned!\033[0m"
In cazul in care vreti sa folositi start/stop/clean/backup ca si comenzi globale,
doar creati in /bin symlink-uri catre fisierele propriuzise si da-ti-le permisiuni de executare.

Cod: Selectaţi tot

ln -s /usr/metin2/server/start_server.sh /bin/start
ln -s /usr/metin2/server/stop_server.sh /bin/serverstop
ln -s /usr/metin2/server/clean_server.sh /bin/clean
ln -s /usr/metin2/server/backup_server.sh /bin/backup


chmod +x /bin/start /bin/stop /bin/clean /bin/backup
ATENTIE! Nu folositi "stop" ca nume de comanda, pentru ca nu va functiona fara argumente.
Folositi ca in exemplul meu: serverstop sau stopserver...
Script schimbare automata a parolei MySQL

Cod: Selectaţi tot

#!/bin/sh

RED='\033[0;31m'
GREEN='\033[0;32m'
AQUA='\033[0;36m'
NC='\033[0m'

stty -echo
printf "${AQUA}Enter new MySQL root password: ${NC}"
read new_password
stty echo
printf "\n"

stty -echo
printf "${AQUA}Confirm new MySQL root password: ${NC}"
read confirm_password
stty echo
printf "\n"

if [ "$new_password" != "$confirm_password" ]; then
    echo -e "\033[0;31mPasswords do not match. Exiting.\033[0m"
    exit 1
fi

if ! command -v mysql > /dev/null; then
    echo "\033[0;31mMySQL client is not installed. Please install it and try again.\033[0m"
    exit 1
fi

/usr/local/etc/rc.d/mysql-server stop
mysqld_safe --skip-grant-tables &
sleep 5
mysql -u root <<EOF
use mysql;
update user set password=PASSWORD('$new_password') where User='root';
flush privileges;
quit
EOF
/usr/local/etc/rc.d/mysql-server stop
/usr/local/etc/rc.d/mysql-server start

echo -e "\033[0;31mMySQL root password changed successfully.\033[0m"

exit 0

Daca mai aveti alte idei...
Pentru acest mesaj, [RaffaeL] a primit 5 (de) mulţumiri
Delusionasfc • D a e m o n • George. • [Demonul] • dwofling
Imagine
Imagine
https://www.youtube.com/watch?v=GhYkCIETEto
Do everything, if you know what you're doing, else... Imagine
Ma gasesti si aici: Discord https://discord.gg/5J5GnyZVSR
Afișează semnătura
Răspunde
Răspunsuri
1
Vizualizări
1082
Răspunsuri
1
Vizualizări
300

Înapoi la “FreeBSD”

  • Informaţie
  • Cine este conectat

    Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 1 vizitator