Forum Moderators: bakedjake
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 :-)
Cheers
man 1 crontab should give a break down of your crontab command. If you're using Vixie Cron,
crontab -u sync -e 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.
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.
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 :-)
>> 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