Advertisement
Guest User

auto-sync.sh

a guest
Sep 7th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.61 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #   When this script is launched it will:
  4. #       1) log into the L4dgoldminers/GMservers github repo and download the server config locally
  5. #       2) Make sure the local server config is up-to-date with the github config
  6. #       3) Log any errors or rsync updates to githubsync_rsyncUpdates.log & githubsync_errors.log
  7. #       4) After it's done it'll correct the server.cfg hostname & rcon_password fields, as well as databases.cfg passwords
  8. #               Note: this script assumes all passwords are the same in database.cfg
  9. #
  10. #       Installation:
  11. #           change file permission: 'chmod 755 auto-sync.sh'
  12. #           Run script manually first time to make sure it's working.
  13. #               (This means refreshing the directory and checking those two log files)
  14. #
  15. #           After verifying it's working properly, install on a cron job that executes once a day:
  16. #               Enter the command 'crontab -e' (select "nano" if you're unfamiliar with using the other editors)
  17. #               0 8 * * * /path/to/auto-sync.sh # Launch auto-sync once per day
  18. #
  19. #           As an additional step to be sure the cronjob is launching it properly, delete the "githubsync_rsyncUpdates.log"
  20. #               log file and see if it regenerates automatically the next day.
  21. #
  22. #==================================================================
  23. # configuration :
  24.  
  25. #github credentials
  26. github_user=""
  27. github_pass="" # use a personal access token, passwords will be deprecated
  28.  
  29. #L4D2 server credentials
  30. server_hostname="[GM] "
  31. server_rcon_password=""
  32. mysql_password="" # assumes every password in databases.cfg is the same.
  33.  
  34. # linux server directory (leave out trailing slash '/')
  35. local_dir="/root/L4D2/left4dead2" #directory where 'addons/' and 'cfg/' folders exist
  36.  
  37. #log file for errors / update status
  38. LogFile="githubsync_rsyncUpdates.log"
  39. ErrorFile="githubsync_errors.log"
  40.  
  41. # Exclude these files from being synced to the 'addons/' folder
  42. Excludes_addons="--exclude sourcemod/configs/sourcebans/sourcebans.cfg \
  43. --exclude sourcemod/configs/sourcebans/sb_admin_groups.cfg \
  44. --exclude sourcemod/configs/sourcebans/sb_admins.cfg \
  45. --exclude sourcemod/data/SourceTVDemos \
  46. --exclude sourcemod/logs \
  47. --exclude sourcemod/gamedata/core.games \
  48. --exclude sourcemod/gamedata/sdkhooks.games \
  49. --exclude sourcemod/gamedata/sdktools.games \
  50. --exclude sourcemod/gamedata/sm-cstrike.games \
  51. --exclude sourcemod/gamedata/funcommands.games.txt \
  52. --exclude sourcemod/gamedata/sm-tf2.games.txt \
  53. --exclude sourcemod/gamedata/accelerator.games.txt \
  54. --exclude sourcemod/configs/sourcebans/overrides_backup.cfg \
  55. --exclude sourcemod/data/sqlite/sourcecomms-queue.sq3 \
  56. --exclude sourcemod/data/dumps \
  57. --exclude sourcemod/data/sqlite/sourcebans-queue.sq3"
  58.  
  59. # Exclude these files from being synced to the 'cfg/sourcemod' folder
  60. Excludes_cfg_sourcemod="--exclude Sm_SourceSleuth.cfg"
  61.  
  62. # END configuration
  63. #==================================================================
  64.  
  65. date=$(date '+%Y-%m-%d:%H:%M')
  66.  
  67. LogCriticalError () {
  68.     echo -e "\n==================== $date ====================\n" >> $ErrorFile
  69.     echo "Error: $1" >> $ErrorFile
  70.     echo "Error: $1"
  71.     rm -rf GMservers
  72.     exit
  73. }
  74.  
  75. # Might be paranoid check. Just in case someone has a folder already named that and is using it...
  76. [ -d "GMservers/" ] && LogCriticalError "Folder named 'GMservers/' already exists in this directory."
  77.  
  78. if ! git clone https://$github_user:$github_pass@github.com/L4dgoldminers/GMservers.git
  79. then
  80.     LogCriticalError "git clone failed. Check 'github credentials' configuration."
  81. fi
  82.  
  83. databasesCFG="$local_dir/addons/sourcemod/configs/databases.cfg"
  84. serverCFG="$local_dir/cfg/server.cfg"
  85. addons_folder="$local_dir/addons/" #keep trailing slash (/) at end
  86. cfg_folder="$local_dir/cfg/" #keep trailing slash (/) at end
  87. cfg_sourcemod_folder="$local_dir/cfg/sourcemod/" #keep trailing slash (/) at end
  88.  
  89. # validate folders
  90. if [ "$1" == "newserver" ]; then
  91.     echo "Validating relevant folders for new install of L4D2.." && sleep 3
  92.    
  93.     [ ! -d "$addons_folder" ] && LogCriticalError "'addons/' folder doesn't exist. Check 'local_dir' variable."
  94.     [ ! -d "$cfg_folder" ] && LogCriticalError "'cfg/' folder doesn't exist. Check 'local_dir' variable."
  95. else
  96.     [ ! -d "$addons_folder" ] && LogCriticalError "'addons/' folder doesn't exist. Check 'local_dir' variable."
  97.     [ ! -d "$cfg_sourcemod_folder" ] && LogCriticalError "'cfg/sourcemod/' folder doesn't exist. Check 'local_dir' variable."
  98.     [ ! -e "$databasesCFG" ] && LogCriticalError "'$databasesCFG' file doesn't exist. Check 'local_dir' variable."
  99.     [ ! -e "$serverCFG" ] && LogCriticalError "'$serverCFG' file doesn't exist. Check 'local_dir' variable."
  100. fi
  101.  
  102. # validate variables
  103. [ -z "$server_hostname" ] && LogCriticalError "'server_hostname' variable left blank."
  104. [ -z "$server_rcon_password" ] && LogCriticalError "'server_rcon_password' variable left blank."
  105. [ -z "$mysql_password" ] && LogCriticalError "'mysql_password' variable left blank."
  106. [ -z "$LogFile" ] && LogCriticalError "'LogFile' variable left blank."
  107. [ -z "$ErrorFile" ] && LogCriticalError "'ErrorFile' variable left blank."
  108.  
  109. remote_dir_addons="$(pwd)/GMservers/Server Config/addons/" #keep trailing slash (/) at end
  110. remote_dir_cfg_sourcemod="$(pwd)/GMservers/Server Config/cfg/sourcemod/" #keep trailing slash (/) at end
  111. remote_dir_serverCFG="$(pwd)/GMservers/Server Config/cfg/server.cfg"
  112.  
  113. Opts="-ahiO --no-perms --no-owner --no-group"
  114.  
  115. # log file starts here
  116. echo -e "\n==================== $date ====================\n\nUpdate check started." >> $LogFile
  117.  
  118. if [ "$1" == "newserver" ]; then
  119.     echo "Rsyncing server config to new install of L4D2" && sleep 3
  120.    
  121.     # rsync 'addons/' folder
  122.     rsync $Opts "$remote_dir_addons" "$addons_folder" >> $LogFile
  123.     # rsync 'cfg/sourcemod/' folder (note: don't rsync 'cfg/' by itself cause it would delete official l4d2 files within cfg/)
  124.     rsync $Opts "$remote_dir_cfg_sourcemod" "$cfg_sourcemod_folder" >> $LogFile
  125. else
  126.     # rsync 'addons/' folder
  127.     rsync $Opts --delete $Excludes_addons "$remote_dir_addons" "$addons_folder" >> $LogFile
  128.  
  129.     # rsync 'cfg/sourcemod/' folder (note: don't rsync 'cfg/' by itself cause it would delete official l4d2 files within cfg/)
  130.     rsync $Opts --delete $Excludes_cfg_sourcemod "$remote_dir_cfg_sourcemod" "$cfg_sourcemod_folder" >> $LogFile
  131. fi
  132.  
  133. #server.cfg - replace hostname and rcon_password with correct entries
  134. mv "$remote_dir_serverCFG" "$serverCFG"
  135. sed -i "s/RCON_PASS_HERE/$server_rcon_password/" $serverCFG
  136. sed -i "s/SERVER_HOSTNAME_HERE/$server_hostname/" $serverCFG
  137.  
  138. # replace MySQL password in databases.cfg (do this after rsync completes)
  139. sed -i "s/MYSQL_PASS/$mysql_password/" $databasesCFG
  140.  
  141. rm -rf GMservers
  142.  
  143. # Remove redundant text from log file
  144. sed -i "/\.\.t\.\.\.\.\.\./d" $LogFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement