Last active
March 23, 2016 17:39
-
-
Save barrycarey/432de812ed7b8238a8a5 to your computer and use it in GitHub Desktop.
TCAdmin Zip Directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import clr | |
from System import Array, DateTime | |
from System.String import Format | |
from System.IO import File, DirectoryInfo, Path, Directory | |
clr.AddReference("TCAdmin.SDK") | |
from TCAdmin.SDK.Misc import CompressionTools | |
base_dir = ThisService.WorkingDirectory | |
files = [] | |
compress_dir = r"platform" # Directory to compress, don't include working dir. Can be sub-dir (world\example) | |
backup_dir = r"Backups" # Can be sub-dir (Minecraft\Backup) | |
compression_type = ".zip" # Should be able to use .zip, .rar, .tar, .tar.gz, .tgz, .tar.bz2 | |
# Make the zip file name | |
output_zip = DateTime.Now.ToString("yyyy-MM-dd--hh-mm-ss") + compression_type | |
full_output_zip_path = Path.Combine(backup_dir, output_zip) | |
full_compress_dir = Path.Combine(base_dir, compress_dir) | |
def build_file_list(dir_name): | |
""" | |
Recursive crawl through provided directory. Append files to list of files. | |
Get all dirs and call func again with new path | |
""" | |
dumpdir = DirectoryInfo(dir_name) | |
for f in dumpdir.GetFiles(): | |
files.append(Path.Combine(dir_name, f.Name).replace(ThisService.WorkingDirectory, '')) | |
for d in dumpdir.GetDirectories(): | |
build_file_list(Path.Combine(dir_name, d.Name)) | |
def compress(): | |
Script.WriteToConsole("Compressing Files and Creating Backup") | |
if not Directory.Exists(Path.Combine(base_dir, backup_dir)): | |
Directory.CreateDirectory(Path.Combine(base_dir, backup_dir)) | |
comp = CompressionTools() | |
file_array = Array[String](files) | |
comp.Compress(base_dir, file_array, Path.Combine(base_dir, full_output_zip_path)) | |
def main(): | |
if not Directory.Exists(full_compress_dir): | |
Script.WriteToConsole(Format("ERROR: Backup Path Doesn't Exist. {0}", full_compress_dir)) | |
Script.Exit() | |
Script.WriteToConsole(Format("Backup Will Be Saved To: {0}", full_output_zip_path)) | |
build_file_list(full_compress_dir) | |
Script.WriteToConsole(Format("We Found {0} Files To Backup", str(len(files)))) | |
compress() | |
Script.WriteToConsole("Backup Complete") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment