Have you ever tried to backup your folder to cloud and didn’t like the way it backups all the time? This occurred to me when I was trying to sync my obsidian vault using google drive. After a lot of research, I found that syncing with github to be the best option for me. And here’s how I did it.
Setting up the Repository
This is something you might have done many times if you’ve worked on open source projects. Here are the steps to creating a github repository.
Creating a Github Repository
- Sign in to github and click on the ’+’ icon on the top right corner.
- Click on New Repository
- Give a name to the repository, make it private and click on Create Repository.
- Copy the url of the repository
Pusing the Local Folder to Github
- Fire up a terminal and navigate to the folder you want to sync with github
-
Enter the following commands in the terminal
git init git remote add origin <url> git add --all git commit -m "Initial Commit" git push --set-upstream origin <branch>
Here <url> is the url of the github repository that you’ve already copied and <branch> is the name of the branch you’re working on, which in general is
main
ormaster
. -
Next time you make a change to your folder, run the following commands:
git add --all git commit -m "some_commit_message" git pull git push
Sync with Github
Doing this every time you make a change to your folder is very uncomfortable. So the idea is to automate the task. ie, to push the code to github at regular intervals if there is any change in your local folder.
Creating a Script to Commit the Changes
#!/bin/bash
gstatus=`git status --porcelain`
if [ ${#gstatus} -ne 0]
then
git add --all
git commit -m "$gstatus"
git pull
git push
fi
Write this code to a file called sync.sh
in your local folder. Give it executable permission using chmod u+x sync.sh
. Now run the script using ./sync.sh
Automate Syncing
We can automate the syncing process using a linux tool called crontab. This is built into linux system. Crontab uses the cron daemon to schedule tasks. These are the steps to schedule a job.
- Create a text file
sync-cron.txt
. -
Add the following text to the file
*/20 * * * * ~/<absolute_path_to_sync_file>/sync.sh
- Schedule a cron job using the command
crontab sync-cron
Here’s a video from Corey-Schafer explaining how to schedule tasks with crontab.
Bug Hunting in Scheduling
Now many times people get frustrated that their crontab is not running the scripts. This has happened to me the first time I did it. Here’s how I fixed it.
-
The most common issues are with the PATH variable being different from script and cron. To fix this I added the following to crontab
SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin */20 * * * * ~/<absolute_path_to_sync_file>/sync.sh
-
Now in the script I added the PATH like this
#!/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin export DISPLAY=:0.0 <rest_of_your_script>
-
You can also use an absolute path to your local folder with git cli.
git -C <absolute_path_to_local_folder> add --all
A Word About Security
Is github safe? This requires another question to be asked. Is google drive, onedrive or dropbox safe? There is no answer to that. It is only them who know if it is safe. There is always a risk in storing personal data in cloud services. It would be a wise to encrypt your data before backing it to github if you’re storing personal information. But in general, github is a safe place even without encryption.
Conclusion
Github syncing is a flexible way to backup your data compared to other cloud services. You should try to backup relatively small data and be careful about storing personal information. Even though github is a code revision system rather than a backup service, it serves to be a better alternative compared to the others.