SSH connection takes too long to respond/connect
Has an SSH connection taken too long to return the password prompt? There are two…
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.
If you navigate into /root/.ssh you should see two files that were just created.
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:
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.
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:
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
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
If you take a look at the folders now, you’ll see a hidden .git folder in there
Remove the temp folder
Add both the db and httpdocs to your repository.
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:
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:
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:
That’s it, have a look at your repo at GitHub to verify the first commit and you’re done.