Trouble with Backup Script

I created a file called backuproutine. In that file I have the following commands:

mkdir /root/Desktop/backup/$(date +%F)
cp -r /srv/www/website /root/Desktop/backup/$(date +%F)

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.

Any ideas? I’m running OpenSuSE 11.1.

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.

Thank You,

it errors out with Permission Denied

Can you post the exact (and complete) error message, please?

On 2010-09-19 18:06, abacabb wrote:

> When I execute the file using ./backuproutine, it errors out with
> Permission Denied.

Is the script marked executable?


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” GM (Minas Tirith))

It is now. That pretty much fixed it. But jdmcdaniel3 stated above that running the script under the root user is a bad thing. Why is that?

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.

Thank You,

On 2010-09-23 02:06, abacabb wrote:

> It is now. That pretty much fixed it. But jdmcdaniel3 stated above that
> running the script under the root user is a bad thing. Why is that?

It is a bad thing to log in as root - which is different.

You should log into the system as a normal user, and then open a terminal as root, or use commands
as kdesu.


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” GM (Elessar))