2018-10-22 17:16:55 +00:00
|
|
|
#!/bin/sh /etc/rc.common
|
|
|
|
|
|
|
|
EXTRA_COMMANDS="backup restore"
|
|
|
|
EXTRA_HELP=<<EOF
|
|
|
|
backup Backup vnstat database
|
|
|
|
restore Restore vnstat database
|
|
|
|
EOF
|
|
|
|
|
|
|
|
START=98
|
|
|
|
STOP=10
|
|
|
|
|
|
|
|
vnstat_option() {
|
|
|
|
sed -ne "s/^[[:space:]]*$1[[:space:]]*['\"]\([^'\"]*\)['\"].*/\1/p" /etc/vnstat.conf
|
|
|
|
}
|
|
|
|
|
|
|
|
BACKUP_FILE=/etc/vnstat_backup.tar.gz
|
|
|
|
LOGGER_TAG=vnstat_backup
|
|
|
|
VNSTAT_DIR="$(vnstat_option DatabaseDir)"
|
2019-07-22 15:47:48 +00:00
|
|
|
|
|
|
|
_chk_omrquota() {
|
|
|
|
config_get enabled $1 enabled
|
|
|
|
[ "$enabled" = "1" ] && backup="true"
|
|
|
|
}
|
|
|
|
|
2018-10-22 17:16:55 +00:00
|
|
|
backup_database() {
|
2019-07-22 15:47:48 +00:00
|
|
|
backup="false"
|
|
|
|
config_load omr-quota
|
|
|
|
config_foreach _chk_omrquota interface
|
2020-08-26 08:45:09 +00:00
|
|
|
[ "$(uci -q get openmptcprouter.settings.vnstat_backup)" = "1" ] && backup="true"
|
2019-07-22 15:47:48 +00:00
|
|
|
if [ "$backup" = "true" ]; then
|
|
|
|
if [ ! -d $VNSTAT_DIR ]; then
|
|
|
|
logger -t $LOGGER_TAG -p err "cannot backup, data directory $VNSTAT_DIR does not exist (yet)"
|
|
|
|
else
|
|
|
|
logger -t $LOGGER_TAG -p info "backing up database"
|
|
|
|
/bin/tar -zcf $BACKUP_FILE -C $VNSTAT_DIR .
|
|
|
|
fi
|
2018-10-22 17:16:55 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_database() {
|
|
|
|
if [ ! -f $BACKUP_FILE ]; then
|
|
|
|
logger -t $LOGGER_TAG -p err "cannot restore, backup file does not exist (yet)"
|
|
|
|
else
|
|
|
|
logger -t $LOGGER_TAG -p info 'restoring database'
|
|
|
|
[ ! -d $VNSTAT_DIR ] && mkdir $VNSTAT_DIR
|
|
|
|
/bin/tar -xzf $BACKUP_FILE -C $VNSTAT_DIR
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
restore_database
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
backup_database
|
|
|
|
}
|
|
|
|
|
|
|
|
backup() {
|
|
|
|
backup_database
|
|
|
|
}
|
|
|
|
|
|
|
|
restore() {
|
|
|
|
restore_database
|
|
|
|
}
|