From 25a669627f9582f929845f89c3c5df1ebd1b0864 Mon Sep 17 00:00:00 2001 From: Mikael Frykholm Date: Sat, 4 Aug 2012 09:44:25 +0200 Subject: [PATCH] Add code. --- TODO | 1 + butterbackup.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ dir.html | 12 ++++++++++++ server.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 TODO create mode 100755 butterbackup.py create mode 100644 dir.html create mode 100755 server.py diff --git a/TODO b/TODO new file mode 100644 index 0000000..95c4bb1 --- /dev/null +++ b/TODO @@ -0,0 +1 @@ +check for rsync return status diff --git a/butterbackup.py b/butterbackup.py new file mode 100755 index 0000000..a7c7843 --- /dev/null +++ b/butterbackup.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +import os +import sys +from subprocess import Popen, PIPE +import shlex +import datetime + +def backup_host(host, base_dir, fp): + dest_dir = os.path.join(base_dir, host, "latest") + if not os.path.exists(base_dir): + print("New host",host,".") + run("btrfs subvol create %s"% os.path.join(base_dir, host)) + os.makedirs(dest_dir) + command = ("rsync -a --numeric-ids --delete --delete-excluded --human-readable --inplace ") + excludes = fp.readline()[:-1] + (stdout,stderr) = run(command + excludes + " root@%s:/ "%(host) + dest_dir) + if stdout: + print(stdout) + if stderr: + print(stdout) + run("btrfs subvol snapshot %s %s"%(os.path.join(base_dir, host),os.path.join(base_dir, host, datetime.datetime.now().date().strftime("%F")))) + +def run(cmd): + (stdout, stderr) = Popen(shlex.split(cmd), stdout=PIPE).communicate() + if stdout: + stdout = stdout.decode('utf-8') + if stderr: + stderr = stderr.decode('utf-8') + return(stdout, stderr) + +if __name__ == "__main__": + if os.geteuid() != 0: + print("You need to be root. Otherwise all permissions will be lost.") + sys.exit(-1) + base_path="/etc/butterbackup" + dest_dir="/mnt/data2" + if not os.path.exists(base_path): + print("No hosts to backup, please place them in",base_path) + sys-exit(-1) + hosts = os.listdir(base_path) + for host in hosts: + fp = open(os.path.join(base_path, host),"r") + backup_host(host, dest_dir, fp) + sys.exit(0) diff --git a/dir.html b/dir.html new file mode 100644 index 0000000..4a5a3c0 --- /dev/null +++ b/dir.html @@ -0,0 +1,12 @@ + + + {{ title }} + + + + + diff --git a/server.py b/server.py new file mode 100755 index 0000000..eb191e2 --- /dev/null +++ b/server.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +import os +import tornado.ioloop +import tornado.web + +class MainHandler(tornado.web.RequestHandler): + def get(self, path): + base_path="/mnt/data2/" + fp = os.path.join(base_path, path) + if not os.path.exists(fp): + return self.write("Not found") + if os.path.isdir(fp): + items = os.listdir(fp) + items2 = [] + for item in items: + if os.path.isdir(os.path.join(fp,item)): + item = item+"/" + items2.append(item) + self.render("dir.html",items=items2, title=path) + return + self.set_header("Content-Type", "binary/octet-stream") + self.write(open(fp,"rb").read()) + +application = tornado.web.Application([ + (r"/(.*)", MainHandler), +], debug=True) + +if __name__ == "__main__": + application.listen(1234) + tornado.ioloop.IOLoop.instance().start() -- 2.39.5