Skip to content

Instantly share code, notes, and snippets.

@Nateowami
Last active May 6, 2023 14:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nateowami/722edc74c15ee25c0e9c to your computer and use it in GitHub Desktop.
Save Nateowami/722edc74c15ee25c0e9c to your computer and use it in GitHub Desktop.
Auto-Sync with Unison
Summery

This is a simple script for syncing Unison profiles. Basically, given a list of profiles (which must already be created in Unison), it will sync all files with non-conflicting states. It logs the time of start and completion to unison.log (change OUTPUT=$HOME/unison.log if you want it to output somewhere else), as well as listing the output Unison gives for each sync. Unison already knows the hosts to sync, because that's recorded by Unison in the profiles it stores in ~/.unison. And you'll need SSH keys already setup.

Automating

If you want to automate this script in Ubuntu, search the dash for "Startup Applications" and open it up, then click on "add" in the window that opens. You'll be prompted for a name and command. Name it anything you like and make the command the path to the script, e.g. ~/scripts/unison_sync.sh. Now it will run every time you login.

Disclaimer

Rest assured I have no idea what I'm doing. This is literally my first bash script, and it comes with ABSOLUTELY NO WARRANTY WHATSOEVER! It's experimental and may do anything it wants including, but not limited to, making loud chewing noises in its sleep or making rude comments about your breath. USE AT YOUR OWN RISK!

Feedback

Feel free to leave feedback or questions (which I probably can't answer) below. If you have a question along the lines of "Why did you write the script like this," then you probably forgot to read the disclaimer!

#!/bin/bash
# declare the output file for logging
OUTPUT=$HOME/unison.log
# header to print each time we sync
echo "*****************************************************" >> $OUTPUT
echo "STARTING UNISON SYNC AT $(date)" >> $OUTPUT
echo "*****************************************************" >> $OUTPUT
echo "" >> $OUTPUT
# a list Unison profiles to sync (must already be created in Unison)
declare -a PROFILES=(Documents Music Pictures Programming Videos)
# loop through the profiles
for p in ${PROFILES[@]}; do
echo "***ABOUT TO SYNC $p***" >> $OUTPUT
# run the sync and concat all standard output and errors to the output file
unison -auto -batch -ui text $p >> $OUTPUT 2>&1
echo "" >> $OUTPUT
done
# say we're done
echo "*****FINISHED AT $(date)*****" >> $OUTPUT
echo "" >> $OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment