Tag: rsync

  • How to Migrate Linux Server with rsync

    How to Migrate Linux Server with rsync

    A few days ago I wanted to Migrate an Ubuntu VPS from one provider to another. I tried to take a snapshot and try to migrate, but it did not work. The VPS had few random services, so migrating services one by one was not easy. What I did was set up a new Ubuntu VPS with the same OS version, rsync files from source VPS to new VPS.

    Make sure you take a backup of your server before doing it. In case anything goes wrong, you should be able to restore from back or OS reload.

    Install rsync on source and destination servers.

    For Ubuntu/Debian

    apt install rsync

    For RHEL/CentOS/AlmaLinux

    yum install rsync

    On the source server, create a file

    vi /root/no_copy

    add the following content

    /boot/
    /etc/modules
    /etc/fstab
    /etc/mtab
    /etc/netplan/
    /etc/network/
    /etc/sysconfig/network-scripts/
    /lib/modules
    lost+found/
    /sys/
    /proc/
    /dev/
    /var/cache/
    /var/log/journal/
    /swapfile
    /swap.img
    /usr/tmpDSK
    /root/.ssh/
    /backup/
    /home/virtfs/

    These files won’t be copied to the new VPS. If you don’t have any other folders or files, that you don’t want to be copied to the new server, you can add to the list.

    Now run rsync on the source server to copy over files to the new server

    rsync -avzPHAX --exclude-from=/root/no_copy / root@NEW_SERVER_IP:/

    In the command above, replace NEW_SERVER_IP with the new server IP address.

    When we do rsync, all files get copied to the new server. After the first rsync run, you can run the rsync command any number of times, only changed files copied, and will finish much faster as less data need to be copied to the new server.

    After the first rsync is completed, we will stop all services on the source and destination servers like MySQL, Apache, Nginx, etc… so no data gets changed while we doing the rsync. Now do another rsync, reboot the destination server, see if everything works fine.

    See rsync

  • Run rsync if not running using cronjob

    Run rsync if not running using cronjob

    I want to rsync files from one server to another server every 5 minutes, but only want to start the rsync if the previous rsync command has finished. Time for one rsync depends on how much data changed on the source server, so the time taken to finish rsync cronjob varies.

    Create file rsync.sh

    #!/bin/bash
    
    if ! pgrep -x "rsync" > /dev/null
    then
        rsync -avzP /var/www/html/files/  [email protected]:/var/www/html/files/
    fi
    

    Set cronjob like

    */10 * * * * /root/rsync.sh > /dev/null
    
  • Bandwidth Limit on rsync

    I wanted to transfer some files between two computers, but don’t want to use all bandwidth available on the network as it will affect other users on the network.

    To limit bandwidth, use –bwlimit Option.

    rsync -avzP  --bwlimit=1500 /mnt/data/learn/css/BootStrap3/ [email protected]:/home/php-tutorial/BootStrap3/
    

    Here –bwlimit=1500 will limit bandwidth usage to 1.5 MB/s.

    See rsync

  • Restart rsync on failure

    When copying a large site form a shared server using rsync, the rsync process on get killed, this may be done by some program on shared host or server admin manually killing the process.

    Here is a bash script that will check if rsync exited normally or not, then retry the trasfter is rsync failure detected.

    #!/bin/bash
    
    while [ 1 ]
    do
        rsync -avzP [email protected]:/kunden/homepages/18/d686010467/htdocs/jobformazione/ /home/jobformazione/
        if [ "$?" = "0" ] ; then
            echo "rsync completed normally"
            exit
        else
            echo "Rsync failed. Retrying..."
            sleep 180
        fi
    done
    

    Save the file as 1.sh, then run it with

    bash ./1.sh
    

    You need to add servers SSH key in remote server so rsync work with out password.

  • Sync Files between 2 servers

    Here are some tools that can be used to sync files between 2 servers.

  • rsync

    rsync command with exclude option, full server backup to a remote server.

    rsync -avz --progress --human-readable "-e ssh -p 3333" --exclude /backup --exclude /proc --exclude /sys / root@REMOTE_SERVER_IP:/backup/serverX/
    

    Transfer a file

    I normally use scp to do single file transfer. It may be better to it with rsync as you will be able to resume.

    rsync -P -e ssh root@SERVER_IP:/path/to/file.tar.gz /local/path/

    -P == –partial –progress

    If you use non standard SSH port, then replace -e ssh with “-e ssh -p PORT_NUMBER”.

    Example

    boby@hon-pc-01:~$ rsync -P "-e ssh -p 3333" root@s12:/home/cloud-architect.tar.bz2 /backup/learn/cloud-architect.tar.bz2
      3,120,159,768 100%  995.98kB/s    0:36:27 (xfr#1, to-chk=0/1)
    boby@hon-pc-01:~$ 
    

    See backup