Feeling Lost? Start Here »»
I’ll share a quick tip that helped me to solve my “backup and restore” problem with zero cost and 100% reliability.
In this quickie, we will be:
So let’s first start with creating a blank private GitHub repo; and then clone it on our server:
$ cd ~/BACKUPS $ git clone email@example.com:user/server-backup.git
Before going any further here are a few remarks:
- This is an experimental setup. In an unlikely case that your private repository is compromised, so will your backup data be. So use it at your own risk;
- Make sure that you use the SSH clone url when doing your clone, so that we will be able to push to the repo without requiring a password; this will also help us automate things;
- Also make sure that this is a private repository. You would not want to share your server configuration with the rest of the world ;).
Having said that, I’ve been using it for the last couple of months, and it works like a charm.
Now let’s create a simple shell script to back everything up:
# ~/BACKUPS/server-backup/backup.sh cd ~/BACKUPS/server-backup; iptables-save > iptables.txt; crontab -l > crontab.txt; rsync -rtv --exclude ".git" ~/webs/ ~/BACKUPS/server-backup/webs rsync -rtv /etc/apache2/sites-available ~/BACKUPS/server-backup/sites-available cp /etc/apache2/apache2.conf ~/BACKUPS/server-backup/apache2.conf cp /etc/nginx/nginx.conf ~/BACKUPS/server-backup/nginx.conf mysqldump -uroot -pdasecretpassword db > db.sql mongodump git add . git commit -m 'backup.' git push origin master echo "Done!"
And then adds everything to git, and commits it to the origin.
Caveat: If you have anything that’s more than 100MB, GitHub will not allow you to push it. If you have very large files, you might either want to exclude them from the backup, or split them, or compress them.
We are almost done.
Finally, let’s automate this process by creating a crontab entry.
Editing the crontab is really easy:
And add the following line to the upcoming document, and save it.
0 0 * * * /userprofile/BACKUPS/backups/backup.sh > /userprofile/cronout.txt 2>&1
Hint: Remember to leave an empty line at the end of the file.
This will create a crontab entry and schedule our backup script to run at every midnight.
And in case anything goes wrong, the output of the last run will be saved to /userprofile/cronout.txt.
We have created a reliable backup solution in less than five minutes!
It’s really easy to keep versioned backups of your important files at GitHub.
More important than that, GitHub allows you to have great web interface where you can do full-text search, diff your data and your files.
And best of it; you won’t need to pay for a full-blown backup server.
And please share if you have other backup solutions that you use;
I’d love to hear about them.
Do you have something to say? Have I missed anything?
Send your comments and suggestions to firstname.lastname@example.org.