When I execute the file using ./backuproutine, it errors out with Permission Denied.
I’m logged in as root and I’m able to run the two commands individually just fine. It’s only when I decide to execute the file above that I run into that issue.
abacabb, it is NOT recommended that you as a normal rule login as root. There is no reason to do so as any script can be set to request the root user password and proceed as root for your required tasks. Lets assume you will login as a normal user and then run this script.
#!/bin/bash
#: Title : webbackup
#: Date Created: Sun Sep 19 10:43:13 CDT 2010
#: Last Edit : Sun Sep 19 10:43:13 CDT 2010
#: Author : abacabb
#: Version : 1.00
#: Description :
#: Options :
#
# Make sure we did not start as root
#
if "$1" == "" ] ;
then
if $# -eq 0 ] ; then
echo ""
echo "Do Not Start webbackup as a Root User...."
echo "webbackup will request your password....."
echo ""
exit 1
fi
echo ""
echo "Root User Permissions are required, Please Enter the ..."
echo
sudo "$0" "$HOME"
exit 0
fi
mkdir /root/Desktop/backup/$(date +%F)
cp -r /srv/www/website /root/Desktop/backup/$(date +%F)
exit 0
# End Of Script
Since I do not know for sure you are running the script properly, I will start from scratch. So forgive me if this is not correct. Copy and past the above code into a text editor and then save the file in YOUR ~/bin folder as the file webbackup. Then execute the following Terminal command to make the script executable:
chmod +x ~/bin/webbackup
I have not yet determined if it is advisable to save this information in the root area, but consider that it is not required. To run the script, open up a terminal session and type in the command webbackup, enter the root password and then see if your commands work.
When you log into your computer as root, there is a greater chance that any sort of attach on your system from a network connection could gain full access to your system. When you log in as a normal user, such attacks and their rate of success, would be greatly diminished. Using Linux puts you in much better shape than a Windows user against such attacks, but why challenge fate by always logging in as root?
In general, you can get root permissions anytime you need them to perform required tasks such as backups, but once complete, you are dropped back to a normal user. Think about anytime you might log in as root and then leave your computer running while you eat or even go to bed. To gain the maximum protection for your PC when connected to the internet it is ALWAYS best to log in as a normal user and then run programs which require root access using the terminal commands sudo or su or while in kde use kdesu, but only when you need root access for certain tasks, not ALL of the time.