Affiliate Disclosure: By buying the products we recommend, you help keep the lights on at MakeUseOf. Read more.
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 needn’t be really. If you have SSH access to your website host (generally you would need at least a virtual private server for this), then it’s easy to backup, restore and migrate your entire website with only a few commands. Let me show you how.
What is SSH Command Line?
SSH gives you the ability to talk directly to your web-server. 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 life-saver and makes the process of migrating sites incredibly easy.
Most shared hosts unfortunately don’t allow SSH access to your account, at least not by default. If you’re hosting with GoDaddy, you can enable it though, so be sure to check first.
To log in via SSH, open up the Terminal in OS X (or get some free SSH software for Windows) and type in the following:
You’ll be prompted for your password. 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, similar to 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 httpdocs directory, which is the root of my web site (where all my wordpress files are stored). You can then ‘ls’ again, just to be sure.
At this point, we’re ready to begin the SSH backup process.
Backing up the Database:
Since the majority readers will be doing this with a WordPress install, you will most certainly have a database to backup in addition to any files stored on the site. First off, you’ll need 3 bits of information to backup your database, but all can be found within wp-config.php (if you’re running wordpress, that is):
- Database name
- Database user
- Database password
Then, issue this simple command, being sure to replace the username, table name, and backup filename where neccessary:
mysqldump --add-drop-table -u username -p tablename > backupfilename.sql
Hit enter, and enter your password. Once it’s 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 somewhere else.
Note: I’ve assumed that your database server is running on the same server on which you are hosting. On a GoDaddy host however, the MySQL database is actually stored remotely on a separate server to which you don’t have SSH access. In cases like these, you will need to access PHPMyAdmin via the hosting control panel, but that is out of the scope of this tutorial.
Backing Up Files:
Now that we have the database stored to single file on the server, we can go ahead and backup both that and your website files down to a single compressed backup file. To do this, we are going to issue one simple command. You need only replace yourbackupfilename with whatever you want it to be called.
tar -vcf yourbackupfilename.tar .
Let me break that down. Tar is a common linux compression format, similar to zip but more efficient. -vcf are simple some options that say “make a new archive, and tell me what you’re doing”. Next is the name of the file we want to create, and finally a single period mark tells it to include everything. We could have written * instead, but this would miss any hidden files such .htaccess which is essential for WordPress.
That’s it. Once that’s run, you will have a single .tar file consisting of every file on your site. You could log in via FTP at this point and download it, but let me show one final step that allows you to restore all these files.
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 now you’d like to restore it to that. First off, log in via FTP and upload the backup file onto your server. Perhaps you’ve been storing them in a special directory. Either way, move the latest complete backup file into the root of your site, and we’ll begin.
Start by unpacking all the files, the reverse of what we did to back them up:
tar -vxf yourbackupfilename.tar
This will overwrite existing files! The crucial difference here is in the -vxf switch, which tells it to extract the files instead of creating a new backup. Also, there is no period on the end of the command this time.
The last step is to suck your database back in to where it was before. Make sure you have a blank database setup with the same password and tablename as before, or you’ll need to change your site configuration settings too. To suck the data back in, issue this command:
mysql -u username -p tablename < databasebackupfilename.sql
Next week: Automating Your Backups
That’s enough to get you started with doing SSH backups for now, then next I’ll show how to automate the task with a simple shell script and a CRON command. If you have some Amazon s3 storage space, I’ll even show you how you can automatically upload your backup files to a storage bucket once they’re done.
One last tip – when I first began to use the command line, this one really impressed me – try pressing the tab key when your typing in a long filename, and if the name is unique enough it will attempt to autocomplete the rest of the filename!