Restoring a Single file or Directory from a System Backup

On a VPS or Dedicated hosting account, likely your only option to restore files is using the WHM.  Sadly this will restore all files for the user and not just a single file, database, or directory.  So if you just need one directory restored or just one file, the best way to do this is via the command line.  So you can use the system console, terminal, or putty.

You are going to need to know your username in order to do this and be ssh’d as the root user.

The first example that I’ll be giving is extracting the public_html and mysql directories from a daily backup and copying them over to your user directory, i.e. /home/username/

The command that will be used is the tar command. The flags used will be z, x, v, and f; z because the backup is a tar.gz (it’s gziped), x because we are extracting, v so that the files that are extracted are listed, and the f is so the command will read from the file listed.  From there we will list the backup files location and then list the directories or files we wish to extract from the backup.  After that we will use the && operator (this will chain the commands together, however if a previous command fails the command will stop) and move the directories or files to our destination and then use && again to change the ownership from root to the username (since we are performing these commands as root we’ll want to be sure all ownership is set to the user).  Here is a rough example of the command:


tar -zxvf /backup/cpbackup/daily/username.tar.gz username/homedir/public_html username/mysql && mv username /home/username/restore-day && chown -R username:username /home/username/restore-day/*


so naturally you’ll replace the username with your username and restore day can be which ever you choose to name it, just make sure you are consistent through out it.  For example if we were restoring Carvertown’s public_html and mysql directory I’d use this:


tar -zxvf /backup/cpbackup/daily/carvertown.tar.gz carvertown/homedir/public_html carvertown/mysql  && mv carvertown /home/carvertown/restore-day && chown -R carvertown:carvertown /home/carvertown/restore-day/*


If you want the weekly or daily backup you’d just change the daily part.  Here are some screen shots of the work done on extracting the public_html and mysql directories.

better restore directory

So this copied over those two directories, public_html and mysql.

ls -lah restore

The databases from your backup with be in mysql and the public_html will be in the homedir directory.

Now if you want to only restore a single file, for example a png you erased on accident, an index.php, or .htaccess file that was deleted on accident you’d do something like this.


tar -zxvf /backup/cpbackup/daily/carvertown.tar.gz carvertown/homedir/public_html/image.png && mv carvertown /home/carvertown/restore-day && chown -R carvertown:carvertown /home/carvertown/restore-day/*


Instead of only listing the directory path you now put the file location. For example:

better restore file

So when we list that restore directory we’d see this:

ls -lah single

And that should get you on the right track for restoring a single file or directory.

Posted in Archived Posts and tagged .