Today we’ll be going over how to backup your website off-site with git every day. You can use this guide with any service that supports git, we’re focusing on GitHub because it’s reliable, robust and offers a free tier.
Start off by signing up with GitHub here.
Next, create a repository to hold the website code and database. Click Create a repository on the left side of the page
At the repository create page make sure you check the Private option so your repository isn’t accessible by the world
Next, you need to add your server’s SSH key to your GitHub account so that your server is able to authenticate with GitHub to pull and push code to it.
To do this, head to settings
Click on SSH and GPG keys and then click on New SSH key
For this next part, you need to SSH into your and type in the following commands to generate your SSH keys.
ssh-keygen -t rsa
[[email protected] ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:TfvbwmNDhaOaqvrrUO1ggfhWJHDcwzbA+UFOAOtGGSI [email protected] The key's randomart image is: +---[RSA 2048]----+ |E*=B+ | |o+**B | |oo.o++ . . | |o. ..o o .o . | | oo + . S o. o | |.. o o ... | | . . o o. | | . o *o | | .+=o.. ..+. | +----[SHA256]-----+
If you navigate into /root/.ssh you should see two files that were just created.
[[email protected] ~]# cd /root/.ssh [[email protected] .ssh]# ls id_rsa id_rsa.pub
These are your private and public keys for the server. You should never share your private key with anyone, only your public key, labeled id_rsa.pub.
Copy the contents of this file, which look something like below and then back to the GitHub page you were on:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAr4P0WHjR45+82NugrjPLfSOVJchAKke+YNDNkG0m+0zWCF+4EXTqvONmSW6yaWJbq5LGw0RL9zHmjvaTpVvCd0dmgiLhw6i4SG/qnZ3C7iVyr1DL2JpQ1MmWWRjuhE44ZNeMbuJY65zUaNKoI8DA71CTIDdwBPTjuX06NQSGiitF1JOwjJK3FcV+DB8pRSAosRZ0Lw2pEidwg0lJpH/5HmZ+iAkdHIANw5u+18EhvZjfJPyPB/lt6i6yOjxYvcQoxDBxgJJN0V3bVOw1eJFH9esN/jgM/ZR6t8wAujomr5JiNzFqt+I3+cE2L9xESMF8DifoPcrJ+bhtJIsc/hIR r[email protected]
Paste the key, enter a title and hit Add SSH key
Now comes the part where you add all your website code and database backup to the repository.
Note: To avoid the possibility of accidental overwrites, it is only recommended to use this method with an empty repository.
Before we do anything to the code of the website, we’ll create a backup first. The website on our server is located at /var/www/vhosts/websiteone.com. There are two directories listed in this folder, db and httpdocs. The folder db is where I dump the websites MySQL database and httpdocs is the working directory where the actual code of the website resides.
tar -cvzf websiteone-backup.tar /var/www/vhosts/websiteone.com
Navigate into the folder where the website is located, for us, this would be /var/www/vhosts/websiteone.com.
You need to get the URL of your repository, head back to the main page of your account click on the repository you created earlier. Inside you should not see a green button labeled Clone or download, click this button and copy the Clone with SSH address
At this point, it is a good idea to make sure you have git installed on your server, if not issue the following:
#For CentOS yum install git #For Ubuntu sudo apt-get install git
Now to clone the repository onto your server and push your website into it. The following line will clone the repository into a folder called temp
git clone [email protected]:theshelltest/websiteone.git temp
[[email protected] websiteone.com]# git clone [email protected]:theshelltest/websiteone.git temp Cloning into 'temp'... The authenticity of host 'github.com (126.96.36.199)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,188.8.131.52' (RSA) to the list of known hosts. remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done.
Here’s a look at the directory structure after the repo was cloned into temp
Move the important bit out of the temp folder and into your website directory
mv temp/.git .git
If you take a look at the folders now, you’ll see a hidden .git folder in there
Remove the temp folder
rm -rf temp
Add both the db and httpdocs to your repository.
git add . --all git commit -m "Full website backup into repo" git push
Counting objects: 2017, done. Delta compression using up to 4 threads. Compressing objects: 100% (1981/1981), done. Writing objects: 100% (2016/2016), 10.98 MiB | 2.15 MiB/s, done. Total 2016 (delta 200), reused 0 (delta 0) remote: Resolving deltas: 100% (200/200), done. To [email protected]:theshelltest/websiteone.git 17f541f..2255471 master -> master
You’ve just successfully pushed your website and database file to the repository.
Now to automate this, create a file called backup.sh in your home directory. Since we’re root for us it’s /root. Add the following to it:
mysqldump -u DB-USER -pDB-PASSWORD websiteone-db > /var/www/vhosts/websiteone.com/db/websiteone-db.sql cd /var/www/vhosts/websiteone.com && /usr/bin/git add --all . cd /var/www/vhosts/websiteone.com && /usr/bin/git commit -m "autocommit" cd /var/www/vhosts/websiteone.com && /usr/bin/git push
You now need to add this file to be executed by cron at a set time of the day. You can change this to run at whichever time you prefer, you can even increase the frequency of execution depending on your needs. Issue the following at the terminal to open the cron text editor.
You’ll be greeted by a blank page, press the letter i to enter edit mode. To backup your website once everyday at one minute past midnight, enter the following line:
01 0 * * * sh /root/backup.sh
Press the ESC key and type :wq followed by the Enter key. This will save the line you just added to cron, now you need to restart the cron service to pick up the new routine:
service crond restart
That’s it, have a look at your repo at GitHub to verify the first commit and you’re done.