From cab6df399a781727f31102fc56729c341248e6a7 Mon Sep 17 00:00:00 2001 From: myvesta <38690722+myvesta@users.noreply.github.com> Date: Thu, 29 Dec 2022 12:57:17 +0100 Subject: [PATCH] Create v-move-folder-and-make-symlink --- bin/v-move-folder-and-make-symlink | 85 ++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 bin/v-move-folder-and-make-symlink diff --git a/bin/v-move-folder-and-make-symlink b/bin/v-move-folder-and-make-symlink new file mode 100644 index 000000000..f543ff655 --- /dev/null +++ b/bin/v-move-folder-and-make-symlink @@ -0,0 +1,85 @@ +#!/bin/bash + +# info: +# This script will move a folder to the new destination and make a symlink from the old path to the new destination + +# options: FROMFOLDER TOFOLDER + +#----------------------------------------------------------# +# Variable&Function # +#----------------------------------------------------------# + +whoami=$(whoami) +if [ "$whoami" != "root" ] && [ "$whoami" != "admin" ] ; then + echo "You must be root or admin to execute this script"; + exit 1; +fi + +# Argument definition +FROMFOLDER=$1 +TOFOLDER=$2 + +# Includes +source $VESTA/func/main.sh + +#----------------------------------------------------------# +# Verifications # +#----------------------------------------------------------# + +# Trimming the ending slash, just in case +FROMFOLDER=$(echo "$FROMFOLDER" | sed 's:/*$::') +TOFOLDER=$(echo "$TOFOLDER" | sed 's:/*$::') + +if [ ! -d "$FROMFOLDER" ]; then + echo "Folder $FROMFOLDER does not exists, aborting" + exit 1 +fi + +USER=$(stat -c '%U' "$FROMFOLDER") +GROUP=$(stat -c '%G' "$FROMFOLDER") +PARENTFOLDER=$(dirname "$TOFOLDER") + +if [ ! -d "$PARENTFOLDER" ]; then + PUSER=$(stat -c '%U' "$PARENTFOLDER") + PGROUP=$(stat -c '%G' "$PARENTFOLDER") + echo "= Creating parent folder..." + mkdir -p $PARENTFOLDER + chown $PUSER:$PGROUP $PARENTFOLDER +fi + +#----------------------------------------------------------# +# Action # +#----------------------------------------------------------# + +rsync -a "$FROMFOLDER/" "$TOFOLDER/" +# with slashes on the end of the path of both folders +if [ "$?" -ne 0 ]; then + echo "Error happened, aborting" + exit 1 +fi + +if [ "$FROMFOLDER" = "/home/$USER" ] && [ -d "$FROMFOLDER/conf" ]; then + # if we are moving myVesta home folder, we must remove immutable attribute from conf/ files + chattr -R -i "$FROMFOLDER/conf/" > /dev/null 2>&1 + # with slashes on the end of the path of the folder +fi + +rm -rf "$FROMFOLDER" +# without slash on the end of the path of the folder + +ln -s "$TOFOLDER" "$FROMFOLDER" +# without slashes on the end of the path of both folders + +chown -h $USER:$GROUP $FROMFOLDER +# without slash on the end of the path of the folder + +#----------------------------------------------------------# +# Log and print result # +#----------------------------------------------------------# + +echo "Done, folder $FROMFOLDER moved to $TOFOLDER and symlinked" + +# Logging +log_event "$OK" "$ARGUMENTS" + +exit