Skip to content

Commit cb90ff9

Browse files
author
buildplan
committed
Re-run the script with sudo if not already root
1 parent 038890b commit cb90ff9

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

backup_script.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# ===================== v0.35 - 2025.08.21 ========================
2+
# ===================== v0.36 - 2025.08.29 ========================
33
#
44
# Example backup.conf:
55
# BACKUP_DIRS="/home/user/test/./ /var/www/./"
@@ -59,10 +59,12 @@ else
5959
C_CYAN=''
6060
fi
6161

62-
# Check if the script is being run as root
63-
if (( EUID != 0 )); then
64-
echo "❌ This script must be run as root or with sudo." >&2
65-
exit 1
62+
# Re-run the script with sudo if not already root
63+
if [[ $EUID -ne 0 ]]; then
64+
echo -e "${C_BOLD}${C_YELLOW}This script requires root privileges to function correctly.${C_RESET}"
65+
echo -e "${C_YELLOW}Attempting to re-run with sudo. You may be prompted for your password.${C_RESET}"
66+
echo "----------------------------------------------------------------"
67+
exec sudo "$0" "$@"
6668
fi
6769

6870
# --- Determine script's location to load the config file ---

0 commit comments

Comments
 (0)