Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Ubuntu 9.10 image running on Amazon EC2 and I've setup a backup script ec2-consisten-snapshot.

I'm able to run the script from SSH, and everything works peachy.

sudo ec2-consistent-snapshot --mysql --xfs-filesystem /vol vol-xxxxxxx >>/mnt/backup.log 2>&1

However when I schedule a cron job in sudo crontab -e, the script runs but gives me errors.

12 18 4 2 *  ec2-consistent-snapshot --mysql --xfs-filesystem /vol vol-xxxxxxx >>/mnt/backup.log 2>&1

ec2-consistent-snapshot: ERROR: Can't find AWS access key or secret access key at /usr/bin/ec2-consistent-snapshot line 76. xfs_freeze: cannot unfreeze filesystem mounted at /vol: Invalid argument ec2-consistent-snapshot: ERROR: xfs_freeze -u /vol: failed(256)

The AWS access keys are located under $HOME/.awssecret and work fine if you don't run it from cron

Can someone point me what I need to do, I've been trying to figure this out for past week. Also how do I troubleshoot xfs_freeze that works fine from command line.

Thank you very much!

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

sudo crontab -e edits root's crontab right? When you say you have $HOME/.awssecret, what is $HOME? root's home or yours?

You might want to think about using /etc/cron.d, you can additionally add the name of the user to execute the script as in these files (e.g. caution: slightly different syntax for the scripts)

Edit (answering your question in the comment):

Create a file /etc/cron.d/myEc2Crontab

make it contain:

SHELL=/bin/sh
PATH=whatever you need as your path
12 18 4 2 *  root ec2-consistent-snapshot --mysql --xfs-filesystem /vol vol-xxxxxxx >>/mnt/backup.log 2>&1

note the added 'root' just after the time specification, prior to your command. This specifies the user the command runs as.

share|improve this answer
    
when I execute the script from shell I add sudo, so it is being executed as root I believe, so that $HOME should be root's home right? If I go to sudo vi $Home/.awssecere I see the keys there –  McLovin Feb 4 '10 at 18:45
    
well, try 'sudo echo $HOME' –  Olaf Feb 4 '10 at 19:14
    
it shows /home/ubuntu which is not root, how do I map so scripts in the crontab run as root but look for values from ubuntu –  McLovin Feb 4 '10 at 19:33
    
see the edited answer for this. –  Olaf Feb 4 '10 at 19:40
1  
You could also use 'sudo -H'. In 'man sudo' it says: "The -H (HOME) option sets the HOME environment variable to the homedir of the target user (root by default) as specified in passwd(5). By default, sudo does not modify HOME (see set_home and always_set_home in sudoers(5))." –  Olaf Feb 4 '10 at 19:46
show 3 more comments

McLovin: I am the primary author of ec2-consistent-snapshot. If you copy .awssecret to /root/.awssecret it should function as you want. There are a number of other ways to pass in the access key id and secret access key if you look at the manpage.

The xfs_freeze error is only happening because of the previous error. The latest versions of this software given an error if you try to unfreeze a file system which is not already frozen. You can ignore the error, though a patch to fix this type of behavior is already under review.

I would add helpful links here to the various resources, but serverfault does not trust me enough yet :)

share|improve this answer
    
Thanks Eric. After messing around I have figured it out. Pretty basic but took me a while, I'm not that good with Linux. I had to add in path variables in the /etc/cron.d/myetcjon and everything worked like a charm. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin HOME=/home/ubuntu Thanks for your answer, I've upvoted it. –  McLovin Feb 6 '10 at 1:36
add comment

Depending on it's configuration sudo doesn't reset all environment variables when run. For example when I sudo in Ubuntu 9.10 $HOME still points to my user's home directory, not roots!

In your script, replace $HOME with the full path to .awssecret.

share|improve this answer
    
Thanks David, that seems like a great solution. However I'm I don't know how to edit that program. Any ideas why xfs_freeze is also failing? Upvoting your answer –  McLovin Feb 4 '10 at 19:35
    
I've looked at ec2-consistent-snapshot (a perl program) and it appears to support command line options that allow you to specify the location of the files. I suggest your use these instead of relying on $HOME. The options are --aws-access-key-id-file and --aws-secret-access-key-file. I never used xfs_freeze, sorry. –  David Feb 6 '10 at 3:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.