#!/bin/bash
# info: backup all users
# options: NONE
#
# example: v-backup-users
#
# This function backups all system users.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Auto-repair all databases before backuping all accounts
mysqlrepair --all-databases --check --auto-repair > /dev/null 2>&1

if [ -z "$BACKUP_SYSTEM" ]; then
	exit
fi
for user in $($BIN/v-list-sys-users plain); do
	check_suspend=$(grep "SUSPENDED='no'" $HESTIA/data/users/$user/user.conf)
	log=$HESTIA/log/backup.log
	if [ ! -f "$HESTIA/data/users/$user/user.conf" ]; then
		continue
	fi
	check_backup_conditions
	check_suspend=$(grep "SUSPENDED='no'" $HESTIA/data/users/$user/user.conf)
	log=$HESTIA/log/backup.log
	if [ -n "$check_suspend" ]; then
		echo -e "================================" >> $log
		echo -e "$user" >> $log
		echo -e "--------------------------------\n" >> $log
		nice -n 19 ionice -c2 -n7 $BIN/v-backup-user $user >> $log 2>&1
		echo -e "\n--------------------------------\n\n" >> $log
	fi
done

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

exit
