Rsync is a freely available, open source utility that provides fast incremental file transfer. You can check if and what version of rsync is installed with rsync --version. If rsync is not installed, install it with sudo apt install rsync.

A basic rsync command has the following syntax:

rsync {optional modifiers} {source} {user}@{HOST}:{destination}

To use rsync for the data backup, follow these steps:

  1. Let’s use a cron job to run rsync in regular intervals. To make sure the cron job isn’t starting another instance of rsync every time, a simple shell script will be used. Go to your home directory (cd ~) and create a new file, e.g. nano rsync.sh, and add this code:

    #!/bin/bash
    
    ScriptName="$(basename $0)"
    PATH="{destination path}"
    SERVER=”{IP address}”
    USER="{hostname} "
    
    if [ $(/bin/pidof -x ${ScriptName}| /bin/wc -w) -gt 2 ]; then
        exit
    else
        /bin/rsync -a --log-file=rsync_log.txt --rsync-path="mkdir -p ${PATH} && rsync" --rsh="/bin/ssh -i /home/pi/.ssh/id_rsa" /home/pi/sync/ ${USER}@${SERVER}:$PATH --progress --remove-source-files
    fi
    

    This script checks first if a process with its own name is already running. If there’s no such process, it will run rsync. Rsync will use a log file named rsync_log.txt and at the destination create the directory specified in “PATH”. This rsync commands assumes the ssh ID has already been copied to the remote and the key can be found in /home/pi/.ssh/id_rsa. It will then copy everything inside the /home/pi/sync/ folder to the “PATH” directory at the remote.

    Make the file executable with sudo chmod +x rsync.sh.

  2. Create the cron job by opening crontab with crontab -e and adding * * * * * /home/pi/rsync.sh. With this, rsync will copy all files contained in the local folder to the remote folder once every minute. Once the files have been received and verified by the destination server, the files will be deleted from the source storage to free up space. {source} will correspond to the file path on the local device where the recordings are written to, and {destination} will correspond to the file path on the remote server where they should be transferred to. If using the default Raspberry Pi, {user} will correspond to “pi”. You can backup the crontab file with crontab -I > cronjob.

    The complete rsync command would look like this:

    rsync -a --rsync-path="mkdir -p /home/user/data/test-org/freq/BW/cycle/length/ && rsync" --rsh="ssh -i /home/pi/.ssh/id_rsa" /home/pi/sync/ [email protected]:/home/user/data/test-org/freq/BW/cycle/length/ --remove-source-files