2

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!

McLovin
  • 143
  • 8

3 Answers3

3

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 :)

Eric Hammond
  • 11,163
  • 1
  • 36
  • 56
  • 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 06 '10 at 01:36
2

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.

Olaf
  • 908
  • 5
  • 7
  • 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 04 '10 at 18:45
  • well, try 'sudo echo $HOME' – Olaf Feb 04 '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 04 '10 at 19:33
  • see the edited answer for this. – Olaf Feb 04 '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 04 '10 at 19:46
  • SHELL=/bin/sh PATH=/home/ubuntu 03 20 4 2 * root /usr/bin/ec2-consistent-snapshot --mysql --xfs-filesystem /vol vol-xxxxxxa >>/mnt/backup.log 2>&1 I get the same error Can't find AWS access key or secret access key at /usr/bin/ec2-consistent-snapshot line 76. – McLovin Feb 04 '10 at 20:15
  • with PATH being set to /home/ubuntu: Is your .awssecret located in /home/ubuntu or in /root? I believe it should be in /root when you execute this way as root. You could also define HOME the same way as you do SHELL or PATH - I didn't mention this earlier – Olaf Feb 04 '10 at 21:01
  • you are the man, after messing around i figure it out it needed SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin HOME=/home/ubuntu Thank you very much! – McLovin Feb 04 '10 at 21:59
1

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.

David
  • 3,555
  • 22
  • 17
  • 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 04 '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 06 '10 at 03:33