Git auto-commit with Crontab

by Adrian Mejia in August 15, 2011 and last update was in August 24, 2011

You might want to commit from a git repository from time to time for several purposes (backup, control version, continuous integration, etc). One way to accomplish that is using the CronTab (Cron is used to run periodic task in Unix-like systems). Here is an example. 1.- Create an script. e.g. with:

# MySQL-dump: save a copy of the actual content in the database. (this is for a Drupal site, the backup is done using drupal's drush)
php -c ~/www/php.ini  ~/drush/drush.php -r /home/adrimej0/www -u 1 sql-dump --result-file=latest.sql

# Git: add and commit changes
cd /home/adrimej0/www && /home/adrimej0/opt/bin/git commit -a -m "weekly crontab backup `date`"

# send data to Git server
cd /home/adrimej0/www && /home/adrimej0/opt/bin/git push origin master
2.- Set up the cron. In the shell write the following
$ crontab -e
add the following command line for weekly auto-commits:
0 0 * * 0 /home/adrimej0/www/
Done. Now your (drupal) site will be backed up every week automatically (every Sunday at midnight).

Tags: drupal, how-to, git, unix, crontab, drush

Add a new comment