Got question on how to set CRONJOB to auto git commits changes on daily basis? you might probably want to implement this for your WordPress project right or may be for some different projects or code, eventually the point is you want to secure your changes on git so that if someone hacked your server and did some updates like deleting the files or modifying the files you don’t have to worry about if you loss your server backup the git commits will always be there for you to reset the project files from appropriate commit.
Hacking could not be the only reason there are a lot of points and keeping backup of updated changes will be always better.
So just be with me, In this tutorial I will give you details along with script to achieve this task on ubuntu server.
1: Configure SSH Keys:
Note – if you have already configured your server ssh keys to your development platform ex. Github. then you can jump on to next step you don’t have to do it again. if not go ahead and follow next instructions.
SSH to your server using terminal where you have your project up and running, make sure you have sudoers access.
Get into root account:
$ sudo su
Now we will have to generate new SSH key and configure with git provider it can be Github, Gitlab or Bitbucket etc.
$ ssh-keygen -t rsa -b 4096 -C "firstname.lastname@example.org"
Press enter to generate the key and use this command to print the newly generated key on terminal like showing in this screen:
$ cat ~/.ssh/id_rsa.pub
Now copy the key you have generate and login to your repository and go to the profile settings and ssh key section, in my case I am using bitbucket it can be different for you if you are one some other different platform but steps should be probably same.
i) Login to your account:
ii) Add new key:
2: Git Commits Script:
Now will have to write commits script into the executable file, before going to do that make sure you have a complete path of your project from server.
create new file called commits.sh and open into edit mode and paste following script make sure to update project path according to yours.
# Git: add and commit changes cd /var/www/project-name && /usr/bin/git add --all && /usr/bin/git commit -m "daily crontab backup `date`" # send data to Git server cd /var/www/project-name && /usr/bin/git push origin master
Save this file and close and note down the location I mean the full path of the this file where you have stored, in my case I have it on /root/commits.sh location.
Now we need to make sure the file is executable so let’s give execute permission to commits.sh file using following command:
$ chmod +x /root/commits.sh
3: Update Crontab:
Now here it comes into cron job, we needs to set up new job to execute on daily basis.
Use following command to edit crontab file:
$ crontab -e
Add this line at the end of the crontab file:
30 6 * * * /root/commits.sh
Save and close the file and we are done, our new script will execute daily at 6:30 AM and all modifications from the project will be sent to the git without any issues.
Let me know if you have any questions.
If you find this tutorial helpful, make sure to like and share with other sharing is caring, share knowledge, also don’t forget to comment in the comment box below.
Next Recommended Learning – Automate Database Backup with Email Notifications using PHP CRON Job