DEV Community

Arthur
Arthur

Posted on

How to Back Up and Restore a Linux Server

Hello, I'm Arthur.
A server can run smoothly for months, but unexpected problems can still happen. A failed update, accidental file deletion, or hardware issue can make important data unavailable.

This is why backups are an essential part of server management.

In this guide, I'll explain how to create a simple Linux server backup, store it safely, and restore it when needed.

1. Decide what needs to be backed up

Before creating a backup, identify which files and services are important.

For a typical website, this may include:

  • Website files and application code
  • Database contents
  • Configuration files
  • Important scripts and scheduled tasks
  • SSL certificates and other required settings

You do not always need to back up the entire operating system.

For many websites, backing up the application files, database, and configuration is enough to rebuild the server if necessary.

2. Check available disk space

Before creating a backup, check how much space is available.

df -h
Enter fullscreen mode Exit fullscreen mode

You can also check the size of a directory:

du -sh /var/www
Enter fullscreen mode Exit fullscreen mode

This helps estimate how much storage the backup may require.

If the server has limited disk space, avoid creating large backup archives without checking the available capacity first.

3. Create a backup of website files

For a website stored in /var/www/html, you can create a compressed archive using tar.

sudo tar -czf website-backup.tar.gz /var/www/html
Enter fullscreen mode Exit fullscreen mode

This creates a compressed archive of the website directory.

To check whether the archive was created:

ls -lh website-backup.tar.gz
Enter fullscreen mode Exit fullscreen mode

You can also list the files inside the archive without extracting it:

tar -tzf website-backup.tar.gz
Enter fullscreen mode Exit fullscreen mode

For a live website, consider whether files may change during the backup. Applications that receive frequent uploads or updates may need a more consistent backup process.

4. Back up the database

If your website uses MySQL or MariaDB, its database needs a separate backup.

For example:

mysqldump -u dbuser -p database_name > database-backup.sql
Enter fullscreen mode Exit fullscreen mode

Replace dbuser and database_name with the appropriate database user and name.

The command asks for the database user's password.

For larger or actively used databases, use a backup method that provides a consistent snapshot. Avoid assuming that copying database files while the database is running will produce a valid backup.

Keep database credentials secure and do not include passwords directly in scripts or commands that may be saved in shell history.

5. Store backups outside the server

A backup stored on the same server is not enough by itself.

If the server's disk fails or the server becomes inaccessible, both the original data and the backup may be lost.

Copy important backups to a separate location, such as:

  • A remote backup server
  • Object storage
  • A secure off-site storage system

For example, you can use scp to transfer a backup to another Linux server:

scp website-backup.tar.gz backupuser@backup-server:/backups/
Enter fullscreen mode Exit fullscreen mode

Use a secure connection and make sure the destination has enough storage.

If you are planning a new hosting environment, compare backup options alongside CPU, RAM, and storage. For example, Seimaxim VPS hosting is an option to consider for websites and applications that need a virtual server environment.

The backup destination should remain separate from the original server so that a single failure does not affect both copies.

6. Automate regular backups

Manual backups are useful, but they are easy to forget.

Linux includes cron, which can schedule recurring tasks.

Open the current user's crontab:

crontab -e
Enter fullscreen mode Exit fullscreen mode

A simple example that runs a backup script every day at 2 AM is:

0 2 * * * /usr/local/bin/backup.sh
Enter fullscreen mode Exit fullscreen mode

The script should create the backup, record its result, and send or copy the backup to the intended storage location.

Make sure the script has the correct permissions and that the scheduled task can access the required files.

Test the script manually before relying on the schedule.

7. Test the restore process

Creating a backup is only half the job.

You also need to know whether the backup can actually be restored.

For example, to extract a website archive into a temporary directory:

mkdir restore-test
tar -xzf website-backup.tar.gz -C restore-test
Enter fullscreen mode Exit fullscreen mode

Check that the expected files are present.

For a database backup, restore it into a test database rather than overwriting the live database.

A proper restore test helps identify missing files, incorrect permissions, incomplete database dumps, or other problems before an emergency occurs.

8. Plan for server recovery

If a server fails, having a backup is useful, but a recovery plan makes the process easier.

Document the steps needed to:

  1. Provision a replacement server.
  2. Install the required software.
  3. Restore application files and databases.
  4. Reconfigure services and networking.
  5. Test the website before directing traffic to it.

For workloads that need dedicated physical resources, Seimaxim dedicated servers are another hosting option to evaluate when planning a replacement or upgraded server environment.

The right recovery plan depends on how much downtime your application can tolerate and how frequently its data changes.

Final thoughts

A reliable backup strategy includes more than copying files.

Identify important data, create consistent backups, store them separately, and test the restore process regularly.

For a website or application, even a simple automated backup routine can make recovery much easier after an unexpected problem.

The best time to prepare for server failure is before it happens.

Top comments (0)