aboutsummaryrefslogtreecommitdiff
path: root/share/hydractl/sync-media
blob: d6102451173d1283173ae998500e628087904535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash
#
# sync-media assets using git-annex
#

REMOTE="$1"
VOLUME="/media/$REMOTE"
DOMAIN="`facter domain`"
HOST="`facter hostname`"
CACHE="/var/cache/$HOST/media"
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, 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
}

# If there is a playlists folder, make sure mpd user can write to it
function sync_media_playlist_perms {
  if [ -d "playlists" ]; then
    $sudo chmod 775 playlists
    $sudo chown -R mpd.audio playlists
    find playlists -type f -exec sudo chmod 664 {} \;
    find playlists -type d -exec sudo chmod 775 {} \;
  fi
}

# Fix incoming permissions
function sync_media_incoming_perms {
  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
}

# Run fsck
function sync_media_fsck {
  if [ "$FSCK" == "true" ]; then
    git annex fsck --fast
  fi
}

# Run dropunused
function sync_media_dropunused {
  if [ "$DROPUNUSED" == "true" ]; then
    git annex unused
    git annex dropunused 1-1000
  fi
}

# Get copies of annexed files
function sync_media_get {
  local repo="$1"
  local numcopies

  if [ "`git -C $repo config sync-media.get`" != "false" ]; then
    if git -C $repo config sync-media.numcopies &> /dev/null; then
      numcopies="`git -C $repo config sync-media.numcopies`"
    else
      numcopies="3"
    fi

    git annex get . --numcopies=$numcopies
  fi
}

# Control whether the repository should have a copy of everything
function sync_media_getall {
  local repo="$1"

  if [ "`git -C $repo config sync-media.getall`" == "true" ]; then
    git annex get .
  fi
}

# Ensure we have a reference to the remote repository
function sync_media_ensure_remote {
  local remote="$1"
  local path="$2"

  if [ -z "$remote" ] || [ -z "$path" ]; then
    return
  fi

  # Check for local or remote repo
  if [ -z "$DRIVE" ]; then
    path="$REMOTE.$DOMAIN:$path"
  elif [ ! -d "$path/.git/annex" ]; then
    return
  fi

  if ! git remote | grep -q "^$remote$"; then
    echo git remote add $remote $path
  fi
}

# 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

# Set drive config
if [ ! -z "$REMOTE" ]; then
  # Check storage media
  MOUNT="`mount | grep $VOLUME`"
  if [ ! -z "$MOUNT" ]; then
    DRIVE="$(basename `echo $MOUNT | awk '{ print $1 }'`)"
  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 {} \;

  sync_media_incoming_perms

  # 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..."

      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
    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..."

      sync_media_ensure_remote $HOST $CACHE/$folder
      sync_media_playlist_perms
      sync_media_identity
      sync_media_add
      git annex sync
      sync_media_get    $VOLUME/$MEDIA/$folder
      sync_media_getall $VOLUME/$MEDIA/$folder
      sync_media_fsck
      sync_media_dropunused
      #git annex drop --auto --numcopies=2
      )
    elif [ -d "$CACHE/$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 [ ! -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

      sync_media_ensure_remote $REMOTE $CACHE/$folder

      (
      cd $CACHE/$folder
      git annex copy . --to $REMOTE
      git annex sync
      )
    else
      if [ ! -e "$CACHE/$folder/.sync-media/skip" ]; then
        echo "Syncing $VOLUME/$MEDIA/$folder..."
        rsync -av --delete --exclude=.sync-media $CACHE/$folder/ $REMOTE.$DOMAIN:$CACHE/$folder/
      fi
    fi
  done
fi