]> git.frykholm.com Git - butterbackup.git/commitdiff
Add code.
authorMikael Frykholm <mikael@frykholm.com>
Sat, 4 Aug 2012 07:44:25 +0000 (09:44 +0200)
committerMikael Frykholm <mikael@frykholm.com>
Sat, 4 Aug 2012 07:44:25 +0000 (09:44 +0200)
TODO [new file with mode: 0644]
butterbackup.py [new file with mode: 0755]
dir.html [new file with mode: 0644]
server.py [new file with mode: 0755]

diff --git a/TODO b/TODO
new file mode 100644 (file)
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 (executable)
index 0000000..a7c7843
--- /dev/null
@@ -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 (file)
index 0000000..4a5a3c0
--- /dev/null
+++ b/dir.html
@@ -0,0 +1,12 @@
+<html>
+   <head>
+      <title>{{ title }}</title>
+   </head>
+   <body>
+     <ul>
+       {% for item in items %}
+         <li><a href="{{ escape(item) }}">{{ escape(item) }}</a></li>
+       {% end %}
+     </ul>
+   </body>
+ </html>
diff --git a/server.py b/server.py
new file mode 100755 (executable)
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()