From 46e00518dec147c889d2d9e1a20d0dc1806ee7e6 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sun, 2 Oct 2016 18:37:01 -0300 Subject: Refactor sync-media --- doc/todo.rst | 2 +- share/hydractl/sync-media | 144 ++++++++++++++++++++++++---------------------- 2 files changed, 75 insertions(+), 71 deletions(-) diff --git a/doc/todo.rst b/doc/todo.rst index 3f538a1..c39949f 100644 --- a/doc/todo.rst +++ b/doc/todo.rst @@ -1,7 +1,7 @@ TODO ==== -- sync-media: proper initialization for new machines. +- sync-media: use unison instead of rsync. - mount-media: lvm support. - import-certs: - concat.pem. diff --git a/share/hydractl/sync-media b/share/hydractl/sync-media index a4fb69a..e8f01dd 100755 --- a/share/hydractl/sync-media +++ b/share/hydractl/sync-media @@ -144,80 +144,63 @@ if [ ! -z "$REMOTE" ]; then fi fi -# Commit changes -if [ -d "$CACHE" ]; then - # Fix cache permissions - #echo "Fixing $CACHE permissions..." - #$sudo find $CACHE -type f -exec chmod 644 {} \; - #$sudo find $CACHE -type d -exec chmod 755 {} \; +# Ensure cache exists +mkdir -p $CACHE + +# Fix cache permissions +#echo "Fixing $CACHE permissions..." +#$sudo find $CACHE -type f -exec chmod 644 {} \; +#$sudo find $CACHE -type d -exec chmod 755 {} \; +sync_media_incoming_perms + +# Iterate over existing repositories in the local cache +for folder in `ls $CACHE`; do + # Sync each repository in the local cache + if [ -d "$CACHE/$folder/.git/annex" ]; then + if [ "`git -C $CACHE/$folder config sync-media.skip`" == "true" ]; then + continue + fi - sync_media_incoming_perms + ( + cd $CACHE/$folder + echo "Syncing $CACHE/$folder..." + + sync_media_playlist_perms + sync_media_ensure_remote $REMOTE $VOLUME/$MEDIA/$folder + sync_media_identity + sync_media_add + git annex sync + sync_media_getall $CACHE/$folder + sync_media_fsck + sync_media_dropunused + ) + fi + + # Ensure the removable media has each repository listed in the local cache + if [ ! -z "$DRIVE" ]; then + mkdir -p $VOLUME/$MEDIA - # Add and update local repositories - for folder in `ls $CACHE`; do if [ -d "$CACHE/$folder/.git/annex" ]; then - if [ "`git -C $CACHE/$folder config sync-media.skip`" == "true" ]; then - continue + if [ ! -d "$VOLUME/$MEDIA/$folder" ]; then + ( + cd $VOLUME/$MEDIA + echo "Initializing $VOLUME/$MEDIA/$folder..." + git clone $CACHE/$folder && cd $folder && sync_media_identity && git annex init $DRIVE && \ + git remote rename origin $HOST && cd $CACHE/$folder && git remote add $DRIVE $VOLUME/$MEDIA/$folder + ) fi - - #if [ "`git -C $CACHE/$folder config sync-media.ready`" != "true" ]; then - # echo "Skipping $CACHE/$folder: not sync-media ready, please config your repo." - # continue - #fi - - ( - cd $CACHE/$folder - echo "Syncing $CACHE/$folder..." - - sync_media_playlist_perms - sync_media_ensure_remote $REMOTE $VOLUME/$MEDIA/$folder - sync_media_identity - sync_media_add - git annex sync - sync_media_getall $CACHE/$folder - sync_media_fsck - sync_media_dropunused - ) - fi - done - - if [ ! -z "$DRIVE" ]; then - if [ ! -d "$VOLUME/$MEDIA" ]; then - echo "Folder $VOLUME/$MEDIA does not exist..." - else - for folder in `ls $CACHE`; do - if [ -d "$CACHE/$folder/.git/annex" ]; then - if [ "`git -C $CACHE/$folder config sync-media.skip`" == "true" ]; then - continue - fi - - #if [ "`git -C $CACHE/$folder config sync-media.ready`" != "true" ]; then - # echo "Skipping $CACHE/$folder: not sync-media ready, please config your repo." - # continue - #fi - - if [ ! -d "$VOLUME/$MEDIA/$folder" ]; then - ( - cd $VOLUME/$MEDIA - echo "Initializing $VOLUME/$MEDIA/$folder..." - git clone $CACHE/$folder && cd $folder && sync_media_identity && git annex init $DRIVE && \ - git remote rename origin $HOST && cd $CACHE/$folder && git remote add $DRIVE $VOLUME/$MEDIA/$folder - ) - fi - elif [ ! -d "$VOLUME/$MEDIA/$folder" ]; then - if [ ! -e "$CACHE/$folder/.sync-media/skip" ]; then - echo "Syncing $VOLUME/$MEDIA/$folder..." - rsync -av --delete --exclude=.sync-media $CACHE/$folder/ $VOLUME/$MEDIA/$folder/ - fi - fi - done + elif [ ! -d "$VOLUME/$MEDIA/$folder" ]; then + mkdir -p $VOLUME/$MEDIA/$folder fi fi -fi +done -# Retrieve changes at media volumes +# Process removable or remote media if [ ! -z "$DRIVE" ] && [ -d "$VOLUME/$MEDIA" ]; then + # Iterate over existing repositories in the removable media for folder in `ls $VOLUME/$MEDIA`; do + + # Sync each local repository in the removable media if [ -d "$VOLUME/$MEDIA/$folder/.git/annex" ]; then if [ "`git -C $VOLUME/$MEDIA/$folder config sync-media.skip`" == "true" ]; then continue @@ -239,10 +222,31 @@ if [ ! -z "$DRIVE" ] && [ -d "$VOLUME/$MEDIA" ]; then #git annex drop --auto --numcopies=2 ) elif [ -d "$CACHE/$folder" ]; then - # Avoid empty source folders or those configured to be skipped - if [ ! -e "$CACHE/$folder/.sync-media/skip" ] && [ ! -z "`ls -1 $CACHE/$folder`" ]; then - echo "Syncing $VOLUME/$MEDIA/$folder..." - rsync -av --delete --exclude=.sync-media $CACHE/$folder/ $VOLUME/$MEDIA/$folder/ + # Avoid those configured to be skipped + if [ ! -e "$CACHE/$folder/.sync-media/skip" ]; then + # Avoid empty source folders + if [ ! -z "`ls -1 $CACHE/$folder`" ]; then + echo "Syncing $CACHE/$folder into $VOLUME/$MEDIA/$folder..." + rsync -av --delete --exclude=.sync-media $CACHE/$folder/ $VOLUME/$MEDIA/$folder/ + elif [ ! -z "`ls -1 $VOLUME/$MEDIA/$folder`" ]; then + echo "Syncing $VOLUME/$MEDIA/$folder into $CACHE/$folder..." + rsync -av --delete --exclude=.sync-media $VOLUME/$MEDIA/$folder/ $CACHE/$folder/ + fi + fi + fi + + # Ensure the local cache has each repository listed in the removable media + if [ ! -d "$CACHE/$folder" ]; then + if [ -d "$VOLUME/$MEDIA/$folder/.git/annex" ]; then + ( + cd $CACHE + echo "Initializing $CACHE/$folder..." + git clone $VOLUME/$MEDIA/$folder && cd $folder && sync_media_identity && git annex init $HOST && \ + git remote rename origin $REMOTE && cd $VOLUME/$MEDIA/$folder && git remote add $HOST $CACHE/$folder + ) + else + echo "Syncing $VOLUME/$MEDIA/$folder into $CACHE/$folder..." + rsync -av --delete --exclude=.sync-media $VOLUME/$MEDIA/$folder/ $CACHE/$folder/ fi fi done @@ -264,7 +268,7 @@ elif [ ! -z "$REMOTE" ]; then else # Avoid empty source folders or those configured to be skipped if [ ! -e "$CACHE/$folder/.sync-media/skip" ] && [ ! -z "`ls -1 $CACHE/$folder`" ]; then - echo "Syncing $VOLUME/$MEDIA/$folder..." + echo "Syncing $CACHE/$folder into $REMOTE.$DOMAIN:$CACHE/$folder..." rsync -av --delete --exclude=.sync-media $CACHE/$folder/ $REMOTE.$DOMAIN:$CACHE/$folder/ fi fi -- cgit v1.2.3