Skip to content

Instantly share code, notes, and snippets.

@muhammadardie
Created November 4, 2024 05:13
Show Gist options
  • Save muhammadardie/0cd786089e61a886418cced138a8f3fa to your computer and use it in GitHub Desktop.
Save muhammadardie/0cd786089e61a886418cced138a8f3fa to your computer and use it in GitHub Desktop.
Backup and restore full backup VPS (migrate VPS)

Backup and restore full backup VPS (migrate VPS)

First step was backup your Linux VPS directly on the server and then download it into your machine. Here’s how to create a compressed backup and download it afterward.

Creating and Downloading a Compressed Backup

  1. Create a Compressed Archive on the VPS:

    • You can use tar to create a compressed archive of the entire root filesystem while excluding directories that don’t need to be backed up (such as /dev, /proc, /sys, etc.).

    • Run the following command on your VPS:

      sudo tar -czvf /root/vps-backup.tar.gz --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /
    • This command will create a file named vps-backup.tar.gz in the /root directory.

    • Explanation:

      • tar -czvf:
        • -c: Creates a new archive.
        • -z: Compresses the archive with gzip.
        • -v: Verbose mode to show progress.
        • -f: Specifies the filename.
      • --exclude: Excludes directories that shouldn’t be part of the backup (as explained previously).
  2. Download the Backup File to Your Local Machine:

    • After creating the compressed file, you can transfer it to your Windows machine using an scp tool like WinSCP or a terminal with SSH (e.g., PowerShell or a WSL terminal).

    • Option 1: Using WinSCP:

      • Open WinSCP and connect to your VPS using SFTP.
      • Navigate to the /root directory and locate vps-backup.tar.gz.
      • Download the file by dragging it to your local directory on Windows.
    • Option 2: Using scp in PowerShell or WSL:

      • Open PowerShell or WSL on your Windows machine.
      • Use the following command to copy the file:
        scp user@vps_ip:/root/vps-backup.tar.gz C:\path\to\local\directory
      • Replace user and vps_ip with your VPS username and IP address, and specify the local path where you want to save the file.
  3. Verify the Backup File on your local machine:

    • After downloading, you can use tools like 7-Zip or WinRAR on Windows to inspect the contents of vps-backup.tar.gz and ensure the backup was successful.
  4. Clean Up (Optional):

    • If you need to free up space on the VPS, you can delete the backup file from /root after confirming it’s safely downloaded:
      sudo rm /root/vps-backup.tar.gz

This method will give you a full, compressed backup of your Linux system that you can easily store or move. To restore it to another Linux server, you can transfer the archive back to the new server and extract it.

To restore the vps-backup.tar.gz file to a new VPS, you can follow these steps. This process will extract all files and configurations, effectively replicating your original system on the new VPS.

Restoring the Backup on a New VPS

  1. Transfer the Backup File to the New VPS:

    • If the backup file is on your local machine, upload it to the new VPS using scp or a tool like WinSCP.

    • Example using scp:

      scp C:\path\to\vps-backup.tar.gz user@new_vps_ip:/root
    • Replace user@new_vps_ip with your new VPS username and IP address, and specify the local path to the backup file.

  2. Prepare the New VPS:

    • Connect to the new VPS via SSH:
      ssh user@new_vps_ip
    • Make sure you are logged in as root or have sudo privileges since restoring the backup will need access to system files and directories.
  3. Extract the Backup Archive:

    • Go to the directory where you transferred the backup file, e.g., /root.
    • Run the following command to extract the backup archive:
      sudo tar -xzvf /root/vps-backup.tar.gz -C /
    • Explanation:
      • -x: Extracts the archive.
      • -z: Decompresses the file (since it’s gzip compressed).
      • -v: Verbose mode to show progress.
      • -f: Specifies the file.
      • -C /: Extracts everything to the root directory.
  4. Adjust System Configurations:

    • After restoring, some configurations may need adjustment based on the new VPS environment. For example:
      • Network Configurations: Ensure any network or IP-specific settings in /etc/network/interfaces or /etc/netplan are correct.
      • Hostname: Update the hostname if needed by editing /etc/hostname and /etc/hosts.
      • SSH Keys: If your SSH keys differ, update them in /root/.ssh or /home/username/.ssh.
  5. Reinstall GRUB (Optional):

    • If your VPS uses a different bootloader or disk layout, you may need to reinstall GRUB or another bootloader.
    • To reinstall GRUB, you can use the following command:
      sudo grub-install
      sudo update-grub
  6. Reboot the New VPS:

    • Restart the VPS to apply changes and ensure all services are running with the restored configurations:
      sudo reboot
  7. Verify the Restoration:

    • After rebooting, check that key services like Apache, Docker, and other applications are running correctly.
    • Run systemctl to see the status of services:
      sudo systemctl status apache2
      sudo systemctl status docker

This process should restore all your original files, configurations, and installed applications, effectively cloning your previous setup on the new VPS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment