Transferring Files To and From the Omega using rsync

The second part of this week’s 2-Bullet Tuesday! See the first part here.
You can subscribe to the newsletter on the 2-Bullet Tuesday page!

Omega Tip of the Week:

Transferring Files To and From the Omega using rsync

For Mac and Linux, we can use the command-line utility rsync (remote sync) to transfer files to and from the Omega. It’s included with Mac OS X and most Linux distributions by default (including the Omega). rsyncuses the ssh protocol when connecting to remote servers.

For Windows, there are 3rd party programs you can download that offer the same functionality as rsync. A quick Google search will return several results; try looking at some of the popular ones to see which one is right for your system.

The rsync command can be used to copy files over to a remote device like your Omega. However, if the files already exist then it takes a smarter approach to sending new data. It looks at the differences between the two sets of files, then applies the differences to the files to bring them up-to-date. This is faster than copying the entire files over and over again!

Copying Files to the Omega

To copy a file or entire directory to the Omega, the command looks like this:

rsync -a (LOCAL DIRECTORY OR FILE) root@(OMEGA HOSTNAME OR IP):(DESTINATION DIRECTORY)

Let’s take a closer look at the arguments below:

  • -a – stands for “archive mode”. Equivalent to a combination of several operations, including but not limited to:
    • -r – recursively sync into directories
    • -l – preserve symlinks
    • -t – presreve modification times
    • and a few more useful flags (-pgoD). See the full rsync reference for full details.
    • The -a flag is a very convenient flag that you’ll want to include in most of your typical calls.
  • LOCAL DIRECTORY OR FILE – A filepath to the directory or file on your computer you want to copy over. Can be an absolute or relative filepath, examples:
    • /home/projects/myCoolFolder
    • ./myScript.py
  • OMEGA HOSTNAME OR IP – The name or IP address of the Omega. For example, Omega-7ABC.local or 192.168.1.156
    • If you’re using the Omega’s hostname, the Omega has to be on the same WiFi/LAN for the command to work.
  • DESTINATION DIRECTORY – The directory on the Omega to which to upload the files. For example, ~/myCoolProject

Let’s try some examples. Connect an Omega to your wireless or wired network, then on your own computer run the following commands. Replace Omega-CB07.local with your own Omega’s name or IP:

touch someCoolFile.txt
rsync -a ./someCoolFile.txt root@Omega-CB07.local:~/

Then login to your Omega and look inside the ~ directory:

root@Omega-CB07:/# cd ~
root@Omega-CB07:~# ls
someCoolFile.txt

We uploaded a dummy file to show you how this works, but what if this were an actual code file that you wanted to update? To see how updating works, we’re going to add some text to a file we uploaded previously, then we’ll upload it again. Return to your own computer and enter the following:

echo “Yabba dabba doo!” > someCoolFile.txt
rsync -a ./someCoolFile.txt root@Omega-CB07.local:~/

Now check the file on the Omega:

root@Omega-CB07:~# cat someCoolFile.txt
Yabba dabba doo!

And voila, the file now has our updates! When you’re working on a project, you can write a shell script that uses these commands to update files on the Omega from the comfort of your own computer.

Copying Entire Directories vs Copying Files Only

There are three ways we can copy the files to the Omega. Let’s explore them by creating a test directory with some dummy files on your computer.

On your own computer, create a folder called myDirectory that contains some files called file1.txt and file2.txt using this command:

mkdir myDirectory
cd  myDirectory
touch file1.txt file2.txt
cd ..

1. To copy the entire directory to the Omega, type the local directory name without a slash at the end when running the command like so:

rsync -a ./myDirectory root@Omega-CB07.local:~/

Verify this on your Omega by navigating to the ~ directory:

root@Omega-CB07:~# ls
myDirectory       someCoolFile.txt
root@Omega-CB07:~# cd myDirectory && ls
file1.txt         file2.txt

2. To copy only the files inside a directory, add a slash / to the end of the local directory name like so:

rsync -a ./myDirectory/ root@Omega-CB07.local:~/

Now check your Omega again and notice the difference:

root@Omega-CB07:~# ls
file1.txt         file2.txt         myDirectory       someCoolFile.txt

This time, only file1.txt and file2.txt were uploaded!

3. To copy files from a local directory to a new (renamed) directory on the Omega, type the name of the new directory at the end of the remote path like so:

rsync -a ./myDirectory/ root@Omega-CB07.local:~/newDirectory/

Check it on your Omega:

root@Omega-CB07:~# ls
file1.txt         myDirectory       someCoolFile.txt
file2.txt         newDirectory
root@Omega-CB07:~# cd newDirectory && ls
file1.txt         file2.txt

Pulling Files From the Omega

The commands for downloading files from the Omega work exactly the same way, except you need to switch around the local and remote directories.

rsync -a root@(OMEGA HOSTNAME OR IP):(REMOTE DIRECTORY OR FILE) (LOCAL DIRECTORY)

Try this out by creating a file on your Omega:

root@Omega-CB07:~# echo “Made on the Omega!” > pulledFile.txt

Now on your computer, run the following:

rsync -a root@Omega-CB07.local:~/pulledFile.txt  .

The . means “the current directory”, or whichever directory your terminal has open at the moment.

If you run ls, you’ll now see the new pulledFile.txt that was downloaded from your Omega. Run cat on the file to see the message we wrote earlier:

cat pulledFile.txt
Made on the Omega!

The same syntax for copying entire directories and files as well as copying directories to renamed ones also applies to downloading as shown in the sections above.

And that’s it! For more info on transferring files to and from the Omega, check out our guide in our documentation.
Let us know what kind of stuff you would like to see featured on 2-Bullet Tuesday! Send a tweet to @OnionIoT with your suggestions!

Thanks for reading! Have a great week!

Team Onion

 

P.S. — Our Omega2 Indiegogo campaign will be ending in the near future! Don’t miss your chance to get the Omega2 for just $5!

P.P.S — We’re shipping packages on a daily basis! If you haven’t gotten yours yet, sit tight!

Share

Lazar Demin

Leave your comment