import os
from subprocess import check_output
import sys
import tarfile

def volume_backup(volume,file_name):
    if not os.listdir(volume):
        print("The selected volume is empty")
    else:
        os.system("ls " + volume)
        print("Backing up ........")
        tar_file(file_name,volume+"/")

def tar_file(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        tar.add(source_dir+"/", arcname=os.path.basename(source_dir))


def untar_file(file_name,path):
    os.system("rm -r "+path+"/*")
    tf = tarfile.open(file_name)
    tf.extractall(path)
    for f in tf.getmembers():
        print(f.name)


def main():
   if (str(sys.argv[1])=="backup"):
       volume_backup(str(sys.argv[2]),str(sys.argv[3]))
   if (str(sys.argv[1])=="restore"):
       untar_file(str(sys.argv[2]),str(sys.argv[3]))


if __name__== "__main__":
   main()
