#!/bin/sh CONF=/etc/config/qpkg.conf QPKG_NAME="HybridBackup" CBS_NAME="CloudConnector3" QPKG_ROOT=`/sbin/getcfg $QPKG_NAME Install_Path -f ${CONF}` QSYNCD_CONF='/etc/qsync/qsyncd.conf' QSYNCD_EXEC="/usr/bin/qsyncd" INCOMING_JOB_CONF='/etc/qsync/qsyncpassivejob.conf' INCOMING_JOB_BAK_CONF='/etc/qsync/qsyncpassivejobhbs2.conf' # Backup Versioning script cgiPath=/home/httpd/cgi-bin/BackupVersionV2 old_cgiPath=/home/httpd/cgi-bin/BackupVersion InstallPath=$(/sbin/getcfg $QPKG_NAME Install_Path -d FALSE -f $CONF) # NC APP_ID=A200 NC_TOOL_BIN="/usr/local/sbin/nc_tool" RSYNC_MODEL="rsync_433" # include qpkg_sh_log function source "$InstallPath/qpkg_sh_log.sh" log_init $QPKG_NAME function fn_log() { echo $1 app_log $1 } function fn_HBS_Versioning_script_in_start() { fn_log 'Start HBS Versioning script' /bin/chmod u+x $InstallPath/scripts/*.pyc } function fn_HBS_Versioning_script_in_stop() { fn_log 'Stop HBS Versioning script' /bin/rm -f $cgiPath if [ -z "$(/sbin/getcfg BackupVersion Install_Path -f ${CONF})" ]; then /bin/rm -f $old_cgiPath fi } function fn_start_CBS_QPKG() { fn_log 'Start CBS' $QPKG_ROOT/$CBS_NAME/HBS_CloudConnector3.sh start 2>&1 & } function fn_stop_CBS_QPKG() { fn_log 'Stop CBS' $QPKG_ROOT/$CBS_NAME/HBS_CloudConnector3.sh stop 2>&1 & } function fn_start_RTRR() { fn_log 'Start RTRR' RTRR_proc=`/bin/ps -ef | /bin/grep -c [R]TRR_MANAGER` if [ $RTRR_proc -eq 0 ]; then cd '/' /sbin/daemon_mgr RTRR_MANAGER start "QNAP_QPKG=HybridBackup /usr/bin/RTRR_MANAGER" fi } function fn_stop_BackupStation() { fn_log 'Stop BackupStation' fn_kill qsync fn_kill qsyncd if [ -f /usr/bin/qsyncman ]; then /sbin/daemon_mgr qsyncman stop '/usr/bin/qsyncman' /usr/bin/killall -q qsyncman 2>/dev/null fi } function fn_start_BackupStation() { fn_log 'Start BackupStation' # disable HBS before remove if [[ `/sbin/getcfg $QPKG_NAME Operating_Status -f ${CONF} | cut -d: -f1` = 5 ]]; then /sbin/setcfg $QPKG_NAME Enable FALSE -f ${CONF} fi qsyncman_proc=`/bin/ps -ef | /bin/grep -c [q]syncman` if [ $qsyncman_proc -eq 0 ] && [ ! -f /var/qfunc/rcK ]; then /etc/rcS.d/S99qsyncman start & fi cd / /etc/init.d/rsyncd.sh restart 2>/dev/null & } function fn_check_nas_model() { fn_log 'Check NAS Model' if [ -f /lib/libuLinux_hal.so ] && [ -d $QPKG_ROOT/bin/qsync_nosnapshot ]; then Target_Name=Snapshot conf_path=/etc/default_config/volume_man.conf Start=$(/bin/sed -n "/\\[$Target_Name]/=" $conf_path) array=($(/bin/sed -n '/\[.*]/=' $conf_path)) HAL_MODEL=qsync_nosnapshot RSYNC_MODEL=rsync_nosnapshot for i in "${!array[@]}" do if [ "${array[$i]}" = "$Start" ]; then first=${array[$i]} if [ -z "${array[$((i+1))]}" ]; then last=\$ else last=$((${array[$((i+1))]}-1)) fi snapshot_flag=$(/bin/sed -n "${first},${last}"p $conf_path | /bin/grep Support | /bin/sed -n 's/.*'='/ /p' | /bin/tr [a-z] [A-Z] ) if [ $snapshot_flag = 'YES' ]; then app_log "This is SnapShot Model." HAL_MODEL=qsync RSYNC_MODEL=rsync_433 else app_log "This is HAL Model." fi fi done else app_log "This model is not support HAL." if [ -d $QPKG_ROOT/bin/qsync_nohal ]; then HAL_MODEL=qsync_nohal RSYNC_MODEL=rsync_nohal else HAL_MODEL=qsync RSYNC_MODEL=rsync_433 fi fi version_current=$(/sbin/getcfg 'System' version | awk -F. '{ printf("%03d%03d%03d\n", $1,$2,$3); }') build_number_current=$(/sbin/getcfg 'System' 'Build Number' -d 0) fw_version_list=("600" "560" "500210303" "435180809" "434180109") fw_600=("006000000" "20250501" "600") fw_560=("005600000" "20250501" "560") fw_500210303=("005000000" "20210303" "500210304") fw_500210303=("005000000" "20210303" "500210304") fw_435180809=("004003005" "20180809" "435180809") fw_434180109=("004003004" "20180109" "434180109") for fw_version in "${fw_version_list[@]}" do eval fw_env=( \"\${fw_${fw_version}[@]}\" ) check_fw=${fw_env[0]} check_build=${fw_env[1]} fw_path=${fw_env[2]} if [ $version_current -ge $check_fw ] && [ $build_number_current -ge $check_build ] && [ "$(/bin/ls -A $QPKG_ROOT/bin/qsync_${fw_path})" ]; then HAL_MODEL="qsync_${fw_path}" RSYNC_MODEL="rsync_${fw_path}" app_log "FW greater than ${fw_path}" break fi done } function fn_kill() { pidnum=`/bin/pidof $1` [ -z "$pidnum" ] || kill $pidnum 2>/dev/null i=0 while [ ${i} -lt 5 ] do pidnum=`/bin/pidof $1` if [ -z "$pidnum" ]; then break fi sleep 1 i=`/usr/bin/expr ${i} + 1` done pidnum=`/bin/pidof $1` [ -z "$pidnum" ] || kill -9 $pidnum 2>/dev/null } function fn_kill_by_system() { pidnum=`/bin/pidof $1` [ -z "$pidnum" ] || kill -USR2 $pidnum 2>/dev/null } # bug 221992 222001 221995 function fn_stop_RTRR_Rsync_jobs() { fn_log 'Stop RTRR/Rsync jobs' fn_kill_by_system RTRR fn_kill_by_system rsyncRR.sh } function fn_stop_RTRR() { fn_log 'Stop RTRR' fn_kill RTRR fn_kill RTRR_DAEMON /sbin/daemon_mgr RTRR_MANAGER stop "/usr/bin/RTRR_MANAGER" /usr/bin/killall -q RTRR_MANAGER } function fn_stop_Rsync() { fn_log 'Stop Rsync' fn_kill rsyncRR.sh fn_kill rsyncd fn_kill rsync /bin/cp -f "/etc/config/rsyncd.conf" "$QPKG_ROOT/rsyncd.conf.bak" } function fn_restart_rsyncd() { cd / if [ -f "$QPKG_ROOT/rsyncd.conf.bak" ]; then authmode=$(/sbin/getcfg "" "HBS3 AuthMode" -f "/etc/config/rsyncd.conf" -d "UNKNOWN") if [ "$authmode" = "UNKNOWN" ]; then fn_log "Restore rsyncd config" rsyncd_authmode=$(/sbin/getcfg "" "HBS3 AuthMode" -f "$QPKG_ROOT/rsyncd.conf.bak" -d "RSYNC") rsyncd_qwantag=$(/sbin/getcfg "" "QWanTag" -f "$QPKG_ROOT/rsyncd.conf.bak" -d "0") /sbin/setcfg "" "HBS3 AuthMode" "$rsyncd_authmode" -f "/etc/config/rsyncd.conf" /sbin/setcfg "" "QWanTag" "$rsyncd_qwantag" -f "/etc/config/rsyncd.conf" fi fi fn_log "Restart Rsyncd" /etc/init.d/rsyncd.sh restart 2>/dev/null & } function fn_umount_the_rtrr_mounted_cifs() { fn_log 'Unmount the RTRR mounted CIFS' SAVEIFS=$IFS IFS=$(echo -en "\n\b") rtrr_mount_list=`mount | grep RTRR_CIFS | sed 's/\(.*\) on \(.*\) type .*/\2/'` for i in $rtrr_mount_list do umount -l "$i" done IFS=$SAVEIFS } function fn_nc_register() { fn_log 'NC register' if [ -x $NC_TOOL_BIN ]; then $NC_TOOL_BIN register -p $QPKG_ROOT -P & fi } function fn_nc_unregister() { fn_log 'NC unregister' if [ -x $NC_TOOL_BIN ]; then $NC_TOOL_BIN unregister -A $APP_ID fi } function fn_start_rr2() { fn_log 'Start RR2 server' ${QPKG_ROOT}/rr2/scripts/rr2_server.sh start & } function fn_stop_rr2() { fn_log 'Stop RR2 server' ${QPKG_ROOT}/rr2/scripts/rr2_server.sh stop > /dev/null 2>&1 } function fn_rtrr_upgrade() { fn_log 'Upgrade RTRR server' ${QPKG_ROOT}/rr2/scripts/rr2_server_setting.sh -upgrade } function fn_rtrr_downgrade() { fn_log 'Downgrade RTRR server' ${QPKG_ROOT}/rr2/scripts/rr2_server_setting.sh -downgrade } function fn_rtrr_def_config() { fn_log "Check RTRR default config" if [ ! -d "/etc/config/qsync" ]; then # extract qsync default file and softlink in /etc/config/qsync def_tgz="/mnt/ext/opt/$QPKG_NAME/rtrr_def.tgz" if [ -f "${def_tgz}" ]; then fn_log "->restore rtrr client & server default config" tar zxf ${def_tgz} -C "/etc/config/" &>/dev/null # /etc/config/qsync/logs -> /etc/logs/qsync/ # /etc/logs/qsync realpath is /mnt/HDA_ROOT/.logs/qsync [ ! -d "/etc/logs/qsync" ] && mkdir "/etc/logs/qsync" def_vol=$(/sbin/getcfg -f /etc/config/def_share.info SHARE_DEF defVolMP -d "") if [ -n "${def_vol}" ]; then qsync_home="${def_vol}/.@qsync" [ ! -d "${qsync_home}" ] && mkdir "${qsync_home}" [ ! -L "/etc/config/qsync/home" ] && ln -s "${qsync_home}" "/etc/config/qsync/home" [ ! -d "${qsync_home}/detail-logs" ] && mkdir "${qsync_home}/detail-logs" && chmod 700 "${qsync_home}/detail-logs" [ ! -d "${qsync_home}/timemap" ] && mkdir "${qsync_home}/timemap" && chmod 700 "${qsync_home}/timemap" [ ! -d "${qsync_home}/event" ] && mkdir "${qsync_home}/event" && chmod 744 "${qsync_home}/event" else fn_log "->@@ no default vol" fi else fn_log "->@@ no ${def_tgz}" fi fi if [ ! -L "/etc/qsync" ] ; then fn_log "->create qsync symlink" ln -s "/etc/config/qsync" "/etc/qsync" fi _detail_log="/etc/logs/qsync/detail" if [ ! -L "${_detail_log}" ]; then def_vol=$(/sbin/getcfg -f /etc/config/def_share.info SHARE_DEF defVolMP -d "") if [ -n "${def_vol}" ]; then fn_log "->fix qsync detail log location" qsync_home="${def_vol}/.@qsync" if [ -d "${_detail_log}" ]; then rm -rf "${_detail_log}.dir_bak" 2>/dev/null mv ${_detail_log} ${_detail_log}.dir_bak elif [ -f "${_detail_log}" ]; then fn_log "-> unexcept detail is file" mv ${_detail_log} ${_detail_log}.file fi # check default direcotry and recreate it [ ! -d "${qsync_home}" ] && mkdir "${qsync_home}" [ ! -L "/etc/config/qsync/home" ] && ln -s "${qsync_home}" "/etc/config/qsync/home" [ ! -d "${qsync_home}/timemap" ] && mkdir "${qsync_home}/timemap" && chmod 700 "${qsync_home}/timemap" [ ! -d "${qsync_home}/detail-logs" ] && mkdir "${qsync_home}/detail-logs" && chmod 700 "${qsync_home}/detail-logs" [ ! -d "${qsync_home}/event" ] && mkdir "${qsync_home}/event" && chmod 744 "${qsync_home}/event" ln -s "${qsync_home}/detail-logs" "${_detail_log}" if [ -d "${_detail_log}.dir_bak" ]; then mv ${_detail_log}.dir_bak/* ${_detail_log}/ rm -r ${_detail_log}.dir_bak fi else fn_log "->@@ fix qsync detail log location, but no default vol" fi fi } function fn_check_migration_send_NC() { fn_log 'Check migration send NC' ${QPKG_ROOT}/CloudConnector3/python/bin/python ${QPKG_ROOT}/migration/bin/hbs_migration.pyc send_NC } function fn_backup_hbs2_incoming_job() { fn_log 'Backup HBS 2 incoming job config file' if [ -f $INCOMING_JOB_CONF ]; then /bin/mv $INCOMING_JOB_CONF $INCOMING_JOB_BAK_CONF fi } function fn_restore_hbs2_incoming_job() { fn_log 'Restore HBS 2 incoming job config file' if [ -f $INCOMING_JOB_BAK_CONF ]; then /bin/mv $INCOMING_JOB_BAK_CONF $INCOMING_JOB_CONF fi } function fn_enable_hbs2_rsync_schedule() { fn_log 'Enable HBS2 RSYNC Schedule' /bin/sed -i '/rsyncRR/ s/#//g' /etc/config/crontab /usr/bin/crontab /etc/config/crontab & } function fn_disable_hbs2_rsync_schedule() { fn_log 'Disable HBS2 RSYNC Schedule' /bin/sed -i '/^[^#].*rsyncRR/ s/^/#/g' /etc/config/crontab /usr/bin/crontab /etc/config/crontab & } function fn_delete_RTRR_residue() { rm -f /etc/qsync/qsynchbs3.conf rm -f /etc/qsync/pipe_Job* } function fn_has_BackupStation() { if [ -d "/home/httpd/cgi-bin/apps/backupRestore" ]; then true; return else false; return fi } function fn_delete_debug_log() { rm -f $QPKG_ROOT/$CBS_NAME/data/server/server-debug.log* & } function fn_start_time_machine() { fn_log 'Start Time Machine service' $QPKG_ROOT/timemachine/avahi_tm.sh apply & } function fn_stop_time_machine() { fn_log 'Stop Time Machine service' $QPKG_ROOT/timemachine/avahi_tm.sh stop & } fn_log "$QPKG_NAME $@" case "$1" in start) ENABLED=$(/sbin/getcfg $QPKG_NAME Enable -u -d FALSE -f $CONF) if [ "$ENABLED" != "TRUE" ]; then /bin/echo "$QPKG_NAME is disabled." fn_log "$QPKG_NAME is disabled." exit 1 fi /bin/ln -snf $QPKG_ROOT /mnt/ext/opt/$QPKG_NAME # NC register fn_nc_register # Backup Versioning script fn_HBS_Versioning_script_in_start #delete rtrr residue for abnormal shutdown fn_delete_RTRR_residue # check Nas Model is support hal or snapshot fn_check_nas_model # delete debug log fn_delete_debug_log if [ -f "/etc/init.d/rsyncRR.sh" ] && [ ! -L "/etc/init.d/rsyncRR.sh" ]; then /bin/mv /etc/init.d/rsyncRR.sh /etc/init.d/rsyncRR-BS.sh /bin/ln -snf $QPKG_ROOT/rsyncRR.sh /etc/init.d/rsyncRR.sh fi if [ -f "/etc/init.d/rsyncd.sh" ] && [ ! -L "/etc/init.d/rsyncd.sh" ]; then /bin/mv /etc/init.d/rsyncd.sh /etc/init.d/rsyncd-BS.sh /bin/ln -snf $QPKG_ROOT/rsyncd.sh /etc/init.d/rsyncd.sh fi if [ -f "/etc/init.d/rsyncd_srv.sh" ] && [ ! -L "/etc/init.d/rsyncd_srv.sh" ]; then /bin/mv /etc/init.d/rsyncd_srv.sh /etc/init.d/rsyncd_srv-BS.sh /bin/ln -snf $QPKG_ROOT/rsyncd_srv.sh /etc/init.d/rsyncd_srv.sh fi if [ -f "$QPKG_ROOT/lib/$HAL_MODEL/libgeneralhbs.so.0.0" ]; then /bin/ln -snf $QPKG_ROOT/lib/$HAL_MODEL/libgeneralhbs.so.0.0 /usr/lib/libgeneralhbs.so.0.0 /bin/ln -snf /usr/lib/libgeneralhbs.so.0.0 /usr/lib/libgeneralhbs.so.0 /bin/ln -snf /usr/lib/libgeneralhbs.so.0.0 /usr/lib/libgeneralhbs.so fi if [ -f "$QPKG_ROOT/lib/$HAL_MODEL/libqsynchbs.so.0.0" ]; then /bin/ln -snf $QPKG_ROOT/lib/$HAL_MODEL/libqsynchbs.so.0.0 /usr/lib/libqsynchbs.so.0.0 /bin/ln -snf /usr/lib/libqsynchbs.so.0.0 /usr/lib/libqsynchbs.so.0 /bin/ln -snf /usr/lib/libqsynchbs.so.0.0 /usr/lib/libqsynchbs.so fi /bin/ln -snf $QPKG_ROOT/lib/$HAL_MODEL/libuLinux_qnapaclhbs.so.1.0.0 /usr/lib/libuLinux_qnapaclhbs.so.1 /bin/ln -snf $QPKG_ROOT/lib/$HAL_MODEL/libuLinux_qnapaclhbs.so.1.0.0 $QPKG_ROOT/lib/libuLinux_qnapaclhbs.so.1.0.0 /bin/ln -snf $QPKG_ROOT/bin/$HAL_MODEL/qsync /usr/bin/RTRR /bin/ln -snf $QPKG_ROOT/bin/$HAL_MODEL/qsync /usr/bin/RTRR_DAEMON /bin/ln -snf $QPKG_ROOT/bin/$HAL_MODEL/qsync /usr/bin/RTRR_MANAGER /bin/ln -snf $QPKG_ROOT/bin/$HAL_MODEL/qsync /usr/bin/qsyncutil /bin/ln -snf $QPKG_ROOT/bin/$RSYNC_MODEL/rsync.hbs /usr/bin/rsync.hbs /bin/rm -rf $QPKG_ROOT/bin/rsync /bin/ln -snf $QPKG_ROOT/bin/$RSYNC_MODEL/rsync.hbs $QPKG_ROOT/bin/rsync /bin/ln -snf /usr/bin/rsync.hbs /usr/bin/rsyncd /bin/ln -snf $QPKG_ROOT/bin/rsync.official /usr/bin/rsync.official /bin/ln -snf ${QPKG_ROOT}/ui /home/httpd/cgi-bin/qpkg/${QPKG_NAME} /bin/ln -snf $QPKG_ROOT/rsyncd.sh /etc/init.d/rsyncd.sh /bin/ln -snf $QPKG_ROOT/rsyncd_srv.sh /etc/init.d/rsyncd_srv.sh # start CBS QPKG fn_start_CBS_QPKG # restart Rsync fn_restart_rsyncd # Disable hbs2 rsync schedule fn_disable_hbs2_rsync_schedule fn_start_time_machine # stop BackupStation, since FW may still invoke qsyncd even removing backup station, 2021/04/19 fn_stop_BackupStation # backup HBS 2 incoming job fn_backup_hbs2_incoming_job # check RTRR default config fn_rtrr_def_config # upgrade RTRR server fn_rtrr_upgrade # restart RTRR fn_start_RTRR # start RR2 fn_start_rr2 fn_log 'Wait for all services to start' wait $(jobs -p) fn_log 'All services started' # Check migration send NC fn_check_migration_send_NC ;; stop) fn_stop_time_machine # stop rtrr/rsync jobs fn_stop_RTRR_Rsync_jobs # stop CBS QPKG fn_stop_CBS_QPKG # stop rsync fn_stop_Rsync # stop RR2 fn_stop_rr2 # stop RTRR fn_stop_RTRR # downgrade RTRR server fn_rtrr_downgrade /bin/rm -f /usr/bin/RTRR /bin/rm -f /usr/bin/RTRR_DAEMON /bin/rm -f /usr/bin/RTRR_MANAGER /bin/rm -f /usr/bin/qsyncutil /bin/rm -f /usr/bin/rsync.hbs /bin/rm -rf $QPKG_ROOT/bin/rsync /bin/ln -snf /usr/bin/rsync /usr/bin/rsyncd if [ -f "/usr/bin/qsync" ]; then /bin/ln -snf /usr/bin/qsync /usr/bin/qsyncutil fi /bin/rm -f /etc/init.d/rsyncSpeedTestHBS.sh if [ -f "/etc/init.d/rsyncRR-BS.sh" ] && [ -L "/etc/init.d/rsyncRR.sh" ]; then /bin/rm -f /etc/init.d/rsyncRR.sh /bin/mv /etc/init.d/rsyncRR-BS.sh /etc/init.d/rsyncRR.sh fi if [ -f "/etc/init.d/rsyncd-BS.sh" ] && [ -L "/etc/init.d/rsyncd.sh" ]; then /bin/rm -f /etc/init.d/rsyncd.sh /bin/mv /etc/init.d/rsyncd-BS.sh /etc/init.d/rsyncd.sh fi if [ -f "/etc/init.d/rsyncd_srv-BS.sh" ] && [ -L "/etc/init.d/rsyncd_srv.sh" ]; then /bin/rm -f /etc/init.d/rsyncd_srv.sh /bin/mv /etc/init.d/rsyncd_srv-BS.sh /etc/init.d/rsyncd_srv.sh fi if [ -f "/home/httpd/cgi-bin/backup/qsyncrequest.cgi.old" ] && [ -L "/home/httpd/cgi-bin/backup/qsyncrequest.cgi" ]; then /bin/rm -f /home/httpd/cgi-bin/backup/qsyncrequest.cgi /bin/mv /home/httpd/cgi-bin/backup/qsyncrequest.cgi.old /home/httpd/cgi-bin/backup/qsyncrequest.cgi fi /bin/rm -f /usr/lib/libgeneralhbs.so.0.0 /bin/rm -f /usr/lib/libgeneralhbs.so.0 /bin/rm -f /usr/lib/libgeneralhbs.so /bin/rm -f /usr/lib/libqsynchbs.so.0.0 /bin/rm -f /usr/lib/libqsynchbs.so.0 /bin/rm -f /usr/lib/libqsynchbs.so /bin/rm -f /usr/lib/libuLinux_qnapaclhbs.so.1 /bin/rm -f /home/httpd/cgi-bin/qpkg/${QPKG_NAME} /bin/rm -f /home/httpd/cgi-bin/backup/rr_ncsend_hbs3.sh # umount the rtrr mounted cifs fn_umount_the_rtrr_mounted_cifs # NC unregister # Bug-2253 QPKG disable should not unregister NC, qpkg uninstall will automatically unregister NC # fn_nc_unregister # Backup Versioning script fn_HBS_Versioning_script_in_stop # restore HBS 2 incoming job if fn_has_BackupStation; then fn_restore_hbs2_incoming_job fi # start BackupStation if fn_has_BackupStation; then fn_start_BackupStation fi # Enable hbs2 rsync schedule if fn_has_BackupStation; then fn_enable_hbs2_rsync_schedule fi fn_log 'Wait for all services to stop' wait $(jobs -p) fn_log 'All services stopped' /bin/rm -f /mnt/ext/opt/$QPKG_NAME ;; restart) $0 stop $0 start ;; stop_backup_station) # stop BackupStation fn_stop_BackupStation ;; *) /bin/echo "Usage: $0 {start|stop|restart|stop_backup_station}" exit 1 esac fn_log "$QPKG_NAME $1 finished" exit 0