Saturday, December 21, 2024

A Comprehensive Guide: Backing Up and Preparing to Restore a Website on a Linux VPS

When tasked with backing up a website hosted on a Linux VPS, particularly when the hosting configuration is unknown, it can seem like a daunting challenge. This guide walks you through identifying the web server, understanding the hosting configuration, creating a backup, and preparing for restoration.

Step 1: Identifying the Web Server


Before initiating a backup, the first task is to understand what web services are running on the server.

Login to the Server

Use SSH to access the VPS:


ssh root@<server-ip>


Check Running Processes

Identify the web server (e.g., Apache or Nginx) using:


ps aux | grep -E 'nginx|apache|httpd'


Example output:


root 788 0.0 0.0 121936 2220 ? Ss 09:06 0:00 nginx: master process /usr/sbin/nginx


From this, we know Nginx is running on the server.


Check Installed Packages

Confirm installed web servers:


yum list installed | grep -E 'nginx|apache'


This verifies if both Nginx and Apache are installed, though only one may be active.


Step 2: Locating the Website Files


The next step is to find where the website files are stored.


Check Web Server Configuration

Look for the root directory in the configuration files:

For Nginx:

grep -r 'root' /etc/nginx/

For Apache:

grep -r 'DocumentRoot' /etc/httpd/


Example for Nginx:


root /var/www/knights;


This indicates the files are in /var/www/knights.


Explore the Directory

Navigate to the directory and list its contents:


cd /var/www/vishrava

ls -l


Identify the Website Type

Look for specific files that indicate the framework or CMS:

WordPress: wp-config.php, wp-content/

Laravel: .env, artisan

Static Website: Files like index.html, manifest.json, static/

In this example, files like index.html, manifest.json, and static/ suggest it’s a static website or single-page application (SPA).


Step 3: Creating a Backup


Once the hosting configuration is understood, create a complete backup of the website and any additional required files.

Backup Website Files

Compress the website directory:


tar -czvf website_backup.tar.gz /var/www/knights


Include Files from Root Directory

If there are important files in the root folder:


tar -czvf complete_backup.tar.gz /var/www/knights /root/file1 /root/file2


Transfer Backup to Local Machine

Use scp to copy the backup to your local machine:


scp root@<server-ip>:/complete_backup.tar.gz /path/to/local/directory


Step 4: Monitoring Folder Capacity


To ensure disk usage remains within limits during the backup process:

Check Folder Size

Use the du command:


du -sh /var/www/vishrava


Example output:


438M /var/www/knights


Monitor Changes in Real-Time

Use the watch command to monitor folder size live:


watch -n 5 du -sh /var/www/vishrava


Find Large Files

If space is unexpectedly high, locate large files:


find /var/www/vishrava -type f -size +100M -exec ls -lh {} \;


Step 5: Preparing for Restoration


With the backup complete, prepare to restore the website if needed:

Verify Backup

Extract the backup locally to ensure it contains the required files:


tar -tzvf /path/to/local/directory/complete_backup.tar.gz


Restore Backup

To restore, place the files back in their original directory:


tar -xzvf complete_backup.tar.gz -C /


Restart Web Server

Ensure the web server is restarted to recognize the restored files:


For Nginx:

systemctl restart nginx


For Apache:

systemctl restart httpd


Backing up a website on a Linux VPS involves identifying the web server, locating the website files, and creating a reliable backup. Monitoring disk usage ensures a smooth process, while preparation for restoration ensures minimal downtime. These practices not only safeguard your data but also empower you with confidence in server management.






0 comments:

Post a Comment