Forum Moderators: bakedjake

Message Too Old, No Replies

rsync, cronjobs and su to other users

problems

         

jamie

11:35 am on Sep 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi,

i've written a (very basic ;-) backup script which mysqldumps all my databases to a BAK folder and then rsync's to a second location.

i have created a user sync on both machines which works using an ssh key pair to avoid password problems. the script works perfectly when i run it as the user sync from the sync $HOME dir

however when i run it as root, i try to 'su sync and cd' in the middle of the script and then the script just hangs at the prompt. here is an excert:

# if not already sync then su to that user
if [ "`getUID`" -ne 514 ];
then
su sync
cd
fi

# loop through list of databases making dumps

for i in $DB_LIST
do
mysqldump -u root -h localhost --password"mypass" $i ¦ gzip > $BACKUP_DIR/$i.sql.gz"
done

when i execute as user sync, it works perfectly. but when i execute as root (for the cron) it stops at the prompt and won't do the database loop.

can anyone tell me why it hangs and won't continue to execute the loop?

many thanks in advance :-)

jamie

5:14 am on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



well i didn't find an answer to the problem above, but i did get around it by running it as su from cron

su - sync -c "/home/sync/mysql_BAK.sh"

this works fine.

i have since set up rsync as a server - the newest version use ssh by default, so the procedure is slightly different. cheers anyway.

charlier

6:47 am on Sep 7, 2004 (gmt 0)

10+ Year Member



Not quite sure it is/was your problem but often the path setup is different for cron jobs then for command line runs.

jamie

8:17 am on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi charlier,

the problem is that if i run the script as root (or from cron) when the script does this:

su sync
cd

it then stops and doesn't continue. i wonderd by these simply commands should make the script stop running?

charlier

9:38 am on Sep 7, 2004 (gmt 0)

10+ Year Member



Hmm try printing out the $HOME variable at the top of your script and after you do the su sync. Maybe its not getting set correctly. If it isn't then or course the cd might be gettig confused. You could also just put in a path for your cd, ie cd /my/sync/home. I am not a UNIX expert so its just a best guess :-).

Cheers

py9jmas

10:08 am on Sep 7, 2004 (gmt 0)

10+ Year Member



Why don't you add it to the "sync" user's crontab instead of the system crontab?

man 1 crontab should give a break down of your crontab command. If you're using Vixie Cron,

crontab -u sync -e

should bring sync's crontab up in your default editor.

If you insist on using the system crontab, you ought to be able to declare what user the entry should be run as in the crontab. This would avoid needing to use su.

bcc1234

10:39 am on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check that your user has the "remote" host listed in the known hosts.

You can simply ssh to the host while being logged in as that user. Once you see a message saying the host is not listed/known then pick "yes" to add the host to the list.

Once you do that, you should be able to ssh with a key without any problems.

jamie

11:09 am on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>> Why don't you add it to the "sync" user's crontab instead of the system crontab?

doh! i should have thought of that ;-) thanks for the tip py9jmas

bcc1234 - the ssh key works fine when i run the script as the user sync, the problem occurs when i run the script as root, it still hangs after the 'su sync' command

with the following simplified version run as root, the same happens

$ su sync; cd /tmp

the cd /tmp command is never run, the command prompt is simply displayed, as though i only typed 'su sync'.

it has something to do with changing shells: the cd /tmp command belongs to root's shell.

if i run the above command, then exit back to root, i find myself in /tmp - so the command did work, it just didn't apply it to user sync's shell.

this is purely academic now, as the script is working perfectly, would just be interesting to know.

thanks all for the feedback :-)

SkyDog

5:33 am on Sep 9, 2004 (gmt 0)

10+ Year Member



For a backup, why not just tar & zip the data directory, it's much simpler.

jamie

11:44 am on Sep 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi skydog,

>> tar zip

because i am syncing my entire virtual sites folder - about a gig. tar-ing and gzip-ing a gig would bring my server to its knees. it would also mean 30 gigs of traffic extra each month, as tar would transfer the entire archive, whereas rsync transfers only the changed bits. (differential)

cheers

SkyDog

8:54 pm on Sep 12, 2004 (gmt 0)

10+ Year Member



That's what I meant, do an rsync on the data directory instead of dumping all the databases and saving that.

MamaDawg

1:52 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



Hi jamie -

In the examples, you're calling su differently in the script and in the crontab:

su sync (script)
su - sync (crontab)

Just using "su" sets a default PATH. However, using "su -" sets the execution path to the PATH of the new user, which I'm guessing is why your cron entry works...

M.G.

jamie

9:17 am on Sep 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi mamadawg,

thanks for that - it's something i've never been doing, even when logging in as a ssh user and su'ing to root. good to know!

cheers