Created
January 3, 2020 15:47
-
-
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.
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
#!/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