Skip to content

Commit 2bc9796

Browse files
committed
Add uninstall option
- Added uninstall option [ -u ]
1 parent 6f85a2e commit 2bc9796

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

invidious_installer.sh

+201
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ while [ $# != 0 ]; do
125125
shift
126126
BANNERS=0
127127
;;
128+
--uninstall | -u)
129+
shift
130+
mode="uninstall"
131+
;;
128132
*)
129133
printf "Unrecognized option: $1\\n\\n"
130134
usage
@@ -682,6 +686,203 @@ GetMaster() {
682686
git checkout origin/${IN_BRANCH} -B ${IN_BRANCH} >>"${RUN_LOG}" 2>&1
683687
}
684688

689+
# Get dbname from config file (used in db maintenance and uninstallation)
690+
get_dbname() {
691+
echo "$(sed -n 's/.*dbname *: *\([^ ]*.*\)/\1/p' "$1")"
692+
}
693+
694+
uninstall_invidious() {
695+
# Set default uninstallation parameters
696+
RM_PostgreSQLDB=${RM_PostgreSQLDB:-y}
697+
RM_RE_PGSQLDB=${RM_RE_PGSQLDB:-n}
698+
RM_PACKAGES=${RM_PACKAGES:-n}
699+
RM_PURGE=${RM_PURGE:-n}
700+
RM_FILES=${RM_FILES:-y}
701+
RM_USER=${RM_USER:-n}
702+
# Set db backup path
703+
PGSQLDB_BAK_PATH="/home/backup/$USER_NAME"
704+
# Get dbname from config.yml
705+
RM_PSQLDB=$(get_dbname "${IN_CONFIG}")
706+
707+
read -p "Express uninstall ? [y/n]: " EXPRESS_UNINSTALL
708+
709+
if [[ ! $EXPRESS_UNINSTALL = "y" ]]; then
710+
echo ""
711+
read -e -i "$RM_PostgreSQLDB" -p " Remove database for Invidious ? [y/n]: " RM_PostgreSQLDB
712+
if [[ $RM_PostgreSQLDB = "y" ]]; then
713+
echo -e " ${ORANGE}${WARNING} (( A backup will be placed in ${ARROW} $PGSQLDB_BAK_PATH ))${NORMAL}"
714+
echo -e " Your Invidious database name: $RM_PSQLDB"
715+
fi
716+
if [[ $RM_PostgreSQLDB = "y" ]]; then
717+
echo -e " ${ORANGE}${WARNING} (( If yes, only data will be dropped ))${NORMAL}"
718+
read -e -i "$RM_RE_PGSQLDB" -p " Do you intend to reinstall?: " RM_RE_PGSQLDB
719+
fi
720+
read -e -i "$RM_PACKAGES" -p " Remove Packages ? [y/n]: " RM_PACKAGES
721+
if [[ $RM_PACKAGES = "y" ]]; then
722+
read -e -i "$RM_PURGE" -p " Purge Package configuration files ? [y/n]: " RM_PURGE
723+
fi
724+
echo -e " ${ORANGE}${WARNING} (( This option will remove ${ARROW} ${REPO_DIR} ))${NORMAL}"
725+
read -e -i "$RM_FILES" -p " Remove files ? [y/n]: " RM_FILES
726+
if [[ "$RM_FILES" = "y" ]]; then
727+
echo -e " ${RED}${WARNING} (( This option will remove ${ARROW} $USER_DIR ))${NORMAL}"
728+
echo -e " ${ORANGE}${WARNING} (( Not needed for reinstall ))${NORMAL}"
729+
read -e -i "$RM_USER" -p " Remove user ? [y/n]: " RM_USER
730+
fi
731+
echo ""
732+
echo -e "${GREEN}${ARROW} Invidious is ready to be uninstalled${NORMAL}"
733+
echo ""
734+
read -n1 -r -p "press any key to continue or Ctrl+C to cancel..."
735+
echo ""
736+
fi
737+
# Remove PostgreSQL database if user ANSWER is yes
738+
if [[ "$RM_PostgreSQLDB" = 'y' ]]; then
739+
# Stop and disable invidious
740+
${SUDO} $SYSTEM_CMD stop ${SERVICE_NAME}
741+
read_sleep 1
742+
${SUDO} $SYSTEM_CMD restart ${PGSQL_SERVICE}
743+
read_sleep 1
744+
# If directory is not created
745+
if [[ ! -d $PGSQLDB_BAK_PATH ]]; then
746+
echo -e "${ORANGE}${ARROW} Backup Folder Not Found, adding folder${NORMAL}"
747+
${SUDO} mkdir -p $PGSQLDB_BAK_PATH
748+
fi
749+
750+
echo ""
751+
echo -e "${GREEN}${ARROW} Running database backup${NORMAL}"
752+
echo ""
753+
754+
${SUDO} -i -u postgres pg_dump ${RM_PSQLDB} > ${PGSQLDB_BAK_PATH}/${RM_PSQLDB}.sql
755+
read_sleep 2
756+
${SUDO} chown -R 1000:1000 "/home/backup"
757+
758+
if [[ "$RM_RE_PGSQLDB" != 'n' ]]; then
759+
echo ""
760+
echo -e "${RED}${ARROW} Dropping Invidious PostgreSQL data${NORMAL}"
761+
echo ""
762+
${SUDO} -i -u postgres psql -c "DROP OWNED BY kemal CASCADE;"
763+
echo ""
764+
echo -e "${ORANGE}${CHECK} Data dropped and backed up to ${ARROW} ${PGSQLDB_BAK_PATH}/${RM_PSQLDB}.sql ${NORMAL}"
765+
echo ""
766+
fi
767+
768+
if [[ "$RM_RE_PGSQLDB" != 'y' ]]; then
769+
echo ""
770+
echo -e "${RED}${ARROW} Dropping Invidious PostgreSQL database${NORMAL}"
771+
echo ""
772+
${SUDO} -i -u postgres psql -c "DROP DATABASE $RM_PSQLDB"
773+
echo ""
774+
echo -e "${ORANGE}${CHECK} Database dropped and backed up to ${ARROW} ${PGSQLDB_BAK_PATH}/${RM_PSQLDB}.sql ${NORMAL}"
775+
echo ""
776+
echo -e "${RED}${ARROW} Removing user kemal${NORMAL}"
777+
${SUDO} -i -u postgres psql -c "DROP ROLE IF EXISTS kemal;"
778+
fi
779+
fi
780+
781+
# Reload Systemd
782+
${SUDO} $SYSTEM_CMD daemon-reload
783+
# Remove packages installed during installation
784+
if [[ "$RM_PACKAGES" = 'y' ]]; then
785+
echo ""
786+
echo -e "${ORANGE}${ARROW} Removing packages installed during installation."
787+
echo ""
788+
echo -e "Note: PostgreSQL will not be removed due to unwanted complications${NORMAL}"
789+
echo ""
790+
791+
if ${PKGCHK} $UNINSTALL_PKGS >/dev/null 2>&1; then
792+
for i in $UNINSTALL_PKGS; do
793+
echo ""
794+
echo -e "${ORANGE}${ARROW} removing packages.${NORMAL}"
795+
echo ""
796+
${UNINSTALL} $i 2> /dev/null
797+
done
798+
fi
799+
echo ""
800+
echo -e "${GREEN}${CHECK} done.${NORMAL}"
801+
echo ""
802+
fi
803+
804+
# Remove conf files
805+
if [[ "$RM_PURGE" = 'y' ]]; then
806+
# Removing invidious files and modules files
807+
echo ""
808+
echo -e "${ORANGE}${ARROW} Removing invidious files and modules files.${NORMAL}"
809+
echo ""
810+
if [[ $DISTRO_GROUP == "Debian" ]]; then
811+
rm -r \
812+
/lib/systemd/system/${SERVICE_NAME} \
813+
/etc/apt/sources.list.d/crystal.list
814+
elif [[ $DISTRO_GROUP == "RHEL" ]]; then
815+
rm -r \
816+
/usr/lib/systemd/system/${SERVICE_NAME} \
817+
/etc/yum.repos.d/crystal.repo
818+
fi
819+
820+
if ${PKGCHK} $UNINSTALL_PKGS >/dev/null 2>&1; then
821+
for i in $UNINSTALL_PKGS; do
822+
echo ""
823+
echo -e "${ORANGE}${ARROW} purging packages.${NORMAL}"
824+
echo ""
825+
${PURGE} $i 2> /dev/null
826+
done
827+
fi
828+
829+
echo ""
830+
echo -e "${ORANGE}${ARROW} cleaning up.${NORMAL}"
831+
echo ""
832+
${CLEAN}
833+
echo ""
834+
echo -e "${GREEN}${CHECK} done.${NORMAL}"
835+
echo ""
836+
fi
837+
838+
if [[ "$RM_FILES" = 'y' ]]; then
839+
# If directory is present, remove
840+
if [[ -d ${REPO_DIR} ]]; then
841+
echo -e "${ORANGE}${ARROW} Folder Found, removing folder${NORMAL}"
842+
rm -r ${REPO_DIR}
843+
fi
844+
fi
845+
846+
# Remove user and settings
847+
if [[ "$RM_USER" = 'y' ]]; then
848+
# Stop and disable invidious
849+
${SUDO} $SYSTEM_CMD stop ${SERVICE_NAME}
850+
read_sleep 1
851+
${SUDO} $SYSTEM_CMD restart ${PGSQL_SERVICE}
852+
read_sleep 1
853+
${SUDO} $SYSTEM_CMD daemon-reload
854+
read_sleep 1
855+
grep $USER_NAME /etc/passwd >/dev/null 2>&1
856+
857+
if [ $? -eq 0 ] ; then
858+
echo ""
859+
echo -e "${ORANGE}${ARROW} User $USER_NAME Found, removing user and files${NORMAL}"
860+
echo ""
861+
shopt -s nocasematch
862+
if [[ $DISTRO_GROUP == "Debian" ]]; then
863+
${SUDO} deluser --remove-home $USER_NAME
864+
fi
865+
if [[ $DISTRO_GROUP == "RHEL" ]]; then
866+
/usr/sbin/userdel -r $USER_NAME
867+
fi
868+
fi
869+
fi
870+
if [ -d /etc/logrotate.d ]; then
871+
rm /etc/logrotate.d/invidious.logrotate
872+
fi
873+
# We're done !
874+
echo ""
875+
echo -e "${GREEN}${CHECK} Un-installation done.${NORMAL}"
876+
echo ""
877+
read_sleep 3
878+
tput cnorm
879+
exit 0
880+
}
881+
882+
if [ "$mode" = "uninstall" ]; then
883+
uninstall_invidious
884+
fi
885+
685886
install_invidious() {
686887

687888
chk_git_repo

0 commit comments

Comments
 (0)