Skip to content

Instantly share code, notes, and snippets.

@velebit
Created May 7, 2021 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velebit/071dd820b47ecfccbfea351c18e8e330 to your computer and use it in GitHub Desktop.
Save velebit/071dd820b47ecfccbfea351c18e8e330 to your computer and use it in GitHub Desktop.
OpenWrt /etc/init.d/ script to backup and restore the rrd (collectd) database, to preserve data across reboots
#!/bin/sh /etc/rc.common
# OpenWrt /etc/init.d/ script to backup and restore the rrd (collectd) database, to preserve data across reboots
#
#
# howto:
# - upload this file as /etc/init.d/rrdbackup
# - (optional) adjust BACKUP_DIR below to point to a different target directory for the backup (e.g., a USB drive)
# - # chmod +x /etc/init.d/rrdbackup
# - # /etc/init.d/rrdbackup enable
# - # /etc/init.d/rrdbackup start
# - (optional) for periodic backups insert into crontab:
# 0 */6 * * * /etc/init.d/rrdbackup backup
# adjust interval to own taste (example above backs up every 6 hours)
# - (optional) add the backup target directory to /etc/sysupgrade.conf for the backup to be preserved across sysupgrades
# queue this init script to start (i.e., restore) right before collectd starts (80)
# and stop (i.e., backup) right after collectd stops (10):
START=79
STOP=11
# add commands "backup" to manually backup database (e.g. from cronjob)
# and "restore" to restore database (should not be needed in regular use)
EXTRA_COMMANDS="backup restore"
EXTRA_HELP="\
backup Backup current rrd database
restore Restore current rrd database"
# directories and files
# RRD_DIR needs to be relative to root, i.e. no slash in front (to mitigate tar "leading '/'" warning)
RRD_DIR=tmp/rrd
BACKUP_DIR=/usr/share/collectd
BACKUP_FILE=rrdbackup.tgz
backup() {
mkdir -p "$BACKUP_DIR"
local TMP_FILE="$(mktemp -up "$BACKUP_DIR")"
tar -czf "$TMP_FILE" -C / "$RRD_DIR"
mv "$TMP_FILE" "$BACKUP_DIR/$BACKUP_FILE"
}
restore() {
[[ -f "$BACKUP_DIR/$BACKUP_FILE" ]] && tar -xzf "$BACKUP_DIR/$BACKUP_FILE" -C /
}
start() {
restore
}
stop() {
backup
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment