Backup & Restore Scripts

From TCAdmin 2.0 Documentation

Configure the game

Secure your Backups folder

This is the most important part of the configuration. If you skip this step you will leave your server vulnerable to exploits.

  • Go to Settings > Games > Select the game > File System Permissions.
  • Expand User Files and select Add permissions by subdirectory.
  • Configure these values:
    • Subdirectory Path: Backups
    • Permissions:
      • If you don't want to allow manual download of the backup files set it to "no permissions".
      • If you want to allow the user to download and delete backups set it to "Basic permissions" and check "Read" and "Delete". DO NOT check "write".
    • Click on Add.
    • Add the same permission for "Sub Admin Files" and "Reseller Files".
    • Click on Save.

Create the variable

  • Configure a variable used by the restore script. Go to Settings > Games > Select the game > Variables (it can also be a global variable). Create this variable:
    • Name: BackupFile
    • Script parameter: Checked
    • Admin access: Checked
    • Sub admin access: Checked
    • Reseller access: Checked
    • User access: Checked
    • Server owner access: Checked
    • Value is required: Checked
    • Required Message: The backup file is required.
    • Item Type: Combobox
    • Label: Backup:
    • Description: Select the backup to restore.
    • Source: File System
    • Filter Type: Files
    • Filter: Backups/*.zip
    • Item Value: Full path
    • Item Display: Name (no extension)

Create the scripts

  • Go back to the game's main settings. Click on the Custom Scripts icon. Add the following scripts. They can also be configured as global scripts.
Operating System: Any
Name: Backup Your Settings
Description: Create a backup of your game server's settings.
Script Engine: IronPython
Event: Custom Icon
Sub admin access: Checked
Reseller access: Checked
User access: Checked
Server owner access: Checked
Stop service before executing: Check if you want to stop the game server before creating the backup.
Script: Configure BACKUP_LOCATION, MAX_BACKUPS, BACKUP_EXTENSIONS, FOLDER_TO_BACKUP as needed. If you change the backup location remember to update your file system permissions.
import clr;
clr.AddReference("TCAdmin.SDK");

import System;
from System import Array, String, DateTime, Exception, Environment, PlatformID
from System.IO import Path, DirectoryInfo, SearchOption
from System.Collections.Generic import List
from System.Globalization import CultureInfo
from TCAdmin.SDK.Misc import CompressionTools, Linux, Windows

clr.AddReference("System.Core")
clr.ImportExtensions(System.Linq)

# Specify where backups are saved.
# Default location is a folder named Backups in the game server's root.
BACKUP_LOCATION = Path.Combine(ThisService.RootDirectory, "Backups")

# Specify the maximum number of backups to keep.
MAX_BACKUPS = 5

# Extension of files that will be included in the backup.
# Only specify config file extensions to keep file size small. Avoid log extensions.
# For security do not backup executables (.exe, .dll or .so, etc). Use ".*" to backup all extensions in the specified folder.
BACKUP_EXTENSIONS = Array[str]([".cfg", ".lua", ".properties", ".txt"])

# Extensions that are excluded from the backup. (only used if you specify ".*" in BACKUP_EXTENSIONS)
BACKUP_EXCLUDE_EXTENSIONS = Array[str]([".xxx"])

backupfilename = String.Format("{0}.zip", DateTime.Now.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture))
backupfiles = List[str]()

####################################################################################################################################### 
# COPY THIS BLOCK IF YOU WANT TO BACKUP MORE THAN 1 PATH ##############################################################################
#######################################################################################################################################
# Specify the folder to backup. Sub folders will be included in the backup.
# All files with the extensions specified in BACKUP_EXTENSIONS will be added to the backup.
# Set value to ThisService.RootDirectory if you want to backup all specified extensions in the game server's root folder and sub folders.
# This example will backup the path RootDirectory\garrysmod\cfg
FOLDER_TO_BACKUP = Path.Combine(ThisService.RootDirectory, "garrysmod", "cfg")
backupdir = DirectoryInfo(FOLDER_TO_BACKUP)
allfiles = backupdir.GetFiles("*", SearchOption.AllDirectories)
for file in allfiles:
  if not file.FullName.StartsWith(BACKUP_LOCATION) :
    if Array.IndexOf(BACKUP_EXTENSIONS, file.Extension) != -1 or (Array.IndexOf(BACKUP_EXTENSIONS, ".*") != -1 and Array.IndexOf(BACKUP_EXCLUDE_EXTENSIONS, file.Extension) == -1 ) :
      backupfiles.Add(file.FullName.Replace(ThisService.RootDirectory, String.Empty).TrimStart(Path.DirectorySeparatorChar))
####################################################################################################################################### 
#######################################################################################################################################
   
if backupfiles.Count == 0 :
  raise Exception("Didn\'t find any files to backup.")

Script.WriteToConsole(String.Format("Backing up {0} files to {1}...", backupfiles.Count, backupfilename))
 
backuplocation = DirectoryInfo(BACKUP_LOCATION)
if not backuplocation.Exists :
  backuplocation.Create()

compressiontools = CompressionTools()
compressiontools.Compress(ThisService.RootDirectory, backupfiles.ToArray(), Path.Combine(BACKUP_LOCATION, backupfilename))
Script.WriteToConsole("The backup was created successfully.")

backupfiles=backuplocation.GetFiles("*.zip").OrderBy(lambda f: f.CreationTime).ToArray()
for i in range(0,backupfiles.Count - MAX_BACKUPS):
  backupfiles[i].Delete()
  Script.WriteToConsole(String.Format ("Deleted previous backup: {0}", backupfiles[i].Name))

if Environment.OSVersion.Platform == PlatformID.Unix :
  if Linux.IsRoot() :
    Linux.SetDirectoryOwnerAutoDetect(BACKUP_LOCATION, True);
else :
 owner = Windows.GetOwner(ThisService.RootDirectory);
 Windows.SetDirectoryOwner(BACKUP_LOCATION, str(owner), True);
Operating System: Any
Name: Restore Your Settings
Description: Restore your game server's settings from a backup.
Script Engine: IronPython
Event: Custom Icon
Prompt for variable values: Checked
Sub admin access: Checked
Reseller access: Checked
User access: Checked
Server owner access: Checked
Stop service before executing: Check if you want to stop the game server before restoring the backup.
Script:
import clr;
clr.AddReference("TCAdmin.SDK");

from System.IO import Path
from System import String, Environment, PlatformID
from TCAdmin.SDK.Misc import CompressionTools, Linux, Windows

Script.WriteToConsole(String.Format("Restoring backup {0}...", Path.GetFileNameWithoutExtension(ThisService.Variables["BackupFile"])))

compressiontools = CompressionTools()
compressiontools.Decompress(ThisService.Variables["BackupFile"], ThisService.RootDirectory)

if Environment.OSVersion.Platform == PlatformID.Unix :
  if Linux.IsRoot() :
    Linux.SetDirectoryOwnerAutoDetect(ThisService.RootDirectory, True);
else:
 owner = Windows.GetOwner(ThisService.RootDirectory);
 Windows.SetDirectoryOwner(ThisService.RootDirectory, str(owner), True);

Script.WriteToConsole("The backup was restored successfully.")


Operating System: Any
Name: Keep backups before reinstall
Description: Moves backups outside of root.
Script Engine: IronPython
Event: Before reinstall
Ignore execution errors: Unchecked (if it fails you don't want to lose the backups)
Script: Configure BACKUP_LOCATION
import clr;

from System import String
from System.IO import Path, Directory

# Specify where backups are saved.
# Default location is a folder named Backups in the game server's root.
BACKUP_LOCATION = Path.Combine(ThisService.RootDirectory, "Backups")

temp_location = Path.Combine(ThisService.RootDirectory, "..", String.Format("_{0}_Backups", ThisService.ServiceId))
if Directory.Exists(BACKUP_LOCATION) :
  Directory.Move(BACKUP_LOCATION, temp_location)


Operating System: Any
Name: Restore backups after install
Description: Moves backups back into root.
Script Engine: IronPython
Event: After reinstall
Script: Configure BACKUP_LOCATION
import clr;

from System import String
from System.IO import Path, Directory

# Specify where backups are saved.
# Default location is a folder named Backups in the game server's root.
BACKUP_LOCATION = Path.Combine(ThisService.RootDirectory, "Backups")

temp_location = Path.Combine(ThisService.RootDirectory, "..", String.Format("_{0}_Backups", ThisService.ServiceId))
if Directory.Exists(temp_location) :
  Directory.Move(temp_location, BACKUP_LOCATION)
Retrieved from "https://help.tcadmin.com/index.php?title=Backup_%26_Restore_Scripts&oldid=2554"