rsync - 10 examples in 11 days (Day 07)

m4r4v

m4r4v

Posted on July 23, 2020

rsync - 10 examples in 11 days (Day 07)

Day 07

10 examples in 11 days

Sync Local to Remote over SSH

Today I will talk about how we can sync files from our local machine to our remote machine using ssh.

I have a remote machine where I will be connecting to. I use ssh with a public and private key to connect to it and this way I will not have to be asked for a password.

Note: I will not show the local nor remote machines IP. Instead I will always display 111.111.111.111 IP as remote and 222.222.222.222 as local

I have a folder called ~/backup-to-upload/. Inside of this folder I store the latest backup file as a .tar.gz. This file will be sitting there waiting for me to rsync it into my remote machine. Once I have upload the tar backup, it gets deleted leaving the ~/backup-to-upload/ folder empty.

Let's see the syntax:

iamgroot@laptop:~$ rsync (1)--progress (2)--remove-source-files (3)-vhre (4)ssh (5)[backup file to upload] (6)iamgroot@111.111.111.111:(7)[remote backup folder]/
Enter fullscreen mode Exit fullscreen mode

Let's see what the above syntax does:

  1. --progress: shows progress while synching
  2. --remove-source-files: after transfer it will remove source file
  3. I used -vhre
  • -v: verbose
  • -h: human readable
  • -r: recursive
  • -e: allows us to use ssh as remote shell and execute its commands
  1. ssh: invoking the ssh service to be used as remote shell.
  2. backup file to upload with full path
  3. ssh connection
  4. remote folder

Note: If the remote folder doesn't exist it will be created

Let's see what I have at my remote machine before we run the rsync command

iamgroot@remote-instance:~$ ls
scripts
Enter fullscreen mode Exit fullscreen mode

Now let's upload our tar file to the remote machine

iamgroot@laptop:~$ rsync --progress --remove-source-files -vhre ssh /home/iamgroot/backup-to-upload/backup.tar.gz  iamgroot@111.111.111.111:backups/
sending incremental file list
backup.tar.gz
            195 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 295 bytes  received 43 bytes  75.11 bytes/sec
total size is 195  speedup is 0.58
Enter fullscreen mode Exit fullscreen mode

Checking if the tar backup file got removed from source

iamgroot@laptop:~$ ls -a backup-to-upload/
.  ..
Enter fullscreen mode Exit fullscreen mode

Empty!!!

Now let's check on the remote machine and first I will check if the folder backups was created and then if it has our backup.tar.gz

iamgroot@laptop:~$ ssh iamgroot@111.111.111.111
Last login: Thu Jul 23 00:38:46 2020 from 222.222.222.222
iamgroot@remote-instance:~$ ls
backups  scripts
iamgroot@remote-instance:~$ ls -a backups/
.  ..  backup.tar.gz
Enter fullscreen mode Exit fullscreen mode

As you can see, it not just created the backups folder, it uploaded our tar file.

Awesome, another example accomplished!


Ok, that'll be for today's example, thanks for reading, ping me if you need any help!!!

Follow, ❤ or 🦄

💖 💪 🙅 🚩
m4r4v
m4r4v

Posted on July 23, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related