From 01bf88f83d40f17375163249d6391cf00f69eb8d Mon Sep 17 00:00:00 2001
From: Krateng <git.noreply@krateng.ch>
Date: Tue, 10 Dec 2019 20:05:19 +0100
Subject: [PATCH] Added backup command

---
 .gitignore           |  1 -
 README.md            |  2 +-
 maloja/__init__.py   |  2 +-
 maloja/controller.py | 39 +++++++++++++++++++++++++++++++++++++--
 4 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/.gitignore b/.gitignore
index 7707cfa..5614825 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,7 +5,6 @@
 *.note
 *.xcf
 nohup.out
-*.egg-info
 
 # currently not using
 /screenshot*.png
diff --git a/README.md b/README.md
index 96add9d..6d1ea2d 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@ Also neat: You can use your **custom artist or track images**.
 
 ## Requirements
 
-* Python 3
+* Python 3.5 or higher
 * Several Pip packages (automatically downloaded)
 * If you'd like to display images, you will need API keys for [Last.fm](https://www.last.fm/api/account/create) and [Fanart.tv](https://fanart.tv/get-an-api-key/). These are free of charge!
 
diff --git a/maloja/__init__.py b/maloja/__init__.py
index 951b56c..8828998 100644
--- a/maloja/__init__.py
+++ b/maloja/__init__.py
@@ -7,7 +7,7 @@ author = {
 	"email":"maloja@krateng.dev",
 	"github": "krateng"
 }
-version = 2,0,7
+version = 2,0,8
 versionstr = ".".join(str(n) for n in version)
 
 
diff --git a/maloja/controller.py b/maloja/controller.py
index d3dc571..caf545a 100755
--- a/maloja/controller.py
+++ b/maloja/controller.py
@@ -161,16 +161,51 @@ def loadlastfm(filename):
 def direct():
 	from . import server
 
+def backup(level="full"):
+	import tarfile
+	from datetime import date
+	import glob
 
 
-@mainfunction({},shield=True)
+
+	user_files = {
+		"minimal":[
+			"rules/*.tsv",
+			"scrobbles"
+		],
+		"full":[
+			"clients/authenticated_machines.tsv",
+			"images/artists",
+			"images/tracks",
+			"settings/settings.ini"
+		]
+	}
+
+	user_files = user_files["minimal"] if level == "minimal" else user_files["minimal"] + user_files["full"]
+	real_files = []
+	for g in user_files:
+		real_files += glob.glob(g)
+
+	today = date.today()
+	datestr = "-".join((str(today.year),str(today.month),str(today.day)))
+	filename = "maloja_backup_" + datestr + ".tar.gz"
+	archivefile = os.path.join(origpath,filename)
+	assert not os.path.exists(archivefile)
+	with tarfile.open(name=archivefile,mode="x:gz") as archive:
+		for f in real_files:
+			archive.add(f)
+
+
+
+@mainfunction({"l":"level"},shield=True)
 def main(action,*args,**kwargs):
 	actions = {
 		"start":restart,
 		"restart":restart,
 		"stop":stop,
 		"import":loadlastfm,
-		"debug":direct
+		"debug":direct,
+		"backup":backup
 	}
 	if action in actions: actions[action](*args,**kwargs)
 	else: print("Valid commands: " + " ".join(a for a in actions))