Advertisement
speck101

terraria server world backup and restart

Jan 26th, 2025
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Terraria server shutdown and restart (optional, customize these variables)
  4. TMODLOADER_SESSION="tmodloader"
  5. SERVER_START_SCRIPT="/home/terraria/DedicatedServerUtils/manage-tModLoaderServer.sh start"
  6.  
  7. # Configuration (Customize these variables for backup)
  8. WORLD_DIR="/home/terraria/DedicatedServerUtils/Worlds"
  9. RCLONE_REMOTE="tmodworldbakup"
  10. BACKUP_DIR="serverbak/tmodworldbackup"
  11. LOG_FILE="/home/terraria/rclone_tmod_backup.log"
  12.  
  13. # Create timestamped archive directory for deleted files
  14. ARCHIVE_DIR="$RCLONE_REMOTE:serverbak/old_backups/$(date +%Y%m%d_%H%M%S)"
  15.  
  16. # Check if the world directory exists
  17. if [ ! -d "$WORLD_DIR" ]; then
  18. echo "Error: World directory '$WORLD_DIR' not found."
  19. exit 1
  20. fi
  21.  
  22. # Stop the Terraria server (optional)
  23. if [ ! -z "$TMODLOADER_SESSION" ] && [ ! -z "$SERVER_START_SCRIPT" ]; then
  24. echo "Stopping Terraria server..."
  25. screen -X -S "$TMODLOADER_SESSION" stuff "exit\n"
  26. sleep 120
  27.  
  28. # Check if the session is still running (optional)
  29. screen -list | grep -q "$TMODLOADER_SESSION"
  30. if [[ $? -eq 0 ]]; then
  31. echo " Server shutdown timed out, forcefully terminating..."
  32. screen -X -S "$TMODLOADER_SESSION" kill
  33. fi
  34.  
  35. sleep 20
  36. fi
  37.  
  38. # Perform the rclone sync
  39. echo "Backing up Terraria worlds..."
  40. rclone sync "$WORLD_DIR" "$RCLONE_REMOTE:$BACKUP_DIR" \
  41. --progress \
  42. --transfers 32 \
  43. --checkers 16 \
  44. --exclude "*.bak" \
  45. --log-file "$LOG_FILE" \
  46. --backup-dir "$ARCHIVE_DIR"
  47.  
  48. # Check the exit code of rclone
  49. if [ $? -eq 0 ]; then
  50. echo "Terraria world backup completed successfully. Log: $LOG_FILE"
  51. if [ ! -z "$SERVER_START_SCRIPT" ]; then
  52. echo "Restarting Terraria server..."
  53. screen -dmS "$TMODLOADER_SESSION" "$SERVER_START_SCRIPT"
  54. fi
  55. else
  56. echo "Error: Terraria world backup failed. Check the log: $LOG_FILE"
  57. exit 1
  58. fi
  59.  
  60. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement