How to Restore Website Backups from the Command Line

Earlier this week we wrote about how to use command line tools to back up your website. Check out our previous article for details on how we create these backups.

In case the worst happens, you might need to restore your backup. No worries. We can easily do this from the command line too! Basically, we are going to do the same thing, but in reverse.

Restoring Backup Files to the Server

We are going to use the scp command again, except this time the syntax will vary a little since we need to upload our backup file to the server, not the other way around. Remember to locate your backup in a location not publicly available on your server!

scp /home/username/backups/domainname/[backup_file_here]  username@serversIP:/home/username/

Once this process finishes. You can login to your server using SSH and start decompressing the backup file:

tar -xf /home/username/[backup_file_here] 

This will leave you with the four directories we created in the last post: db, core, logs and conf

Restoring the Database

If your database was infected, the best thing you can do is wipe it out and restore it completely.

If your site uses MySQL, run the following command:

mysql -u root -p

This will start MySQL, then run the following commands:

# create a new database named new_database
create database new_database;
# use the database we just created
use new_database;
# import database dump into new_database
source /home/username/backup/db/[backup_file_here];
# exit mysql

And this should put your MySQL database back in the game!

If your site uses PostgreSQL, run the following commands:

# open PostgreSQL
sudo -u postgres psql
# create a new database named new_database
createdb -T template0 new_database
# import database dump into new_database
psql --set ON_ERROR_STOP=on new_database < /home/username/backup/db/[backup_file_here]
# exit PostgreSQL

And this should be it to have your PostgreSQL database back on the server!

If your site uses SQLite, you just need to copy the file to where your website configuration expects it to be:

cp /home/username/backup/db/[backup_file_here] /path/SQLite.db

Now we should have our database up and running!

Restoring Core Files

Restoring core files is pretty straightforward. All you need is one command:

tar -xf /home/username/backup/core/core.tar -C /home/username --strip-components=2

If you have a VPS/Dedicated Server and your root directory is something like /var/www/ you will probably need to execute the above command as a sudoer (a user with root privileges).

The –strip-components flag allows us to extract the files without their parent directories. In this case, we are stripping two directories “home” and “username“. If your website’s root directory were to be /var/www/html, you would need to strip the directories var, www and html (–strip-components=3).

Important: All files with the same name will be overridden.

Voilà! Just like that your website files have been restored! You usually do not need to restore configuration files, but in case you need to, you now possess the knowledge to do so. Happy restoring!


This is not a complete solution, although it will get you started in the science of making backups.

In future posts we will cover a few things we are lacking in this post

  • Incremental backups (backup full once and then save history of modified/new files)
  • Integrity checks for the backups (don’t trust anything that is not signed!)
  • Automation (who backs up your websites when you’re vacationing?)
  • Replication (backups of your backups!)

These four reasons should be enough to keep you thinking on a professional hassle-free backup solution. But think no more, we got you covered.

You May Also Like