10 examples in 11 days
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 184.108.40.206 IP as remote and 220.127.116.11 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)firstname.lastname@example.org:(7)[remote backup folder]/
Let's see what the above syntax does:
- --progress: shows progress while synching
- --remove-source-files: after transfer it will remove source file
- I used -vhre
- -v: verbose
- -h: human readable
- -r: recursive
- -e: allows us to use ssh as remote shell and execute its commands
- ssh: invoking the ssh service to be used as remote shell.
- backup file to upload with full path
- ssh connection
- 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
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 email@example.com: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
Checking if the tar backup file got removed from source
iamgroot@laptop:~$ ls -a backup-to-upload/ . ..
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 firstname.lastname@example.org Last login: Thu Jul 23 00:38:46 2020 from 18.104.22.168 iamgroot@remote-instance:~$ ls backups scripts iamgroot@remote-instance:~$ ls -a backups/ . .. backup.tar.gz
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 🦄