#!/bin/sh
#author HuangYeWuDeng
#date 20180811

BAK_DEV=/dev/sda1
if [ $# -eq 1 ]; then
    BAK_DEV=/dev/$1
fi

if ! test -b $BAK_DEV; then
	echo "error: dev $BAK_DEV does not exists. exit ..."
	exit 1
fi

echo "using dev: $BAK_DEV"

MOUNT_POINT=$(cat /proc/mounts | grep $BAK_DEV | awk '{ print $2 }')
if [ ! "$MOUNT_POINT" ];then
	echo "error: seems that dev $BAK_DEV has not been mounted. exit ..."
	exit 1
fi

BACKUP_DST=$MOUNT_POINT/n1-cfg-backup

func_test_mount
test -d $BACKUP_DST || mkdir $BACKUP_DST

echo "kill all daemon ..."
/etc/init.d/rcK

echo "start backup ..."
cp -rav /var/lib $BACKUP_DST
cp -rav /etc $BACKUP_DST
cp -rav /root/.ssh  $BACKUP_DST

#if it is very old n1
if [ ! -d $BACKUP_DST/var/lib/qbittorrent ]; then
	mkdir -p $BACKUP_DST/var/lib/qbittorrent
	cp -rav /root/.cache $BACKUP_DST/var/lib/qbittorrent/
	cp -rav /root/.config $BACKUP_DST/var/lib/qbittorrent/
	cp -rav /root/.local $BACKUP_DST/var/lib/qbittorrent/
fi

echo "start all daemon ..."
/etc/init.d/rcS

echo "--------------------------------------------------------"
echo "--------------------------------------------------------"
ls -lahp $BACKUP_DST
echo "--------------------------------------------------------"
echo "--------------------------------------------------------"

echo "all done."
