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
/etc
: This directory contains important system configuration files./home
: This directory holds user data and personal files./root
: This directory contains the root user's personal files and configurations./var
: This directory holds variable data like logs, databases, and websites./usr/local
: This directory contains locally installed software and configurations./boot
: This directory contains essential boot files, including the kernel and initial ramdisk./srv
: This directory holds data for services provided by the system, like web and FTP servers.
You should backup the system key configuration files
/etc/fstab
: This file defines the filesystems and partitions to be mounted at boot time, their mount points, and options for mounting./etc/passwd
: This file contains user account information, such as username, user ID, group ID, home directory, and login shell./etc/group
: This file contains group information, including group names and group IDs./etc/shadow
: This file stores encrypted user passwords, along with password aging information./etc/hosts
: This file defines the mapping between IP addresses and hostnames for the local system./etc/resolv.conf
: This file configures the system's DNS resolver, specifying nameservers to use for domain name resolution./etc/sysctl.conf
: This file contains kernel parameters that can be modified at runtime to tune the system's performance and behavior./etc/hostname
: This file contains the hostname of the system, which is used to identify the system on a network./etc/profile
and/etc/bashrc
: These files contain global environment settings and functions for the Bash shell./etc/crontab
: This file defines system-wide scheduled tasks, or "cron jobs," which are executed by thecron
daemon./etc/sudoers
: This file defines user privileges for thesudo
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:
-
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
-
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.
-
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 IP192.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 andremote_host
with the remote server's hostname or IP address.