umputun / remark42

comment engine
https://remark42.com
MIT License
4.84k stars 376 forks source link

How about just want to stored comment only for latest 1-month #1800

Open rahman77889 opened 1 month ago

rahman77889 commented 1 month ago

Hello

As you know, not every single needs to store all comments for months or even more

With minimum resources, we need a new feature that can auto-remove and only keep the latest 1-month

It will be better for maintaining performance and high availability with minimum treatment

Thank you very much

paskal commented 1 month ago

Here's how to back up all comments, remove those older than one month using a shell script with jq, and restore the modified backup:

1. Backup Comments

First, back up the comments using the provided docker exec command:

docker exec -it remark42 backup -s {your site ID}

This will create a file named userbackup-{site ID}-{timestamp}.gz.

2. Extract Backup File

Next, extract the backup file to work with it:

gzip -d userbackup-{site ID}-{timestamp}.gz

3. Remove Comments Older Than One Month

Create a shell script to filter out comments older than one month using jq:

#!/bin/bash

input_file="userbackup-{site ID}-{timestamp}"
output_file="filtered_userbackup-{site ID}-{timestamp}"
one_month_ago=$(date -d '1 month ago' +%s)

jq --argjson one_month_ago "$one_month_ago" '
    select(.time | fromdateiso8601 | mktime > $one_month_ago)
' "$input_file" > "$output_file"

Replace {site ID} and {timestamp} with the actual values.

Save this script as filter_comments.sh and make it executable:

chmod +x filter_comments.sh

Run the script:

./filter_comments.sh

4. Compress the Filtered Backup File

Compress the filtered backup file:

gzip filtered_userbackup-{site ID}-{timestamp}

5. Restore Comments

Finally, restore the comments using the provided docker exec command:

docker exec -it remark42 restore -f filtered_userbackup-{site ID}-{timestamp}.gz -s {your site ID}

By following these steps, you'll be able to back up all comments, filter out those older than one month using a shell script with jq, and restore the modified backup effectively removing the old comments.