Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 (undocumented, should not be needed in regular use)
EXTRA_COMMANDS="backup restore"
EXTRA_HELP=" backup backup 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() {
local TMP_FILE=$(mktemp -u)
tar -czf $TMP_FILE -C / $RRD_DIR
mkdir -p $BACKUP_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
}
@tmkasun

This comment has been minimized.

Copy link

@tmkasun tmkasun commented Jan 9, 2021

Thank you very much for providing this gist, It was very helpful to write my own startup task and do the rdd backups.
I was bit confused with crontabs because i didn't know that the above

0 */6 * * * /etc/init.d/rrdbackup backup

has to be inserted to /etc/crontabs/root file (which is the default i think).
For more info on crontabs , here is the official documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.