DEV Community

Cover image for rsync - 10 examples in 11 days (Day 07)
m4r4v
m4r4v

Posted on

2

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 🦄

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay