Last active
May 1, 2021 17:32
-
-
Save benfiratkaya/bfe1ec730d085292f7dc63b612ae7ec5 to your computer and use it in GitHub Desktop.
Minecraft server backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
## | |
## Author: Firat Kaya | |
## www.leaderos.com.tr | |
## | |
function send_console_command() { | |
screen -R $1 -X stuff "$2 $(printf '\r')" | |
} | |
function backup() { | |
## $1 = Yedek Adı | |
## $2 = Yedek Klasoru | |
send_console_command $1 "say Sunucu yedekleniyor! Bu işlem birkaç dakika sürebilir!" | |
echo "$1 sunucusu kayit ediliyor..." | |
send_console_command $1 "save-off" | |
send_console_command $1 "save-all" | |
backup_time=$(date +%H%M%S) | |
backup_file="$1-$backup_time" | |
echo "$1 sunucusu yedekleniyor..." | |
zip -r $backup_file.zip $2 | |
echo "$1 sunucusu yedeklendi!" | |
send_console_command $1 "say Sunucu yedeklemesi tamamlandı!" | |
for i in 5 4 3 2 1 | |
do | |
send_console_command $1 "say Sunucu $i saniye içinde yeniden başlatılacak!" | |
sleep 1 | |
done | |
echo "$1 sunucusu yeniden baslatiliyor..." | |
send_console_command $1 "save-on" | |
send_console_command $1 "save-all" | |
send_console_command $1 "stop" ## sunucunuz kapanınca otomatik başlamıyorsa "stop" yerine "restart" yazın. | |
} | |
function backup_db() { | |
## $1 = Veritabanı Tipi | Desteklenenler: mysql | |
## $2 = Veritabanı IP (localhost) | |
## $3 = Veritabanı Port (3306) | |
## $4 = Veritabanı Kullanıcı Adı | |
## $5 = Veritabanı Şifresi | |
## $6 = Veritabanı Adı | |
backup_time=$(date +%H%M%S) | |
backup_file="$1-$4_$6-$backup_time" | |
echo "$1 verileri yedekleniyor... (K: $4 - DB: $6)" | |
if [ $1 = "mysql" ] | |
then | |
mysqldump -P $3 -h $2 -u $4 -p$5 $6 > "$backup_file.sql" | |
echo "$1 verileri yedeklendi! (K: $4 - DB: $6)" | |
else | |
echo "Desteklenmeyen veritabani!" | |
fi | |
} | |
function remove_old_backups() { | |
## $1 = Yedek Dizini | |
## $2 = Max Yedek Gün Sayısı | |
backups=() | |
while IFS= read -rd '' dir; do backups+=("$dir") | |
done < <(find $backup_dir/* -maxdepth 0 -type d -print0) | |
if [[ ${#backups[@]} -gt $2 ]] | |
then | |
i=1 | |
for value in "${backups[@]}" | |
do | |
if [[ ${#backups[@]}-$2 -ge $i ]] | |
then | |
echo "$value adli eski yedek siliniyor..." | |
rm -rf $value | |
echo "$value adli eski yedek silindi!" | |
((i=i+1)) | |
else | |
break | |
fi | |
done | |
fi | |
} | |
## Ayarlar | |
backup_dir="/home/backups" | |
max_backup_count=7 # En fazla X gun yedek alsın | |
backup_date=$(date +%Y-%m-%d) | |
## Yedek dizinine git | |
cd $backup_dir | |
## Guncel tarihe ait backup klasoru olustur | |
mkdir -p $backup_date | |
cd $backup_date | |
## Yedek: MySQL | |
backup_db "mysql" "localhost" "3306" "root" "password" "leaderos" | |
## Yedek: Factions | |
backup "factions" "/home/servers/Factions/" | |
## Yedek: SkyBlock | |
backup "skyblocks" "/home/servers/SkyBlocks/" | |
## Eski Backupları sil | |
remove_old_backups $backup_dir $max_backup_count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment