tl;dr I used the following code snippets to backup my emacs org files. A quick explanation would be - The cron-job invokes backup_files_to_github.sh, once every 15 minutes, to push modified org files in the ~/org directory to a github repository. You could use this to backup any files.

NOWT=$(date +"%Y-%m-%d %T")
git add .
git commit -m "$NOWT Automated git push"
git push
Code Snippet 1: backup_files_to_github.sh

*/15 * * * * cd ~/org && ./backup_files_to_github.sh >> ~/cron_log.log 2>&1
Code Snippet 2: crontab entry

Checkout log rotation to avoid growth of cron_log.log


A better guide for the uninitiated

Backup to git

If you are not familiar with git, please do learn-the-basics-of-git-in-under-10-minutes before you go ahead.

  1. Create a github repository (repo) with a readme and clone it to your local machine

  2. Copy your files into this folder

  3. Copy code snippet 1 to a file named backup_files_to_github.sh and save it in the folder where you have cloned the git repo

  4. Make the above file shell script executable

    sudo chmod +x backup_files_to_github.sh
    
    Code Snippet 3: Giving executable permissions to a file
  5. Make a few changes in your org files. Invoke the above shell script to see your changes getting uploaded to git

    ./backup_files_to_github.sh
    
    Code Snippet 4: Invoking the shell script

Scheduling backup

We will schedule the shell script invocation using crontab (Quick Read on crontab) and cron.

*/15 * * * * cd ~/org && ./backup_files_to_github.sh >> ~/cron_log.log 2>&1
Code Snippet 5: crontab entry
  1. Copy the crontab entry to a file named .crontab in your root directory ~/.crontab. You can choose any directory, I have chosen root here for ease of explanation

  2. Run the following commands

    ## List scheduled cronjobs
    crontab -l
    
    ## Invoke crontab to pick the cronjob entries from your file.
    ## This command can be used to refresh the jobs when you modify the .crontab file as well
    crontab ~/.crontab
    
    ## Check if your cronjobs are listed
    crontab -l
    
    Code Snippet 6: crontab commands
  3. Once you have run these commands, the files will be backed up every 15th minute of an hour hh:15, hh:30, hh:45, hh:00

  4. If you want to change the frequency of your backup, you can play with cron expressions editor to tweak */15 * * * *

Log rotation

You can avoid log creep, unprecedented growth of log files, in two ways

  • Delete this line >> ~/cron_log.log 2>&1 from code-snippet 5 (or)
  • Enable log-rotation for the generated logs

I like to have my logs, so I went with log-rotation and achieved it using logrotate. Here is a 10-second guide

  1. Install logrotate with homebrew

    brew install logrotate
    
    # This step might fail, we could still try
    brew link logrotate
    
    Code Snippet 7: logrotate installation
  2. Create a new file logrotate.conf and save it with the following content

    ~/cron_log.log {
      daily
      rotate 7
      compress
    }
    
    Code Snippet 8: logrotate.conf
  3. Force run log rotation to check if it works

    logrotate --force ~/logrotate.conf
    
    ## If brew link failed before, use this command
    ## You might need to replace 3.17.0 with the version that got installed
    /usr/local/Cellar/logrotate/3.17.0/sbin/logrotate --force ~/logrotate.conf
    
    Code Snippet 9: Invoke logrotation
  4. Logrotate only works if scheduled, so you would need to update your crontab file (code snippet 2) with another entry for logrotate

    */15 * * * * cd ~/org && ./backup_files_to_github.sh >> ~/cron_log.log 2>&1
    0 11 */1 * * /usr/local/Cellar/logrotate/3.17.0/sbin/logrotate ~/tools/logrotate.conf
    
    Code Snippet 10: cronfile with logrotate updated
  5. Invoke crontab ~/.crontab to refresh crontab with the new jobs

That’s a detailed quick guide for backing up files in github.