Skip to content

Commit

Permalink
Merge pull request #66 from jekhokie/add-prune-by-date
Browse files Browse the repository at this point in the history
Add Prune Scripts for Date/Number Ranges
  • Loading branch information
jekhokie authored Feb 12, 2021
2 parents fa3cced + d382409 commit d923ea7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
2 changes: 2 additions & 0 deletions ansible/roles/common/templates/noaa-v2.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ NOAA_MAP_CROSSHAIR_COLOR={{ noaa_map_crosshair_color }}
NOAA_MAP_GRID_DEGREES={{ noaa_map_grid_degrees }}
NOAA_MAP_GRID_COLOR={{ noaa_map_grid_color }}
PRODUCE_SPECTROGRAM={{ produce_spectrogram|lower }}
PRUNE_OLDER_THAN={{ delete_older_than_n }}
PRUNE_OLDEST={{ delete_oldest_n }}
6 changes: 6 additions & 0 deletions config/settings.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@ log_level: DEBUG
# by default disabled on "extra-small" devices such as phones due to
# the processing and space requirements
enable_satvis: true

# pruning capabilities - must be configured in cron (see documentation)
# delete_oldest_n - how many oldest captures to delete on each run
# delete_older_than_n - delete all images older than this many days
delete_oldest_n: 0
delete_older_than_n: 0
...
30 changes: 25 additions & 5 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,33 @@ Audio may not exist depending on how you configured your installation. By defaul
after images are created to preserve space on your Raspberry Pi. If you disabled deleting audio, the audio should obviously
still remain in its directory after image processing completes.

In addition, there is an ability to "prune" images to maintain space on your Raspberry Pi, deleting the 10 oldest images
from the disk and the database. Note that this is a destructive operation that is non-recoverable, so be certain this is
what you want to be doing. If so, run the following command, which will configure the prune script to run each day at
midnight:
In addition, there is an ability to "prune" images to maintain space on your Raspberry Pi. This can be done one of 2 ways,
detailed below. In either case, to adjust the configurations for these, update the respective parameter in the `config/settings.yml`
file and re-run the `install_and_upgrade.sh` script to propagate your settings *BEFORE* creating the cron jobs.

**NOTE**: Both of these prune scripts delete the associated files and database records for the captures that are in scope.
Make sure this is what you want as once the script has run and the captures are deleted, they will not be recoverable.

## Prune Oldest n Captures

This script, named `scripts/prune_scripts/prune_oldest.sh`, is used to prune the oldest `n` number of captures, where `n` is
configured as the `delete_oldest_n` parameter in `config/settings.yml`. This is an example of a cron job that is
configured to run nightly at midnight using this script:

```bash
# prune oldest n captures
cat <(crontab -l) <(echo "1 0 * * * /home/pi/raspberry-noaa-v2/scripts/prune_oldest.sh") | crontab -
```

## Prune Captures Older Than n Days

This script, named `scripts/prune_scripts/prune_older_than.sh`, is used to prune all captures older than `n` days old, where
`n` is configured as the `delete_older_than_n` parameter in `config/settings.yml`. This is an example of a cron job
that is configured to run nightly at midnight using this script:

```bash
cat <(crontab -l) <(echo "1 0 * * * /home/pi/raspberry-noaa-v2/scripts/prune.sh") | crontab -
# prune captures older than n days
cat <(crontab -l) <(echo "1 0 * * * /home/pi/raspberry-noaa-v2/scripts/prune_older_than.sh") | crontab -
```

# Completely White Meteor-M 2 Images
Expand Down
20 changes: 20 additions & 0 deletions scripts/prune_scripts/prune_older_than.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
# Purpose: Prunes (removes) all captures older than $PRUNE_OLDER_THAN days old, including
# database records and associated images/files on disk.

# import common lib and settings
. "$HOME/.noaa-v2.conf"
. "$NOAA_HOME/scripts/common.sh"

#Generate date since epoch in seconds - days
let prunedate=$(date +%s)-$PRUNE_OLDER_THAN*24*60*60

log "Pruning captures..." "INFO"
for img_path in $(sqlite3 ${DB_FILE} "select file_path from decoded_passes where pass_start < $prunedate;"); do
find "${IMAGE_OUTPUT}/" -maxdepth 1 -type f -name "${img_path}*.jpg" exec rm -f {} \;
find "${IMAGE_OUTPUT}/thumb/" -maxdepth 1 -type f -name "${img_path}*.jpg" exec rm -f {} \;
log " ${img_path} file pruned" "INFO"
sqlite3 "${DB_FILE}" "delete from decoded_passes where file_path = \"$img_path\";"
log " Database entry pruned" "INFO"
done
10 changes: 5 additions & 5 deletions scripts/prune.sh → scripts/prune_scripts/prune_oldest.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/bin/bash
#
# Purpose: Prunes (removes) the 10 oldest captures from the database
# and respective images.
# Purpose: Prunes (removes) the $PRUNE_OLDEST number of oldest captures, including
# database records and associated images/files on disk.

# import common lib and settings
. "$HOME/.noaa-v2.conf"
. "$NOAA_HOME/scripts/common.sh"

log "Pruning captures..." "INFO"
for img_path in $(sqlite3 ${DB_FILE} "select file_path from decoded_passes limit 10;"); do
find "${IMAGE_OUTPUT}/" -type f -name "${IMG_NAME}*.jpg" -exec rm -f {} \;
find "${IMAGE_OUTPUT}/thumb/" -type f -name "${IMG_NAME}*.jpg" -exec rm -f {} \;
for img_path in $(sqlite3 ${DB_FILE} "select file_path from decoded_passes limit ${PRUNE_OLDEST};"); do
find "${IMAGE_OUTPUT}/" -maxdepth 1 -type f -name "${img_path}*.jpg" exec rm -f {} \;
find "${IMAGE_OUTPUT}/thumb/" -maxdepth 1 -type f -name "${img_path}*.jpg" exec rm -f {} \;
log " ${img_path} file pruned" "INFO"
sqlite3 "${DB_FILE}" "delete from decoded_passes where file_path = \"$img_path\";"
log " Database entry pruned" "INFO"
Expand Down

0 comments on commit d923ea7

Please sign in to comment.