[Solved] Creating a Batch File to Backup Game Server

I need some pointers, or a push in the right direction to figure out how to write a batch file to backup my 7 Days to Die server

What I want to do is have the batch file save my server game data, and then copy the files to a new drive, essentially creating stampeded backups.

I’m very new to this, and my problem is having the batch file save my data.

So I have a “Save” batch file.
telnet localhost <Port#>
#Timeout here? Probably not needed
@echo off
echo saveworld

In order to save a 7 Days game server, you telnet into it and simply type saveworld. My problem is is that once the batch file telnets, it will not type the saveworld command.

From a few Google searches, people said a batch file cannot input any commands after it telnets, they say I might need a powershell script. Is this true?

But my backup works fine after I manually save the data!

mkdir 7daysserverbackup
xcopy /E /K /Y “Server File Path” "Backup File Path"
ren “7daysserverbackup” “%date:/=-% %time::=-%”

This batch file creates a folder, copies the files I want into it, and renames it with a time stamp.
Is there a way I can have it zip the folder as well?

Thanks you for any input I don’t need anyone to tell me exactly what I need to write, but a link to a website would be great! Unless you’re bored and want something to do :wink:

Some of MS’s default cli apps are not scripting friendly. The workarounds are to use powershell/AutoIT to automate them anyway, or use a third party app that is scripting friendly. Programmers really like powershell because it is an interpreted language designed for programmers, but you do not actually ever need to use it. Here is a third party app:

Telnet: http://support.moonpoint.com/downloads/windows/network/Telnet/tst10.php

tst10.exe --help

Usage Syntax:

tst10.exe /r:script.txt [options]

/r:script.txt      run script.txt
[options]          any of these:

/o:output.txt      send session output to output.txt
/m                 run script in minimized window

Usage Example:

tst10.exe /r:script.txt /o:output.txt /m

Scripting Syntax:

HOSTNAME PORT      port number optional, default: 23
WAIT "string"      string to wait for
SEND "string"      string to send
\"                 represents the a quote character
\m                 represents a <CR/LF>
\\                 represents the backslash character

Scripting Example:

hostname.com 23
WAIT "login"
SEND "root\m"
WAIT "password"
SEND "mypassword\m"
WAIT ">"
SEND "dip internet.dip\m"
WAIT ">"

Scripting Note:

You can start with either WAIT or SEND commands,
but you *must* alternate them. ie: you can't use two
or more WAIT or SEND in a row.

Note:

TST will disconnect and close as soon
as its done with the last entry of the script.

If you need to, you can type in the terminal
window while the script is running.

For ziping stuff, use 7zip. 7-zip should really be installed on every Windows instance.
"C:\Program Files\7-Zip\7z.exe" --help

7-Zip [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04

Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]
       [<@listfiles...>]

<Commands>
  a : Add files to archive
  b : Benchmark
  d : Delete files from archive
  e : Extract files from archive (without using directory names)
  h : Calculate hash values for files
  i : Show information about supported formats
  l : List contents of archive
  rn : Rename files in archive
  t : Test integrity of archive
  u : Update files to archive
  x : eXtract files with full paths

<Switches>
  -- : Stop switches parsing
  -ai[r[-|0]]{@listfile|!wildcard} : Include archives
  -ax[r[-|0]]{@listfile|!wildcard} : eXclude archives
  -ao{a|s|t|u} : set Overwrite mode
  -an : disable archive_name field
  -bb[0-3] : set output log level
  -bd : disable progress indicator
  -bs{o|e|p}{0|1|2} : set output stream for output/error/progress line
  -bt : show execution time statistics
  -i[r[-|0]]{@listfile|!wildcard} : Include filenames
  -m{Parameters} : set compression Method
    -mmt[N] : set number of CPU threads
  -o{Directory} : set Output directory
  -p{Password} : set Password
  -r[-|0] : Recurse subdirectories
  -sa{a|e|s} : set Archive name mode
  -scc{UTF-8|WIN|DOS} : set charset for for console input/output
  -scs{UTF-8|UTF-16LE|UTF-16BE|WIN|DOS|{id}} : set charset for list files
  -scrc[CRC32|CRC64|SHA1|SHA256|*] : set hash function for x, e, h commands
  -sdel : delete files after compression
  -seml[.] : send archive by email
  -sfx[{name}] : Create SFX archive
  -si[{name}] : read data from stdin
  -slp : set Large Pages mode
  -slt : show technical information for l (List) command
  -snh : store hard links as links
  -snl : store symbolic links as links
  -sni : store NT security information
  -sns[-] : store NTFS alternate streams
  -so : write data to stdout
  -spd : disable wildcard matching for file names
  -spe : eliminate duplication of root folder for extract command
  -spf : use fully qualified file paths
  -ssc[-] : set sensitive case mode
  -ssw : compress shared files
  -stl : set archive timestamp from the most recently modified file
  -stm{HexMask} : set CPU thread affinity mask (hexadecimal number)
  -stx{Type} : exclude archive type
  -t{Type} : Set type of archive
  -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName] : Update options
  -v{Size}[b|k|m|g] : Create volumes
  -w[{path}] : assign Work directory. Empty path means a temporary directory
  -x[r[-|0]]{@listfile|!wildcard} : eXclude filenames
  -y : assume Yes on all queries

So maybe like:

