[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Changing Users in a script



Hal Vaughan wrote:
> I have a system with several different users and would like to use
> cron to run this script as root:
> 
> #!/bin/bash
> 
> for user in `ls /home/`; do
> #	echo "Path: $user"
> 	if [ "${user:0:1}" != "0" ]; then
> 		path="/home/$user/Backup"
> 		if [ -e $path ]; then
> 			echo "Calling backup for user: $user"
> 			sudo -u $user /usr/local/bin/user-backup
> 		fi
> 	fi
> done

Personally if I were writing this then if the script is running as
root then instead of using 'sudo' here I would use 'su' instead.

  su -c /usr/local/bin/user-backup $user

Mostly because su is more traditional and "more core" than sudo and
just seems like the better fit for the job.  But it is a matter of
taste here.

> The idea is that instead of adding a backup script every time I add
> a user, this script will go through the /home directories and skip
> any that start with a 0 (a program I'm using creates some
> directories there, but starts their names with a 0) and
> automatically call the generic backup script for that user.

Seems reasonable so far.  Also you should skip directories called
"lost+found" in the case that /home happens to be a mount point on a
filesystem such as ext[23] that uses lost+found.

> The problem is sudo can't be run without a tty, so I can run it
> myself, but it won't run from a script.

Using 'su' would solve that problem.

> Any other way I can do this?

There are distinct advantages to a backup push system.  Not proposing
that you change away from it.  But I tend to pull backups from /home
to the backup server.  This means that whatever is in /home comes over
whether it is associated with a user's home directory or not.  All I
manage is machines.  Not machines and users.

Bob

Attachment: signature.asc
Description: Digital signature


Reply to: