How to create and manage site backups using SSH

How to create and manage site backups using SSH

Although we provide 30-day automatic backups to all Nexcess clients, we recommend supplementing these with your own backups. Maintaining multiple backup sources is an industry best practice, as this provides insurance against catastrophic data loss. If suitable storage space is available, we also recommend maintaining a history longer than 30 days.

SSH is a reliable way to create backups of your site files and database.

But first...

  • This method requires SSH access. Nexcess clients on physical (non-cloud) servers must first enable SSH access and adjust their firewall settings in SiteWorx; Cloud accounts have SSH enabled by default.

  • In commands, ALL_CAPS represents placeholder information. Replace it with the indicated information.

Contents

Creating a database backup

Attention: The creation of backups generates significant load and can degrade site performance, especially for large databases. Consider waiting until non-peak hours.

  1. Log in to your server via SSH, then navigate to your domain’s directory:

    cd YOUR_DOMAIN
  2. From your domain directory, create the backup of your database,

    mysqldump --opt --skip-lock-tables --routines --max_allowed_packet=2G -u USERNAME -p DATABASE | gzip > database-backup-$(date +%F).sql.gz

  1. Enter your password when prompted. Your backups will be saved in the domain directory.

Creating site files backups

To create a backup of your site files, change to the domain directory:

cd YOUR_DOMAIN

Then, create a backup of your domain’s html directory:

tar -czf html-backup-$(date +%F).tar.gz html

Restoring your database

Attention: Before restoring the database, we recommend creating a backup using the method provided above.

  1. Truncate the database to be restored:

    Attention
    : If you do not wish to truncate the database, you can import the backup into a new database instead. Be sure to update your Magento or WordPress configuration accordingly.

    mysql -u USERNAME -p

    Enter your password when prompted, then issue:

    drop database DATABASE; create database DATABASE; exit;

  1. Import the database backup from the domain directory into the truncated database:

    zcat database-backup-DATE.sql.gz | mysql -u USERNAME -p DATABASE

  2. Enter your password when prompted. This operation may take some time to complete; to prevent idle-timeout, tap the spacebar every 30 seconds.

Restoring site files

Attention: Before restoring your files, we recommend creating a backup using the method provided above.

  1. Delete the html directory by issuing the below command from the domain directory:

    rm -r html

    If you do not wish to delete the html directory, you may instead rename it to html.old or another option:  

    mv html html.old

  1. Extract the html backup from your domain directory:

    tar -xzf html-backup-DATE.tar.gz

This operation may take some time to complete; to prevent idle-timeout, tap the spacebar every 30 seconds.

Retrieving credentials for Magento or WordPress

You can retrieve the database, username, and password values for any domain by running the following SSH commands from the html directory of that domain:

To do so, first, change to the html directory of that domain:

cd YOUR_DOMAIN/html

Then, issue one of the below commands:

  • If using Magento 1, issue:

    grep -C2 username app/etc/local.xml

  • If using Magento 2, issue:

    grep -C2 username app/etc/env.php 

  • If using WordPress, issue:

    grep -C3 password wp-config.php

 

For 24-hour assistance any day of the year, contact our Support Team by email or through the Client Portal.

Article Rating (No Votes)
Rate this article
  • Icon PDFExport to PDF
  • Icon MS-WordExport to MS Word
 
Attachments Attachments
There are no attachments for this article.
Related Articles RSS Feed
How to perform one-time backups with SiteWorx
Added on Wed, Dec 19, 2018
How to schedule automatic backups with SiteWorx
Added on Thu, Dec 20, 2018
How to restore partial backups with SiteWorx
Added on Tue, Dec 18, 2018
Nexcess Backup Policy
Added on Wed, Jan 22, 2014
How to delete backup files with SiteWorx
Added on Thu, Aug 1, 2013