Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a RotateFiles post processor.

  • Loading branch information...
commit 46fe3f877b7cdcdd3fde1cf309785228d2e052f3 1 parent 4139bbf
@robhudson authored
Showing with 61 additions and 2 deletions.
  1. +9 −2 README.rst
  2. +1 −0  dumpy/base.py
  3. +51 −0 dumpy/postprocessor/rotating.py
View
11 README.rst
@@ -28,14 +28,14 @@ The following is an idea of what the configuration file, located at
name = dbname1
user = db1
password = db1
- postprocessing = TimestampRename, PrependDatabaseName, Bzip, SystemFileCopy, S3Copy
+ postprocessing = TimestampRename, PrependDatabaseName, Bzip, FileSystemCopy, S3Copy
[database db2]
type = postgresql
name = dbname2
user = db2
password = db2
- postprocessing = TimestampRename, Bzip, SystemFileCopy
+ postprocessing = TimestampRename, PrependDatabaseName, Bzip, FileSystemCopy, RotateFiles
[mysqldump options]
path = /opt/local/lib/mysql5/bin/mysqldump
@@ -55,6 +55,13 @@ The following is an idea of what the configuration file, located at
secret_key = secret_key
bucket = bucket
prefix = path/to/directory
+
+ [FileSystemCopy options]
+ directory = /path/to/directory/
+
+ [RotateFiles options]
+ directory = /path/to/directory/
+ number = 5
Status
View
1  dumpy/base.py
@@ -128,6 +128,7 @@ def __init__(self, db):
'Bzip': 'dumpy.postprocessor.bzip.Bzip',
'FileSystemCopy': 'dumpy.postprocessor.fscopy.FileSystemCopy',
'PrependDatabaseName': 'dumpy.postprocessor.dbname.PrependDatabaseName',
+ 'RotateFiles': 'dumpy.postprocessor.rotating.RotateFiles',
'S3Copy': 'dumpy.postprocessor.s3copy.S3Copy',
'TimestampRename': 'dumpy.postprocessor.timestamp.TimestampRename',
}
View
51 dumpy/postprocessor/rotating.py
@@ -0,0 +1,51 @@
+import logging
+import os
+import shutil
+
+import dumpy
+
+logger = logging.getLogger("dumper")
+
+class RotateFiles(dumpy.base.PostProcessBase):
+ """
+ A post processor that rotates files in a directory to keep an optional
+ number on file before deleting them.
+ """
+ def __init__(self, db):
+ self.db = db
+
+ def parse_config(self):
+ super(RotateFiles, self).parse_config()
+ self.dir = self._get_option_value(self.config, 'RotateFiles options', 'directory')
+ self.num = self._get_option_value(self.config, 'RotateFiles options', 'number', 'int')
+ if not self.num:
+ self.num = 10
+
+ def process(self, file):
+ self.parse_config()
+
+ base, ext = os.path.splitext(os.path.basename(file.name))
+ if self.dir.endswith('/'):
+ self.dir = self.dir[0:-1]
+ basename = '%s/%s%s' % (self.dir, base, ext)
+
+ for i in range(self.num, 0, -1):
+ if self.num == i: # We remove last one
+ if os.path.exists("%s.%d" % (basename, i)):
+ os.remove("%s.%d" % (basename, i))
+ logger.info('%s - %s - Remove last file %s.%d' % (
+ self.db, self.__class__.__name__, basename, i))
+ if i > 1: # Copy n-1 to n (e.g. name.1 to name.2)
+ if os.path.exists("%s.%d" % (basename, i-1)):
+ os.rename("%s.%d" % (basename, i-1), "%s.%d" % (basename, i))
+ logger.info('%s - %s - Copy file %s.%d to %s.%d' % (
+ self.db, self.__class__.__name__,
+ basename, i-1, basename, i))
+ elif i == 1: # name.1 is a copy of the original
+ shutil.copy(file.name, "%s.%d" % (basename, i))
+ logger.info('%s - %s - Copy file %s to %s.%d' % (
+ self.db, self.__class__.__name__,
+ file.name, basename, i))
+
+ return file
+
Please sign in to comment.
Something went wrong with that request. Please try again.