5

I believe it's a simple question, but I've added a folder as Origin Master just as this guide oriented.

I created a file backupgit.cron and inside it the following commands:

cd /var/bkpfolder/
git add .
git commit -m "another commit"
git push origin master

And at crontab -e I included the line bellow:

30 * * * * /var/bkpfolder/backupgit.cron

The thing is, the (new) files I add to test this crontab are never uploaded... If i run the commands at the backupgit.cron manually they do work.

I believe that it's something to do with the going to the origin master folder (cd /var/bkpfolder/?) to execute the push command, so what I'm looking for is a correct way to execute the push commands to the /bkpfolder/ stated in the backupgit.cron

thanks in advance!

mrbTT
  • 1,399
  • 1
  • 18
  • 31
  • 1
    A few things... First and most important: capture the output from the cron job. Seeing any error output will be critical to diagnosing. Second, are you sure of what you're doing? Having a cron job do `git add .` seems very dangerous to me. Lastly, it's not clear what you mean about "setting up an origin master folder"? Origin is (usually) a remote; master is (usually) a branch. A folder is neither of these things. Maybe it would help to know what steps from the linked document you mean to be following. – Mark Adelsberger Feb 21 '17 at 13:38
  • @MarkAdelsberger ,everything that folder will have are the files I need to push. I did all the steps the link I provided stated and the only way I managed to push was following all the steps I stated are inside the `backupgit.cron`. I don't have to follow those exact steps, if there is another way to push all files added to that folder periodically that's the solution for me. – mrbTT Feb 21 '17 at 13:51
  • Is your origin `Master` as metioned in the first sentence or `master` as mentioned in the script? Maybe it is only a typo? – Roman Feb 21 '17 at 14:24
  • just `master`.. – mrbTT Feb 21 '17 at 14:37

2 Answers2

6

I figured it out what I was doing wrong... As I stated, the commands inside the backupgit.cron worked when I was inside the folder (read as not the default folder crontab is)... So I just figured out that at the beggining of the gitcommand I can specify my folder of action as in git -C /var/bkpfolder/

So I replaced what was inside of the backupgit.cron from:

cd /var/bkpfolder/
git add .
git commit -m "another commit"
git push origin master

To:

git -C /var/bkpfolder/ add .
git -C /var/bkpfolder/ commit -m "another commit"
git -C /var/bkpfolder/ push origin master

+

Added permissions to read/write the file a the user the crontab was written to

mrbTT
  • 1,399
  • 1
  • 18
  • 31
  • check out my example. you do not need to add `-C /var/bkpfolder`. – Roman Feb 21 '17 at 20:34
  • your solution only worked for me when I was inside the new 'main_backup'... it didn't work at cron unless I specified the folder with the `-C`command :/ – mrbTT Feb 22 '17 at 19:07
  • But for me, it miss the passphrase handling – Mahefa Mar 03 '21 at 08:53
  • @Mahefa, I believe you can store your git credentials for the user.. After signing in, try this response: https://stackoverflow.com/questions/35942754/how-to-save-username-and-password-in-git – mrbTT Mar 04 '21 at 12:36
  • @mrbTT > Thanks. I have removed the passphrase cause I can. I will try this when I have a case where I can't remove passphrase – Mahefa Mar 04 '21 at 12:47
0

Your origin master has to be either a bare-repository or the branch may not be checked out.

If it is not a bare-repository and the branch which will be pushed is checked out, you will get the following error:

git push origin master --force
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set 'receive.denyCurrentBranch' configuration variable to
remote: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /Users/Roman/gittest/master
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/Users/Roman/gittest/master'

Solution

If you do not want to use a bare-repository, you have to checkout a branch that differs between your bkpfolder and your master-directory.

Out of the box example

For better undestanding i renamed the remote master to origin.

mkdir main_backup
cd main_backup && git init --bare && cd ..
git clone main_backup bkpfolder
cd bkpfolder && echo "This is a test" >> test.txt
git add .
git commit -m 'Automatic backup'
git push origin master
Roman
  • 2,530
  • 2
  • 27
  • 50
  • The command `git push origin master` only works if I'm inside the `/var/bkpfolder/`, I'm not sure I follow what I'm intended to do... consider that I'm not a git expert, I just received an url that I've set as `remote repository` while I was at the `bkpfolder`. – mrbTT Feb 21 '17 at 14:40
  • `bkpfolder` is the folder you want to backup and you want to push the changes to your `master` directory which is the remote `origin`. – Roman Feb 21 '17 at 14:41
  • Right... and how do I do that? – mrbTT Feb 21 '17 at 14:47