4

To backup my sql database I have a cron job running that makes a mysql dump. Now I want to create another cron job that commits this dump to the repo, the problem now is that it doesn't work.

In the cron log I see the line where the command gets run but nothing is committed.

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
MAILTO=root
HOME=/

49 16 * * * cd /path/to/backup/folder/ && /usr/local/bin/git commit -am "Updating DB backup"

This is what I put in crontab -e. I also tried this in /etc/crontab, where I put root as user and that works but only as root user which is a problem because of the git post-commit hook that does push after something is commited. Since only my user is allowed to push this doesn't work when root doest the commit.

Since its working with the root user I'm thinking it's a permissions problem, but permissions of what? These commands work in normal command line.

When catching the stdout and stderr I get the following in the log.

Permission denied (publickey).^M
fatal: The remote end hung up unexpectedly

so it doesn't really runs as my user or can't find the key?

2
  • Please clarify your question. You're using push and commit interchangeably and they have nothing in common. For example, I could see a problem with push if it where pushing to a remote ssh target as root, just like root access to any server might be blocked. If you just focus on commit, that's updating the local index, which you likely own e.g. .git/, so permissions shouldn't matter at all there. Also please provide the cron logs surrounding the incident.
    – ppetraki
    Mar 6, 2012 at 16:16
  • I got it working by creating a new key without passphrase for the root user and set up the cron job for him.
    – dazz
    Mar 8, 2012 at 10:19

2 Answers 2

3

The format for your cron file (which is what you get when you type crontab -e) is different from the system cron file. You don't need to specify user in the user cron file, it is automatically run as you. Try:

49 16 * * * cd /path/to/backup/folder/ && /usr/local/bin/git commit -am "Updating DB backup"

I would also suggest catching stdout and stderr so that you can actually see what the commit has done, for example:

49 16 * * * cd /path/to/backup/folder/ && /usr/local/bin/git commit -am "Updating DB backup" >> /path/to/logfile 2>&1

Edited to add: Now that you've shown the output from stdout/stderr, the problem is clear. ssh is not finding your keys, which is likely due to you setting

HOME=/

It should probably be something like

HOME=/home/me

Alternatively, you could probably just leave it unset.

2
  • added the output to the question
    – dazz
    Mar 7, 2012 at 10:47
  • doesn't fix it, i also add /home/me/.ssh to the PATH
    – dazz
    Mar 7, 2012 at 15:39
1

You probably are setting the crontab using another user that not you. Check if you are doing "sudo crontab -e" and try just "crontab -e" with your user or whoever is the owner of the git and files.

This sorted me out so maybe it will work for you too.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .