Skip to content

Instantly share code, notes, and snippets.

@canonex
Last active September 6, 2025 08:56
Show Gist options
  • Save canonex/63668f6a40c5528f1bbfdce6f8afffc8 to your computer and use it in GitHub Desktop.
Save canonex/63668f6a40c5528f1bbfdce6f8afffc8 to your computer and use it in GitHub Desktop.

Revisions

  1. canonex revised this gist Sep 6, 2025. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions flockRsyncBackup.sh
    Original file line number Diff line number Diff line change
    @@ -7,3 +7,7 @@
    # Flock use a lock file: if it already exist it does not execute the command (-n option)
    # Read more on https://www.man7.org/linux/man-pages/man1/flock.1.html
    flock -n rsyncExec.lock -c 'rsync -aAXH --info=progress2 --delete --delete-excluded -e "ssh -p 12345 -i /myuser/.ssh/mykey" /myrepo/mydata/ [email protected]:/mybackup/myfolder/'

    # An example using ssh config file and basic logging
    flock -n rsyncExec.lock -c 'rsync -aAXH --info=progress2 --delete --delete-excluded /myrepo/mydata/ MyServerInSshConfig:/mybackup/myfolder/; echo Exit code: $? - $(date) >> backup.log'
    # instead of using a log file it is also possible to use the system log using logger instead of echo
  2. Riccardo Gagliarducci created this gist Jun 30, 2022.
    9 changes: 9 additions & 0 deletions flockRsyncBackup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #!/bin/bash

    # Example of flock use with rsync where local files are pushed to a remote server using a ssh and a key.
    # Using flock is useful when file copying is slow and the transfer duration may overlap with a newly scheduled run.
    # For example, if a large number of files are uploaded in one day in an office, whose backup is scheduled every day, and the transfer may take more than 24 hours.

    # Flock use a lock file: if it already exist it does not execute the command (-n option)
    # Read more on https://www.man7.org/linux/man-pages/man1/flock.1.html
    flock -n rsyncExec.lock -c 'rsync -aAXH --info=progress2 --delete --delete-excluded -e "ssh -p 12345 -i /myuser/.ssh/mykey" /myrepo/mydata/ [email protected]:/mybackup/myfolder/'