Backup org files in github
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
*/15 * * * * cd ~/org && ./backup_files_to_github.sh >> ~/cron_log.log 2>&1
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.
-
Create a github repository (repo) with a readme and clone it to your local machine
-
Copy your files into this folder
-
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
-
Make the above
fileshell script
executablesudo chmod +x backup_files_to_github.sh
-
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
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
-
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 -
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
-
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
-
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
-
Install logrotate with homebrew
brew install logrotate # This step might fail, we could still try brew link logrotate
-
Create a new file logrotate.conf and save it with the following content
~/cron_log.log { daily rotate 7 compress }
-
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
-
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
-
Invoke
crontab ~/.crontab
to refresh crontab with the new jobs
That’s a detailed quick guide for backing up files in github.