Backing up your website or blog can be an expensive and arduous task, requiring a variety of plugins, or additional plans from your hosting provider—but it doesn’t have to be, really.
If you have SSH access to your website, then it’s easy to perform various high-level tasks remotely. Here’s how to back up your website using SSH in a command line session.
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’re using Linux hosting you should have SSH access. If your website is hosted with GoDaddy, SSH should be enabled by default. If not, you can enable SSH in the GoDaddy cPanel interface. Other web hosts will offer a similar feature.
Meanwhile, VPS and dedicated server web hosts will allow SSH. Don’t know the difference? Check our guide to web hosting services
to learn more.
How to Use SSH on Your Computer
All three desktop operating systems feature a command line interface with support for SSH.
Simply open the interface and enter the ssh command to use the related tools.
If you haven’t used a command line environment before, some of this might seem difficult. While there is no time to teach you everything about SSH right now, here are a couple of shortcuts:
- Use the up and down arrows to cycle through previously entered commands
- 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:
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.
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 screen.
Don’t worry, that’s for security.
Once logged in, you’ll be presented with a command prompt, like the following:
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:
To ‘list’ the current files and folders.
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:
- Database name
- Database user
- 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.
No Access to Database Using SSH
We’ve assumed that your database server is running on the same server on which you are hosting.
However, on GoDaddy, the MySQL database is stored on a remote server to which you don’t have SSH access. In cases like these, you will need to access PHPMyAdmin via the host’s cPanel, beyond the scope of this tutorial.
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 in to 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
SSH Website Backups: Quicker Than Web Consoles and Plugins
While various tools and plugins have been published that help you to make site backups, nothing is quicker than SSH.
If you have SSH access to GoDaddy or whoever you host your site with, you can now backup a website. Interested in knowing more? It’s time to learn how to remotely manage a Linux server with SSH