Skip to content

Instantly share code, notes, and snippets.

@jarulsamy
Created January 3, 2020 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarulsamy/2d09d9e4324f30f383ed931aa3e08e6e to your computer and use it in GitHub Desktop.
Save jarulsamy/2d09d9e4324f30f383ed931aa3e08e6e to your computer and use it in GitHub Desktop.
A Python script to compress and upload terraria server worlds to google drive.
#!/usr/bin/python3
import enum
import logging
import subprocess
import time
import os
logging.basicConfig(filename="/home/terraria/terraria-backup.log", filemode="a",
format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.DEBUG)
filename = time.strftime("%Y-%m-%d", time.gmtime()) + ".tar.gz"
# Enum for size units
class SIZE_UNIT(enum.Enum):
BYTES = 1
KB = 2
MB = 3
GB = 4
def convert_unit(size_in_bytes, unit):
""" Convert the size from bytes to other units like KB, MB or GB"""
if unit == SIZE_UNIT.KB:
return size_in_bytes / 1024
elif unit == SIZE_UNIT.MB:
return size_in_bytes / (1024 * 1024)
elif unit == SIZE_UNIT.GB:
return size_in_bytes / (1024 * 1024 * 1024)
else:
return size_in_bytes
# Compress
try:
subprocess.run(
[f"tar -zcvf {filename} -C /home/terraria/tshock/Worlds ."], shell=True, check=True)
except Exception as e:
logging.exception("Tar Exception")
else:
file_size = convert_unit(os.path.getsize(filename), SIZE_UNIT.MB)
logging.info(
f"Successfully Compressed to {filename}: {round(file_size, 2)}")
# Upload to gdrive
try:
subprocess.run([f"rclone copy {filename} gdrive:terraria-backup"],
shell=True, check=True)
except Exception as e:
logging.exception("Rclone Exception")
else:
logging.info(f"Sucessfully Uploaded {filename} to gdrive")
os.unlink(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment