Run a Small Backup Service with Rsync and Cron

I shared a code that allows to run a small backup service with rsync and cron. The goal is to run an independent service that will runs every hour or every day, connect to remote SFTP server and upload the changes. Rsync allow to upload only changed or new files. This solution works very fast.

Let’s looks at the rsync command located in the file.

rsync -avzh --info=progress2 -e 'ssh -p22 -i /.ssh/private -o StrictHostKeychecking=no' SOURCE_DIR user@server:DESTINATION_DIR

String -i /.ssh/private says that the ssh key will be used to access the remote server. String -o StrictHostKeychecking=no says that the host verification step will be ignored.

I run all things via docker-compose using the pretty simple configuration.

version: '3'


    build: .
    container_name: cron-rsync-backup
    restart: always
      - ${SSH_KEY_PATH}:/.ssh/private:ro

I use the .env files to provide all environment variables to the docker container on a service start. I mount the ssh key and the host directory that should be backed up.

Edit the crontab file to change the scheduling.

0 * * * * /bin/sh / > /proc/1/fd/1 2>/proc/1/fd/2

When things are ready, run the service.

docker-compose up --build -d