How to create and manage site backups using SSH

    October 17, 2019

    Caution

    Although we provide 30-day automatic backups for our clients, we recommend supplementing these with your own backups. Maintaining multiple backup sources is a best practice to prevent 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.

    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
    3. 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;
    2. Import the database backup from the domain directory into the truncated database:
      zcat database-backup-DATE.sql.gz | mysql -u USERNAME -p DATABASE
    3. 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.oldor another option:
      mv html html.old
    2. Extract the htmlbackup 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 your Client Portal.

    Was this article helpful?

    Send feedback

    Can’t find what you’re looking for?

    Our award-winning customer care team is here for you.

    Contact Support