PDA

View Full Version : Scripting Help



jimmy20013
10th September 2006, 05:13 PM
Here is a little script I wrote for backing up some files from my network


#!/bin/bash
#This script back's up Star Must Save and Work folders from Star's Hardrive.
mount -t cifs //192.168.10.254/"Star Hardrive" /home/sidd/Desktop/USB -o password=xxxxxxxxxxxxxxxxxxxxx
cp -r /home/sidd/Desktop/USB/"Documents and Settings"/ICC/Desktop/WORK /backup
cp -r /home/sidd/Desktop/USB/"Documents and Settings"/ICC/Desktop/"Star Must Save" /backup
umount //192.168.10.254/"Star Hardrive" /home/sidd/Desktop/USB


I know to run this script I need root priveleges. My question is, how do I add su command to this script so that it will take the password from another file which has the password. Also will the script wait till the copying is done before unmounting the share.

Thank you.

daverj
10th September 2006, 09:43 PM
First, if you are running the script as root, you do not need to enter a password when you su to another user. Second, you can use the -c option to run the command as another user. Try the following as the root user:

su - whateverUserNameYou -c id

When I do it, it looks like this:

[root@andromeda ~]# su - davidj -c id
uid=500(davidj) gid=100(users) groups=10(wheel),100(users)
[root@andromeda ~]#

Notice 2 things: 1) as the root user I was not prompted for the users password. 2) after the command executed I was still the root user. This will enable me to run multiple commands as root in another users name. This might be a workable solution if you need to do a number of things as different users.

Davidj

jimmy20013
11th September 2006, 02:18 AM
Thanks for the reply and I had considered what you said as a solution but I don't want to run this script as root. I want to run this script as a normal user because I am going to add it to my daily cron jobs.

jimmy20013
11th September 2006, 04:51 PM
I know someone must know how to do this.

VictorienSardou
11th September 2006, 05:13 PM
look at sudo, sudoers and visudo.
You can setup a particular user to run the script as root without having to put the root password in a seperate file.

jimmy20013
11th September 2006, 08:34 PM
Thanks for the pointer dude. I solved the problem by adding
%user ALL=ALL NOPASSWD:/bin/mount, /bin/umount to /etc/sudoers

and using sudo mount in the script instead of only mount.
Here is the code if anyone is interested.


#!/bin/bash

#This script back's up Star Must Save and Work folders from Star's Hardrive.
star="//192.168.10.254/SHD"
mount1="/home/sidd/Desktop/USB"

#This will mount Star's Hard Drive
sudo mount -t cifs $star $mount1 -o password=xxxxxxxxxxxxxxxxxx


#This will update the files if they have been modified or copy them if they dont# exist.
cp -r -u $mount1/"Documents and Settings"/ICC/Desktop/WORK /backup
cp -r -u $mount1/"Documents and Settings"/ICC/Desktop/"STAR MUST SAVE" /backup

sudo umount -t cifs $star $mount1
exit 0

a thing
12th September 2006, 06:02 AM
Add the thing you need to mount to /etc/fstab with the option users.

uth
12th September 2006, 05:13 PM
Thanks for the reply and I had considered what you said as a solution but I don't want to run this script as root. I want to run this script as a normal user because I am going to add it to my daily cron jobs.

I'm not sure I understand the thinking behind this. There's no security advantage to not using root's cron, especially as you were planning to read the root pw from a file.

Is it just because you want all your cron jobs under a single user?

jimmy20013
12th September 2006, 10:35 PM
I'm not sure I understand the thinking behind this. There's no security advantage to not using root's cron, especially as you were planning to read the root pw from a file.

Is it just because you want all your cron jobs under a single user?
Yeah, I want all the cron jobs under the same user.I have other cron jobs running under a normal user and I wanted this cron to run under the same user as well.

Gave me an opportunity to learn something new as well.