Skip to content

Commit

Permalink
Add filter option for backup (#141)
Browse files Browse the repository at this point in the history
* Add filter option for backup

* Black linting
  • Loading branch information
taekop authored May 2, 2022
1 parent d74ce90 commit 01db8d8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ usage: dynamodump [-h] [-a {zip,tar}] [-b BUCKET]
[--writeCapacity WRITECAPACITY] [--schemaOnly]
[--dataOnly] [--noConfirm] [--skipThroughputUpdate]
[--billingMode BILLING_MODE] [--dumpPath DUMPPATH] [--log LOG]
[-f FILTEROPTION]
Simple DynamoDB backup/restore/empty.
Expand Down Expand Up @@ -110,6 +111,8 @@ optional arguments:
backups (defaults to use 'dump') [optional]
--log LOG Logging level - DEBUG|INFO|WARNING|ERROR|CRITICAL
[optional]
-f FILTEROPTION, --filterOption FILTEROPTION
Filter option for backup, JSON file of which keys are ['FilterExpression', 'ExpressionAttributeNames', 'ExpressionAttributeValues']
```

Backup files are stored in a 'dump' subdirectory, and are restored from there as well by default.
Expand Down
39 changes: 35 additions & 4 deletions dynamodump/dynamodump.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def do_empty(dynamo, table_name, billing_mode):
)


def do_backup(dynamo, read_capacity, tableQueue=None, srcTable=None):
def do_backup(dynamo, read_capacity, tableQueue=None, srcTable=None, filterOption=None):
"""
Connect to DynamoDB and perform the backup for srcTable or each table in tableQueue
"""
Expand Down Expand Up @@ -703,6 +703,8 @@ def do_backup(dynamo, read_capacity, tableQueue=None, srcTable=None):
optional_args = {}
if last_evaluated_key is not None:
optional_args["ExclusiveStartKey"] = last_evaluated_key
if filterOption is not None:
optional_args.update(filterOption)
scanned_table = dynamo.scan(
TableName=table_name, **optional_args
)
Expand Down Expand Up @@ -1230,6 +1232,11 @@ def main():
parser.add_argument(
"--log", help="Logging level - DEBUG|INFO|WARNING|ERROR|CRITICAL " "[optional]"
)
parser.add_argument(
"-f",
"--filterOption",
help="Filter option for backup, JSON file of which keys are ['FilterExpression', 'ExpressionAttributeNames', 'ExpressionAttributeValues']",
)
args = parser.parse_args()

# set log level
Expand Down Expand Up @@ -1282,6 +1289,20 @@ def main():
if args.noSeparator is True:
prefix_separator = None

# set filter options
filter_option = None
if args.filterOption is not None:
with open(args.filterOption, "r") as f:
filter_option = json.load(f)
if filter_option.keys() != set(
(
"FilterExpression",
"ExpressionAttributeNames",
"ExpressionAttributeValues",
)
):
raise Exception("Invalid filter option format")

# do backup/restore
start_time = datetime.datetime.now().replace(microsecond=0)
if args.mode == "backup":
Expand Down Expand Up @@ -1311,9 +1332,19 @@ def main():

try:
if args.srcTable.find("*") == -1:
do_backup(conn, args.read_capacity, tableQueue=None)
do_backup(
conn,
args.read_capacity,
tableQueue=None,
filterOption=filter_option,
)
else:
do_backup(conn, args.read_capacity, matching_backup_tables)
do_backup(
conn,
args.read_capacity,
matching_backup_tables,
filterOption=filter_option,
)
except AttributeError:
# Didn't specify srcTable if we get here

Expand All @@ -1324,7 +1355,7 @@ def main():
t = threading.Thread(
target=do_backup,
args=(conn, args.readCapacity),
kwargs={"tableQueue": q},
kwargs={"tableQueue": q, "filterOption": filter_option},
)
t.start()
threads.append(t)
Expand Down

0 comments on commit 01db8d8

Please sign in to comment.