summaryrefslogtreecommitdiff
path: root/templates
diff options
authorJuan L. Negron <juan.negron@canonical.com>2013-02-14 20:33:10 -0800
committerJuan L. Negron <juan.negron@canonical.com>2013-02-14 20:33:10 -0800
commit7a798aef42dd7d4ab6f2d0d08ae26841ad73309f (patch)
tree04d3f7c91e6c819d3dfbafe9fff19e8620daaa56 /templates
parent7771f960606b89fdc74fc0732029f6eeca21b509 (diff)
parent1b4f44049c3eb7dd7edafaa64d61c152cafac7fe (diff)
Add support for mongodump backups from cron.
- Adds a backup script which is copied to /var/lib/mongodb to do the actual backup work. - On config change, it generates the backup script from the template in the charm with the path/backup copies kept config data. - Added some readme around the config of backup with notes on limitations such as backups continuing after a service destroy.
Diffstat (limited to 'templates')
-rwxr-xr-xtemplates/backup.py.tpl53
1 files changed, 53 insertions, 0 deletions
diff --git a/templates/backup.py.tpl b/templates/backup.py.tpl
new file mode 100755
index 0000000..bb1f079
--- /dev/null
+++ b/templates/backup.py.tpl
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+"""Generate the cronjob to backup with mongodbump."""
+
+from os import chdir
+from os import listdir
+from os import remove
+from os.path import exists
+from os.path import join
+from datetime import datetime
+from shutil import rmtree
+import subprocess
+from tempfile import mkdtemp
+
+
+when = datetime.now()
+backupdir = '$backup_directory'
+tmpdir = mkdtemp()
+
+# Clean up any old backup copies.
+current_backups = listdir(backupdir)
+current_backups.sort()
+for file in current_backups[0:-$backup_copies]:
+ remove(join(backupdir, file))
+
+chdir(tmpdir)
+
+# Make sure the directory to stick tarballs exist.
+if not exists(backupdir):
+ subprocess.call([
+ 'mkdir',
+ '-p',
+ backupdir,
+ ])
+
+# Generate a pretty unique backup filename.
+# The unique name might have slashes in it so replace them.
+backup_filename = "%s/%s-%s.tar.gz" % (
+ backupdir,
+ '$unique_name'.replace('/', '-'),
+ when.strftime("%Y%m%d-%H%M%S"),
+)
+
+# mongodump creates a directory per db. Drop them all in a tmpdir and then
+# we'll tar it up as step 2.
+dump = '/usr/bin/mongodump --host 127.0.0.1:$port'
+subprocess.call(dump, shell=True)
+
+# Generate the ta
+tar = 'tar czvf %s dump/*' % backup_filename
+subprocess.call(tar, shell=True)
+
+# Clean up the tmpdir.
+rmtree(tmpdir)