Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######################################################################
  2. ##                        Configuration                             ##
  3. ######################################################################
  4. #  Rotate logs, check for Java, execute server continuously          #
  5. #  Version 1, written by AssMuncher                                  #
  6. #                                                                    #
  7. #  Features:                                                         #
  8. #  * Automatically restarts on Crash                                 #
  9. #  * Backs up world before restarting                                #
  10. #  * Exit loop if configured player is banned                        #
  11. #                                                                    #
  12. ######################################################################
  13. #  Set these configuration options                                   #
  14. ######################################################################
  15.  
  16. #Uncomment one of these and set your path to the java.exe and override
  17. #  the JAVA_HOME environment variable (if it exists).
  18.  
  19. #$Java = "C:\Program Files (x86)\Java\jre6\bin\java.exe"
  20. #$Java = "C:\Program Files\Java\jre6\bin\java.exe"
  21. $JavaCLI = "-Xmx1536M -Xms512M"
  22.  
  23. #This is the secret "player" you should ban to make the server die. The
  24. #  script will look thru the banned-players.txt file to see if this exists
  25. #  if it does, it will NOT continue to loop.
  26.  
  27. #Also, you'll probably have to change this each time if you want it to be
  28. #  secure and have untrusted users. I run a small server for a small group
  29. #  of friends, so this suffices.
  30. $Secret = "loopserver-shutdown-1111"
  31.  
  32. #If you want to use the backup feature, please uncomment this and feed it
  33. #  a path to 7-zip's 7z.exe. Backups will be made in the current directory
  34. #  and named by date/time.
  35. $Backup = "C:\Program Files\7-Zip\7z.exe"
  36.  
  37. ######################################################################
  38. ##                       Script Follows                             ##
  39. ######################################################################
  40.  
  41. function Pause() {
  42.     $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  43. }
  44.  
  45. if($Env:JAVA_HOME -and !$Java) {
  46.     $Java = "$Env:JAVA_HOME\bin\java.exe"
  47. }
  48. elseif(!$Env:JAVA_HOME -and !$Java) {
  49.     Write-Host -foregroundcolor red "Please configure `$Java, your JAVA_HOME is not set or incorrect."
  50.     Pause
  51.     Exit
  52. }
  53.  
  54. do {
  55.     if(!(Test-Path ".\minecraft_server.jar")) {
  56.         Write-Host -foregroundcolor red "Minecraft server JAR not found!"
  57.         Pause
  58.         Exit
  59.     }
  60.    
  61.     if(!(Test-Path $Java)) {
  62.         Write-Host -foregroundcolor red "Unable to locate suitable JRE!"
  63.         Pause
  64.         Exit
  65.     }
  66.    
  67.     $cmd = '"' + $Java + '" ' + $JavaCLI + ' -jar minecraft_server.jar nogui'
  68.     cmd /c $cmd
  69.     $ReturnValue = $LastExitCode
  70.  
  71.     Write-Host -foregroundcolor green "Rotating logs..."
  72.     if(Test-Path ".\server.5.log") { Remove-Item ".\server.5.log" }
  73.     if(Test-Path ".\server.4.log") { Rename-Item ".\server.4.log" ".\server.5.log" }
  74.     if(Test-Path ".\server.3.log") { Rename-Item ".\server.3.log" ".\server.4.log" }
  75.     if(Test-Path ".\server.2.log") { Rename-Item ".\server.2.log" ".\server.3.log" }
  76.     if(Test-Path ".\server.1.log") { Rename-Item ".\server.1.log" ".\server.2.log" }
  77.     if(Test-Path ".\server.log")   { Rename-Item ".\server.log"   ".\server.1.log" }
  78.    
  79.     #Backing up scripts
  80.     if($Backup -and (Test-Path $Backup)) {
  81.         Write-Host -foregroundcolor green "Backing up configured world files..."
  82.  
  83.         $Props = ''
  84.         Get-Content ".\server.properties" | ForEach-Object {$Props = @{}} {
  85.             $Props[$_.split('=')[0]] = $_.split('=')[1]
  86.         }
  87.         $cmd = '"' + $Backup + '" a ' + $Props.'level-name'
  88.        
  89.         if($ReturnValue -ne 0) {
  90.             $cmd = $cmd + '.crash'
  91.             Write-Host -foregroundcolor yellow "Crash Detected!"
  92.         }
  93.        
  94.         $cmd = $cmd + '.backup-' + (Get-Date -format MMddyyyy\-HHmmss) + '.7z '
  95.         $cmd = $cmd + $Props.'level-name' + ' -r'
  96.  
  97.         cmd /c $cmd
  98.        
  99.         if($LastExitCode -ne 0) {
  100.             Write-Host -foregroundcolor yellow "Backup exited abnormally!"
  101.         }
  102.     }
  103. }
  104. while(!(Get-ChildItem .\banned-players.txt | Select-String -SimpleMatch $Secret))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement