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 backup.sh
file.
rsync -avzh --info=progress2 -e 'ssh -p22 -i /.ssh/private -o StrictHostKeychecking=no' SOURCE_DIR [email protected]: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'
services:
rsync-cron-backup:
build: .
container_name: cron-rsync-backup
environment:
- SFTP_SERVER=${SFTP_SERVER}
- DESTINATION_DIR=${DESTINATION_DIR}
- SOURCE_DIR=${SOURCE_DIR}
restart: always
volumes:
- ${SSH_KEY_PATH}:/.ssh/private:ro
- ${HOST_SOURCE_DIR}:${SOURCE_DIR}: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 /backup.sh > /proc/1/fd/1 2>/proc/1/fd/2
When things are ready, run the service.
docker-compose up --build -d