'How to use ddev commands in its own exec-host hooks for an automated backup

I've made a custom command to my ddev, creating a database backup with a single command (yes, I'm lazy, sorry).

I was thinking if there's some way to hook a ddev command, e.g. ddev poweroff to run another command or command sequence together.

The idea is to make a backup of all databases in a specific directory when I run the ddev poweroff.

Anyone have a clue about it?

Thanks



Solution 1:[1]

Sure, pre-stop exec-host hooks can invoke ddev directly. Here's an example of a pre-stop hook that does both a snapshot and a traditional db dump:

hooks:
  pre-stop:
    - exec-host: ddev snapshot --name=$(date +%Y%m%d%H%M)
    - exec-host: mkdir -p .tarballs && ddev export-db --file=.tarballs/db.$(date +%Y%m%d%H%M).sql.gz

For more info on hooks, see DDEV hook docs.

Hope that helps!

Solution 2:[2]

Use list.sort with key=int (thanks @Metapod, @deceze et al):

for v in d.values():
    v.sort(key=int)

If you want a one-liner (which makes no sense in this case because for-loop is very nice), a monstrosity like this one could be one I guess:

dict(zip(d.keys(), map(lambda x: sorted(x, key=int), d.values())))

Output:

{'SOL200122': ['120', '125', '130', '135', '140', '145', '150', '160', '170']}

Solution 3:[3]

try this:

for v in d.values():
    v.sort(key=int)

output:

{'SOL200122': ['120', '125', '130', '135', '140', '145', '150', '160', '170']}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rfay
Solution 2
Solution 3 Tal Folkman