set sevenzip=C:\Program Files\7-Zip\7z.exe
set folderName=%%date:/=-%% %%time::=-%%

"%sevenzip%" a "%folderName%.zip" "%folderName%"

Further reading/random notes:

  • Use the set command to work with variables.
    • >set favFood=pie
    • >echo %favFood%
    • >pie
  • Sometimes %'s need to be doubled up %% in batch files depending on the command.
  • For timestamps, a sane scheme is to take the european format (dd.mm.yyyy) and reverse it to yyyy.mm.dd that way they computers sort the entries properly. So like 2019.12.04
  • Batch/cmd reference: https://ss64.com/nt/
  • Code legibility guide: https://icodealot.com/batch-scripts-can-not-suck/
3 Likes

Thank you for all of this information!

I’m currently in a game, so I have not read most of it. But I will be back shortly.

Bah! I can’t get the program to work :frowning:

I created this batch file:

cd E:\7days
TST10.exe /r:telnetsave.txt\

It doesn’t seem to do anything. I’ve also tried removing the CD portion and dropping the .bat folder into the 7 days folder where I have my tst10.exe fil.

When i double click on tst10.exe it shows a window that has all of the information about the tool.

When I close TST10.exe, it gives me the windows error “TST.10.exe has stopped working”

Is there a compatibility issue with windows 10?

Ionu, maybe. Try fixing the syntax issues however. Win 10 should also have a compatibility mode.

Before writting the batch file, try to understand the exact syntax the program needs correctly by opening a command prompt and manually typing out the command. Once you get the syntax issues solved, it should then be simple enough to script it.

Random:

  • When changing drives with cd use the /d flag like so: cd /d E:\7days. Alternatively, use pushd.
  • /r:telnetsave.txt\ does not need that last \. Try /r:telnetsave.txt.
  • Try to always use the full paths. There are some wildcards like %appdata% and %userprofile% that you can use as well. Type set without any additional arguments to see a partial list.
set savePath=E:\7days
set telnetPath=C:\Users\User\Downloads\TST10\TST10.exe
set telnetConfigFile=C:\Users\User\Downloads\TST10\telnetsave.txt

cd /d "%savepath%"

"%telnetPath%" /r:"%telnetConfigFile%"
1 Like

I’ll take a look at my syntax issues. I must have hit backslash and enter at the same time, because I do not have .txt\ in my batch files.

I’ll take another crack at it with this new info, thanks!

Wish me luck.

Edit: after a quick google search, I had no idea I needed the /dafter cd to change drive and folder at the same time. I thought cd was all that was needed!

Oh man, what a journey.

So this telnet script thing was not working for me. I have no idea what I’m doing wrong. I tried following as much advise from you as I could, as well as some reading from the links you provided.

So I thought I would try puTTY and autopuTTY, I forgot you can’t really execute autopuTTY and the followup with telnet commands! Maybe you can, I’m not aware of it. I’m pretty sure you can with SSH though.

I figured out a different way to do this, and hot diggity damn it works!

I Wrote a batch file:

for /f %%x in (‘wmic path win32_localtime get /format:list ^| findstr “=”’) do set %%x
for /f “tokens=1-2 delims=/: " %%a in (”%TIME%") do (if %%a LSS 10 (set mytime=0%%a%%b) else (set mytime=%%a%%b))
set today=%Year%-%Month%-%Day%
mkdir 7daybu
serversave.wsf
TIMEOUT /t 2
xcopy /E /K /Y “Server Path” "E:\7days\7daybu"
ren “7daybu” “%today% %mytime%”

I can’t seem to wrap my head around tokens, delims and puling dates/time, but I found a few good readings on stackoverflow.

I did a bunch of research into VBscrips to assist me with telnet commands.
serversave.wfs contains the following:

<job>
<script language="VBScript">
Option Explicit
On Error Resume Next
Dim WshShell
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
WScript.Sleep 1000
WshShell.SendKeys "telnet localhost port#"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 2000
WshShell.SendKeys "saveworld"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WScript.Quit 
</script>
</job>

Woo was that part fun. I never knew a lick of VB scripts until today. I still don’t understand the first portion of the script that launches cmd.exe. Running any code that you don’t understand is foolish, but enough forums pointed to it being safe.

The next step is to zip the file. Here I go!

Edit 30min later.

My new bacukp batchfile that does everything I need!

for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x
for /f "tokens=1-2 delims=/: " %%a in ("%TIME%") do (if %%a LSS 10 (set mytime=0%%a%%b) else (set mytime=%%a%%b))
set today=%Year%-%Month%-%Day%
set timestamp=%today% %mytime%
mkdir 7daybu
serversave.wsf
TIMEOUT /t 2
xcopy /E /K /Y C:\Filepath" "E:\7days\7daybu"
7za a -tzip "%timestamp%.zip" 7daybu
rmdir E:\7days\7daybu /s /q

I can sleep now! What a fun and frustrating experience this was.

10/10 would do it again.

I’m sure I can clean it up a bit, maybe set more variables, just for practice.

Thank you Peanut 253, I owe you a beer.

2 Likes

Congrats on getting it to work.

If you would do it again then I recommend AutoIT over powershell/vbscript. It is far more intuitive, at least to me, and allows you to record clicks and keyboard presses and edit them as text.

Congrats again and sleep well!