How to backup and migrate linux to another VPS. A comprehensive guide

Migrating a Linux server to another Virtual Private Server (VPS) can be a daunting task, especially if you have never done it before.

There are many reasons you might want to migrate your server, such as upgrading to a more powerful VPS, switch to another VPS with free tier plan, switching hosting providers.

Regardless of your reasons, this guide will walk you through the essential steps to ensure a successful migration with minimal downtime.

Import Files Checklist

First you should understand the usage of each important folders and files

  1. /etc: This directory contains important system configuration files.
  2. /home: This directory holds user data and personal files.
  3. /root: This directory contains the root user's personal files and configurations.
  4. /var: This directory holds variable data like logs, databases, and websites.
  5. /usr/local: This directory contains locally installed software and configurations.
  6. /boot: This directory contains essential boot files, including the kernel and initial ramdisk.
  7. /srv: This directory holds data for services provided by the system, like web and FTP servers.

You should backup the system key configuration files

  1. /etc/fstab: This file defines the filesystems and partitions to be mounted at boot time, their mount points, and options for mounting.
  2. /etc/passwd: This file contains user account information, such as username, user ID, group ID, home directory, and login shell.
  3. /etc/group: This file contains group information, including group names and group IDs.
  4. /etc/shadow: This file stores encrypted user passwords, along with password aging information.
  5. /etc/hosts: This file defines the mapping between IP addresses and hostnames for the local system.
  6. /etc/resolv.conf: This file configures the system's DNS resolver, specifying nameservers to use for domain name resolution.
  7. /etc/sysctl.conf: This file contains kernel parameters that can be modified at runtime to tune the system's performance and behavior.
  8. /etc/hostname: This file contains the hostname of the system, which is used to identify the system on a network.
  9. /etc/profile and /etc/bashrc: These files contain global environment settings and functions for the Bash shell.
  10. /etc/crontab: This file defines system-wide scheduled tasks, or "cron jobs," which are executed by the cron daemon.
  11. /etc/sudoers: This file defines user privileges for the sudo command, which allows users to execute commands with elevated privileges.

In addition to these directories, also backing up user specific configs:

  • ~/.ssh This folder stores the ssh private and public key.
  • ~/.gitconfig is a user-specific configuration file for Git.
  • ~/.tmux.conf is a user-specific configuration file for tmux, a terminal multiplexer that allows users to manage multiple terminal sessions within a single terminal window.
  • ~/.vim/ is the user-specific configuration files, plugins, and other customizations for the Vim text editor
  • ~/.basrc or ~/.zshrc is the user-specific configuration file for the Bash and Zsh shell

Backup Tools

You can use tools like rsync, tar, or cp to create backups, or leverage backup software like BorgBackup, Amanda, or Bacula.

It's highly recommend to use rsync to do the backup, especially you want to directly backup the data to the remote server.

Here's a basic example of how to use rsync to backup files:

  1. First, make sure rsync is installed on your system. You can install it using your package manager, for example:

    For Debian/Ubuntu systems:

    1sudo apt-get update
    2sudo apt-get install rsync
    

    For CentOS/RHEL systems:

    1sudo yum install rsync
    
  2. To backup files using rsync, you can use the following command structure:

    1rsync [OPTIONS] SOURCE DESTINATION
    

    For example, to backup the /etc directory to a backup folder in /home/user/backup, you can run:

    1rsync -avz /etc/ /home/user/backup/etc/
    

    In this example:

    • -a: Archive mode. It preserves file permissions, ownership, timestamps, and symbolic links.
    • -v: Verbose mode. It provides more detailed output of the backup process.
    • -z: Compress data during transfer, which can save bandwidth and speed up the process.
  3. To backup files to a remote server, use the following command structure:

    1rsync -avz SOURCE [email protected]_host:DESTINATION
    

    For example, to backup the /etc directory to a remote server with IP 192.168.1.2 and store it in the /backup folder, you can run:

    1rsync -avz /etc/ [email protected]:/backup/etc/
    

    Replace user with the appropriate remote user and remote_host with the remote server's hostname or IP address.