summaryrefslogtreecommitdiffstats
path: root/archives/publik-move/dump-schemas
diff options
context:
space:
mode:
Diffstat (limited to 'archives/publik-move/dump-schemas')
-rwxr-xr-xarchives/publik-move/dump-schemas46
1 files changed, 46 insertions, 0 deletions
diff --git a/archives/publik-move/dump-schemas b/archives/publik-move/dump-schemas
new file mode 100755
index 0000000..525bf03
--- /dev/null
+++ b/archives/publik-move/dump-schemas
@@ -0,0 +1,46 @@
+#! /usr/bin/env python
+
+import os
+import sys
+import subprocess
+import datetime
+
+try:
+ import psycopg2
+except ImportError:
+ sys.exit(0)
+
+conn = psycopg2.connect(dbname='postgres')
+cursor = conn.cursor()
+cursor.execute('SELECT datname FROM pg_database WHERE datistemplate = false')
+databases = [x[0] for x in cursor.fetchall()]
+conn.close()
+
+for database in databases:
+ if database == 'postgres':
+ continue
+
+ conn = psycopg2.connect(dbname=database)
+ cursor = conn.cursor()
+ cursor.execute('SELECT schema_name FROM information_schema.schemata')
+ schemas = [x[0] for x in cursor.fetchall()]
+
+ backups_dir = os.path.join('/var/lib/postgresql/migration/dump', database)
+ if not os.path.exists(backups_dir):
+ os.makedirs(backups_dir, mode=0770)
+ os.chmod(backups_dir, 0770)
+
+ # trigger backup mode for continuous archiving
+ today = datetime.date.today()
+
+ for schema in schemas:
+ if schema.startswith('pg_') or schema == 'information_schema':
+ continue
+ backup_filename = os.path.join(backups_dir, '%s.tar' % schema)
+ subprocess.call(['pg_dump', '-n', schema, '-F', 'tar',
+ '-f', backup_filename + '.new', database])
+ if os.path.exists(backup_filename):
+ os.unlink(backup_filename)
+ os.rename(backup_filename + '.new', backup_filename)
+
+ conn.close()