@@ -219,29 +219,66 @@ resolve_backup_url() {
219219 fi
220220}
221221
222+ FORCE_CONFIRM=0
223+
222224parse_args () {
223- while getopts " :b:p:h " opt; do
225+ while getopts " :b:p:yfh " opt; do
224226 case " ${opt} " in
225227 b) BACKUP_URL=" ${OPTARG} " ;;
226228 p) BACKUP_PASS=" ${OPTARG} " ;;
229+ y|f) FORCE_CONFIRM=1 ;;
227230 h)
228- echo " Usage: sudo $0 -b <backup-url> [-p <backup-password>]"
231+ echo " Usage: sudo $0 -b <backup-url> [-p <backup-password>] [-y] "
229232 exit 0
230233 ;;
231234 \? )
232235 log_err " Invalid option: -${OPTARG} "
233- echo " Usage: sudo $0 -b <backup-url> [-p <backup-password>]"
236+ echo " Usage: sudo $0 -b <backup-url> [-p <backup-password>] [-y] "
234237 exit 1
235238 ;;
236239 :)
237240 log_err " Option -${OPTARG} requires an argument."
238- echo " Usage: sudo $0 -b <backup-url> [-p <backup-password>]"
241+ echo " Usage: sudo $0 -b <backup-url> [-p <backup-password>] [-y] "
239242 exit 1
240243 ;;
241244 esac
242245 done
243246}
244247
248+ check_existing_installation () {
249+ if [[ -d " ${APP_DIR} " ]] || [[ -d " ${DATA_DIR} " ]]; then
250+ log_warn " An existing pg-node installation was detected (${APP_DIR} )."
251+
252+ if [[ " ${FORCE_CONFIRM} " -eq 1 ]]; then
253+ log_info " Non-interactive confirmation flag (-y/-f) set. Proceeding with overwrite..."
254+ elif [[ -t 0 ]]; then
255+ echo -e " ${COLOR_YELLOW} [WARNING] An existing pg-node installation was found on this server.${COLOR_RESET} "
256+ read -r -p " $( echo -e " ${COLOR_BLUE} [INPUT]${COLOR_RESET} Do you want to remove existing pg-node and restore this backup? [y/N]: " ) " choice
257+ case " ${choice} " in
258+ [yY][eE][sS]|[yY])
259+ log_info " User confirmed. Cleaning up existing installation..."
260+ ;;
261+ * )
262+ log_info " Restore cancelled by user."
263+ exit 0
264+ ;;
265+ esac
266+ else
267+ log_err " Existing pg-node installation found. Run with -y to force overwrite in non-interactive mode."
268+ exit 1
269+ fi
270+
271+ if [[ -f " ${APP_DIR} /docker-compose.yml" ]] && command -v docker & > /dev/null && docker compose version & > /dev/null; then
272+ log_info " Stopping existing pg-node stack..."
273+ (cd " ${APP_DIR} " && docker compose down) 2> /dev/null || true
274+ fi
275+
276+ log_info " Removing existing application and data directories..."
277+ rm -rf " ${APP_DIR} " " ${DATA_DIR} "
278+ log_ok " Existing installation cleaned up."
279+ fi
280+ }
281+
245282DOWNLOADER=" "
246283detect_downloader () {
247284 if command -v curl & > /dev/null; then
@@ -430,10 +467,11 @@ main() {
430467 detect_distro
431468 repair_broken_packages
432469 ensure_prerequisites
470+ resolve_backup_url
471+ check_existing_installation
433472 install_pg_node_official
434473 stop_default_stack
435474 detect_downloader
436- resolve_backup_url
437475 download_backup
438476 extract_backup
439477 start_stack
0 commit comments