#!/bin/bash # # sync-media assets using git-annex # REMOTE="$1" VOLUME="/media/$REMOTE" CACHE="/var/cache/media" DOMAIN="`facter domain`" HOST="`facter hostname`" MEDIA="media.$DOMAIN" INCOMING="$CACHE/incoming" WHOAMI="`whoami`" OPTIONS="$*" # Fix identity function sync_media_identity { if [ -z "`git config --local user.email`" ] || [ -z "`git config --local user.name`" ]; then repo="$(basename `pwd`)" git config user.name "${repo^} Archive" git config user.email "$repo@localhost" fi } # Add files into the annex function sync_media_add { git annex add . # Adding hidden files and symlinks, find version #find -name '.*' -exec git annex add {} \; #find -type l -exec git add {} \; # Adding hidden files and symlinks, git+for version #for file in `git status --porcelain -u | sed -e 's/?? //'`; do # if [ -h "$file" ]; then # git add $file # else # git annex add $file # fi #done # Adding hidden files and symlinks, git+while version git status --porcelain -u | sed -e 's/?? //' | while read file; do if [ -h "$file" ]; then git add "$file" else git annex add "$file" fi done } # Set sudo config if [ "$WHOAMI" != 'root' ]; then sudo="sudo" else echo "Sorry, cannot run as root" exit 1 fi # Set fsck config if echo $OPTIONS | grep -q -- "--fsck"; then FSCK="true" fi # Set unused config if echo $OPTIONS | grep -q -- "--dropunused"; then DROPUNUSED="true" 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 {} \; # Fix incoming permissions if [ -d "$INCOMING" ]; then echo "Fixing $INCOMING permissions..." $sudo find $INCOMING -type f -exec chmod 664 {} \; $sudo find $INCOMING -type d -exec chmod 775 {} \; $sudo chown -R $WHOAMI.incoming $INCOMING fi # 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 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..." # If there is a playlists folder, make sure mpd user can write to it if [ -d "playlists" ]; then $sudo chmod 775 playlists $sudo chown -R mpd.audio playlists $sudo chmod 664 playlists/* fi sync_media_identity sync_media_add git annex sync if [ "$FSCK" == "true" ]; then git annex fsck --fast fi if [ "$DROPUNUSED" == "true" ]; then git annex unused git annex dropunused 1-1000 fi ) fi done if [ ! -z "$REMOTE" ]; then # Check storage media MOUNT="`mount | grep $VOLUME`" if [ ! -z "$MOUNT" ]; then DRIVE="$(basename `echo $MOUNT | awk '{ print $1 }'`)" fi 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 && \ cd $CACHE/$folder && git remote add $DRIVE $VOLUME/$MEDIA/$folder ) fi elif [ ! -d "$VOLUME/$MEDIA/$folder" ]; then echo "Syncing $VOLUME/$MEDIA/$folder..." rsync -av --delete $CACHE/$folder/ $VOLUME/$MEDIA/$folder/ fi done fi fi fi fi # Retrieve changes at media volumes if [ ! -z "$DRIVE" ] && [ -d "$VOLUME/$MEDIA" ]; then for folder in `ls $VOLUME/$MEDIA`; do if [ -d "$VOLUME/$MEDIA/$folder/.git/annex" ]; then if [ "`git -C $VOLUME/$MEDIA/$folder config sync-media.skip`" == "true" ]; then continue fi ( cd $VOLUME/$MEDIA/$folder echo "Syncing $VOLUME/$MEDIA/$folder..." # Check remote if ! git remote | grep -q "^$HOST$"; then git remote add $HOST $CACHE/$folder fi sync_media_identity sync_media_add git annex sync git annex get . --numcopies=3 if [ "$FSCK" == "true" ]; then git annex fsck --fast fi if [ "$DROPUNUSED" == "true" ]; then git annex unused git annex dropunused 1-1000 fi #git annex drop --auto --numcopies=2 ) elif [ -d "$CACHE/$folder" ]; then echo "Syncing $VOLUME/$MEDIA/$folder..." rsync -av --delete $CACHE/$folder/ $VOLUME/$MEDIA/$folder/ fi done elif [ ! -z "$REMOTE" ]; then # Try to copy to a remote 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 remote | grep -q "^$REMOTE$"; then ( cd $CACHE/$folder git annex copy . --to $REMOTE ) else echo "No such remote $REMOTE" fi else echo "Syncing $VOLUME/$MEDIA/$folder..." rsync -av --delete $CACHE/$folder/ $REMOTE.$DOMAIN:$CACHE/$folder/ fi done fi