Enroll Course

100% Online Study
Web & Video Lectures
Earn Diploma Certificate
Access to Job Openings
Access to CV Builder



Online Certification Courses

How to Back Up Your Website Through SSH Command Line

How to Back Up Your Website Through SSH Command Line. 

What Is SSH Command-Line?

SSH gives you the ability to talk directly to your webserver. It doesn’t give a pretty interface, or a nice GUI, just a straight-up powerful command line. This can be daunting to some people, but the sheer power, speed, and level of automation it provides can be an absolute lifesaver and makes the process of migrating sites incredibly easy.

Many shared hosts unfortunately don’t allow SSH access to your account by default. However, this is changing, and if you are using Linux hosting you should have SSH access enabled by default.

How to Use SSH on Your Computer

All three desktop operating systems has a command-line interface with support for SSH.

  • Windows: use Windows PowerShell
  • macOS: use a Terminal
  • Linux: also use a Terminal

Simply open the interface and enter the ssh command to make use of the related tools.

If you have not used a command-line environment before, some of this might look difficult. While there is no time to teach you everything about SSH right now, here are a couple of shortcuts to follow:

  1. Use the up and down arrows to cycle through previously entered commands
  2. Press the tab key when your typing in a long filename—if the name is unique enough it should autocomplete

When you’re comfortable with SSH it’s time to start backing up your website.

Log In to Your Website Over SSH

Start by launching your preferred SSH tool and enter the following:

ssh [email protected]

You also use just the IP address. This is useful if you’re accessing a web server that hasn’t had a URL assigned, or if you’re migrating websites and the URL has moved.

ssh [email protected]

Enter the password when prompted. If you’ve never used SSH before, you might be surprised when typing in your password doesn’t anything on the screen.

Don’t worry, that’s for security.

Once logged in, you’ll be presented with a command prompt, like the following:

-bash-3.2:~$

This means everything is fine, so go ahead and continue with these commands.

Start by taking a look around and trying to navigate to your web directory. Type:

ls

To ‘list’ the current files and folders.

cd directoryname

to change to a directory. In this case, I’m going to navigate to the httpd directory, which is the root of my web site. You can then ls again, just to be sure.

At this point, we’re ready to begin the SSH backup process.

Backing up Your Website Database With SSH

As you will probably be backing up a WordPress install, you will want to back up the database and files.

You’ll need three bits of information to back up your database. Fortunately, if you’re running WordPress, these can all be found in the wp-config.php file:

  1. Database name
  2. Database user
  3. Database password

(If you’re using a different database-driven web application, refer to the set-up documentation for these details.)

Then, issue this simple command, being sure to replace the username, table name, and backup filename where necessary:

mysqldump --add-drop-table -u [username] -p [tablename] > [backupfilename].sql

Hit enter, then enter your password when prompted. Once run, you can then issue another ls command to check that the file has been output. Congratulations, this is all the information in your database as a single SQL file, ready to backup or import elsewhere.

Backing Up a Website’s Data With SSH

With the database stored as a single file on the server, you can go ahead and backup your site over SSH. First, navigate (using cd) to the directory you want to create the backup in. Next, use

tar -vcf yourbackupfilename.tar /directory/path

Let’s break this down:

  • tar—common Linux compression format, similar to zip but more efficient.
  • -vcf—simple options that say “make a new archive, and tell me what you’re doing”.
  • tar—your chosen name for the archive
  • /directory/path—specify the path to the website directory

An optional single period mark can substitute the file path, instructing the archive to include everything. You could also use * as a catch-all, but this omits hidden files such .htaccess which is essential for WordPress.

Once that’s run, you will have a single TAR file consisting of every file on your site.

At this point, you can connect via FTP and download the site archive.

Restoring Your Website Backup With SSH

Let’s say the worst has happened, and something has gone horribly wrong with your site. You’ve got a TAR file of everything that you backed up last week, so you’d like to restore it.

First off, log in via FTP and upload the backup file into the root directory of your server.

Start by unpacking all the files, the reverse of what we did to back them up:

tar -vxf yourbackupfilename.tar

WARNING: This will overwrite existing files!

The crucial difference here:

-vxf—instructs tar to extract the files instead of creating a new backup.

The last step is to suck your database back into where it was before. Start by having a blank database setup with the same password and table name as before. If you don’t have this, you’ll need to change your site configuration settings too.

To restore the database, use:

mysql -u [username] -p [tablename] < [databasebackupfilename].sql
Corporate Training for Business Growth and Schools