How To Use Rsync To Deploy Your Application Or Website Easily

No Comments
Modified: 09.10.2023

Rsync is a fast file-copying tool, and in this post, you will learn how you can use it to deploy your application or website to a server easily!

Don’t want to read? Watch the video instead!

What is rsync

Rsync is a versatile command-line utility and protocol for synchronizing files and directories on one local system or over a network. It efficiently updates data by copying only the parts of files that have changed, making it ideal for tasks like backups, mirroring data between locations, and deploying your application. rsync preserves file attributes, permissions, and timestamps, ensuring the integrity of your data during synchronization, whether it’s done locally or over a network. This powerful tool is essential for system administrators and users who need reliable file synchronization.

Rsync basics

You can sync two directories with each other by running:

rsync -a dir1/ dir2

With the flag -a you sync recursively and preserve symbolic links, special and device files, modification times, groups, owners, and permissions. It is a recommended flag to use whenever you run rsync.

To better understand what any rsync run do you can additionally use the flag -n for a dry run and -v for verbose output:

rsync -anv dir1/ dir2

With that, we learned about the basic usage of rsync. Next, we will use it to deploy an application to a server.

Need help or want to share feedback? Join my discord community!

Rsync to automatically deploy your website with all changes

To deploy an application or website to your server, we need to use the flags -a, -P to continue the transfer in case of connection loss and –delete to delete files that were deleted in the source. Finally, we need to sync the directory and not the files in the directory.

rsync -aP --delete src/ <user>@<host>:<dest>/

You can add this to the build command of your application, for example, inside the build script in a package.json.

KOFI Logo

If this guide is helpful to you and you like what I do, please support me with a coffee!

Additionally, this can be nicely combined with caddy and the file_server directive to deliver static websites. You can learn more about Caddy here.

Problems

If the directory you try to sync does not delete old files, make sure that you are synchronizing a directory and not only the files!
So instead of rsync -aP --delete src/* dest/ do this rsync -aP --delete src/ dest/.

Conclusion

We learned how to easily deploy an application or website to a server using rsync. This is a bit of a different take compared to using pipelines and docker from this post here and here. It is simpler and more time-effective but not suitable for all use cases.

I hope this post was helpful to you, and if you have any questions, feel free to ask them in the comments. And if you want to get updated on my latest posts, subscribe to my newsletter!

[convertkit form=2303042]

Discussion (0)