Programming

How to Back Up Your Website Through SSH Command Line

Christian Cawley Updated 16-04-2020

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.

Advertisement

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 The Best Web Hosting Services: Shared, VPS, and Dedicated Looking for the best web hosting service for your needs? Here are our best recommendations for your blog or website. Read More to learn more.

How to Use SSH on Your Computer

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

Advertisement

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:

  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:

Advertisement
ssh username@yourdomain.com

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 username@YOUR.IP.ADDRESS.HERE

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:

Advertisement
-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.

Advertisement

Access your website via SSH to backup and restore data

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.

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

Backup your website with SSH

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 How to Remotely Manage a Linux Server with SSH SSH is very functional, which is why it's a go-to for remote server management. Learn how to remotely manage a Linux server via SSH, from connection, to installing software, and file transfers. Read More .

Related topics: Data Backup, Linux Tips, SSH, Web Server, Webmaster Tools, Wordpress.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. SANJEEV CHAUHAN
    June 3, 2019 at 8:35 pm

    How to get table name what to backup

    • James Bruce
      June 4, 2019 at 7:04 am

      You'll find all your DB details stored in wp-config.php in the root of the website. Username, password, and table name.

  2. R
    April 2, 2017 at 11:08 pm

    Great article. Thanks. Please, add that extracting overwrites existing files

  3. Estela
    February 7, 2017 at 11:37 pm

    brilliant intel we have at this point what is everyone's first impressions with our post touching on angry birds
    star wars cheats tool

  4. Mike
    January 14, 2017 at 11:44 pm

    You should also make sure to remove your backup file from your public facing directory /var/www/html for example. You could use the following commands:

    mkdir ~/backups # This will create a backups folder in root home folder if logged in as root
    cp /var/www/html/backupfilename.tar ~/backups # This will copy the file into the backup folder
    rm /var/www/html/backupfilename.tar # This will erase it

    If somebody guesses the filename they will have all sorts of information that you do not want them to have.

  5. Wolfgang
    August 26, 2016 at 9:03 pm

    You note that, "Tar is a common linux compression format....". However, tar does not compress, it only concatenates your files into one file. By adding the -z (gzip) or -j (bzip2) switches will you then compress the file. So to compress the command should be -vzcf.

  6. Anonymous
    August 7, 2015 at 7:11 pm

    hello i have newbie question :)
    is this method will slow the time of loading my website ?
    if not, then even if i do live streaming ??

  7. Matthias
    February 2, 2015 at 10:17 am

    Thanks for this nice article. One little question: when I backup files, how can I skip folders from the tar-package?

    thanks!

    • James Bruce
      February 2, 2015 at 10:27 am

      tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz

      (exclude must be at the start of the command)

    • Matthias
      February 2, 2015 at 10:33 am

      Great! Saved my day. Thank you!

  8. Hemant
    December 12, 2014 at 4:36 am

    Thank James Bruce, for this simple and beautiful article

  9. Sil
    May 5, 2011 at 7:55 pm

    Thanks James, seriously appreciate it!

  10. Sil
    May 5, 2011 at 3:50 am

    Heya James, did you end up adding the Automated tutorial? Thanks for this one!

    • James Bruce
      May 5, 2011 at 7:47 am

      Not yet, there were a number of stories I wanted to get out first. Thanks for reminding me, I'll get on it today, so expect to be published next week sometime.

    • James Bruce
      May 5, 2011 at 9:07 am

      Whoops, my memory si seriously failing me. actually Sil, I have written it, and it scheduled to be published tomorrow. I did in fact write a while ago, but I've been pushing other stories in front of it in order to provide up to date info on a few pertinent issues. So anyway, watch out for it tmrw!

  11. James
    April 27, 2011 at 4:08 pm

    Guys you are the best, thank you very much for these tutorials!

  12. Joe
    April 19, 2011 at 4:11 am

    Thank you for this tutorial, James! I have struggled to backup my oversized WP db using phpmyadmin ... kept timing out ... thankfully PUTTY and your tutorial to the rescue! I can sleep now. Thank you so much.

    • James Bruce
      April 19, 2011 at 8:52 am

      You're welcome Joe, you're well on the way to super-high geekdom now!

      Last thing to do is ditch windows!

  13. pceasies
    April 10, 2011 at 11:29 pm

    If you want to easily backup your website I'd recommend rsync. If your host has SSH setup they probably have rsync setup as well. If you're on a Windows machine you'll need cygwin you an rsync package to get it working.

    Cygwin
    Once you have it installed, open a cygwin command window
    Your drives are all under /cygdrive/ so /cygdrive/c/ is the same thing as C:
    This will backup everything on your server (in your home directory) to Q:\WebsiteBackups\
    rsync -avze ssh USERNAME@SSH-SERVER.com:~/ "/cygdrive/q/WebsiteBackups/"

    If you delete a file on your server and run that again, the file will still be backed up on your computer. You could make a shell script that automatically runs and makes a new folder based on the date and backs up the website to that (maybe once every 2 weeks if it's large, or once a week if it's smaller)

    Here's a sample that you could schedule to run every 2 weeks (cronjob or scheduled task):

    #!/bin/bash

    LOCATION='/cygdrive/c/backupDirectory'
    USERNAME='YourLoginName'
    SITE='YourSSHServer'

    year=`date +%Y`
    month=`date +%m`
    day=`date +%d`
    FOLDER="$LOCATION/$year/$month/$day/"
    mkdir -p $FOLDER
    rsync -avze ssh $USERNAME@$SITE:~/ "$FOLDER"

    Open up nano/joe/vi in cygwin and paste/type that in. Save it as "backup-site" or something similar
    Run chmod +x backup-site (or whatever you named it)

    Open up Task Scheduler
    Create a new task and select date/time
    For action use "C:\cygwin\bin\bash.exe --login -i -c ~/backup-site"

    Now your website should be automatically backed up. You may want to create a script that automatically deletes older versions so it won't take up too much space. Another alternative would be to use the same folder and empty it every time before it backs it up again, then use Acronis, or Norton Ghost to include that folder it incremental backups (this would save space).

  14. dogbert
    April 10, 2011 at 11:15 pm

    The > in the last line should be a <, like so:
    mysql -u username -p tablename < databasebackupfilename.sql

    • James Bruce
      April 11, 2011 at 1:59 pm

      Thanks dogbert, I'll correct the mistake in the post now.

      • Zveno
        December 17, 2016 at 9:15 pm

        mysql -u username -p tablename
        should be
        mysql -u username -p tablename < databasebackupfilename.sql

        Wasted a hour trying to restore a backup without filename in command xD

        • James Bruce
          December 18, 2016 at 11:01 am

          Oddly, the source in my editor window was correct, but wordpress was stripping out the last bit for some reason. I've twiddled around with it again, and it's appearing now. Thanks for notifying.

  15. pceasies
    April 10, 2011 at 9:29 pm

    If you want to easily backup your website I'd recommend rsync. If your host has SSH setup they probably have rsync setup as well. If you're on a Windows machine you'll need cygwin you an rsync package to get it working.

    Cygwin
    Once you have it installed, open a cygwin command window
    Your drives are all under /cygdrive/ so /cygdrive/c/ is the same thing as C:
    This will backup everything on your server (in your home directory) to Q:WebsiteBackups
    rsync -avze ssh USERNAME@SSH-SERVER.com:~/ "/cygdrive/q/WebsiteBackups/"

    If you delete a file on your server and run that again, the file will still be backed up on your computer. You could make a shell script that automatically runs and makes a new folder based on the date and backs up the website to that (maybe once every 2 weeks if it's large, or once a week if it's smaller)

    Here's a sample that you could schedule to run every 2 weeks (cronjob or scheduled task):

    #!/bin/bash

    LOCATION='/cygdrive/c/backupDirectory'
    USERNAME='YourLoginName'
    SITE='YourSSHServer'

    year=`date +%Y`
    month=`date +%m`
    day=`date +%d`
    FOLDER="$LOCATION/$year/$month/$day/"
    mkdir -p $FOLDER
    rsync -avze ssh $USERNAME@$SITE:~/ "$FOLDER"

    Open up nano/joe/vi in cygwin and paste/type that in. Save it as "backup-site" or something similar
    Run chmod +x backup-site (or whatever you named it)

    Open up Task Scheduler
    Create a new task and select date/time
    For action use "C:cygwinbinbash.exe --login -i -c ~/backup-site"

    Now your website should be automatically backed up. You may want to create a script that automatically deletes older versions so it won't take up too much space. Another alternative would be to use the same folder and empty it every time before it backs it up again, then use Acronis, or Norton Ghost to include that folder it incremental backups (this would save space).

    • James Bruce
      April 11, 2011 at 2:03 pm

      thanks pceasies, that's a fantastic tutorial and something I was hoping to cover at a later date. I'll be sure to integrate your instructions for Windows users, as the process is a whole lot easier on OsX or to another hosting server.

  16. Kim Hjortholm
    April 10, 2011 at 6:21 pm

    For a joomla site Akeebabackup https://www.akeebabackup.com/ is an excellent solution,very simple setup and easy to use.