|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# import the contents of the database archive |
| 4 | + |
| 5 | +: ${IMPORT_LEVEL:=1} |
| 6 | + |
| 7 | +# If archive IMPORT has been defined, and there is no existing archive, then perform the import |
| 8 | +if [ -n "$IMPORT_LOCAL$IMPORT_REMOTE" -a ! -f "$ARCHIVE_DIR/1.atm" -a "$runningArchives" -eq 0 ]; then |
| 9 | + echo "Importing into empty archive..." |
| 10 | + [[ -n "$IMPORT_REMOTE" && "$IMPORT_REMOTE" != ?*://?* ]] && echo "ERROR: IMPORT_REMOTE is not a valid URL: $IMPORT_REMOTE - import aborted" && exit 98 |
| 11 | + |
| 12 | + # clean up any tombstone of the archive for this SM |
| 13 | + if [ -n "$myArchive" ]; then |
| 14 | + echo "Cleaning up archive tombstone for $HOSTNAME: $myArchive..." |
| 15 | + [ $(nuocmd get archives --db-name $DB_NAME | wc -l) -eq 1 ] && echo "Cleaning up database first..." && nuocmd delete database --db-name $DB_NAME 2>&1 || exit 98 |
| 16 | + nuocmd delete archive --archive-id $myArchive --purge 2>&1 || exit 98 |
| 17 | + fi |
| 18 | + |
| 19 | + # if IMPORT_REMOTE is set - work out whether to import from existing (IMPORT_LOCAL) cache |
| 20 | + importFromCache='false' |
| 21 | + if [ -n "$IMPORT_REMOTE" ]; then |
| 22 | + [ -n "$IMPORT_AUTH" -a "$IMPORT_AUTH" != ':' ] && curlAuth="--user $IMPORT_AUTH" |
| 23 | + if [ -n "$IMPORT_LOCAL" ]; then |
| 24 | + |
| 25 | + # IMPORT_LOCAL is an empty dir |
| 26 | + if [ -d "$IMPORT_MOUNT" -a $(ls -1 "$IMPORT_MOUNT" | wc -l) -eq 0 ]; then |
| 27 | + echo "Extracting and caching $IMPORT_REMOTE into directory host:$IMPORT_LOCAL..." |
| 28 | + time curl -k ${curlAuth:-} "$IMPORT_REMOTE" | tar xzf - --strip-components ${IMPORT_LEVEL} -C $IMPORT_MOUNT || exit 98 |
| 29 | + importFromCache='true' |
| 30 | + |
| 31 | + # IMPORT_LOCAL is an empty file |
| 32 | + elif [ ! -s "$IMPORT_MOUNT" ]; then |
| 33 | + echo "Caching $IMPORT_REMOTE into file host:$IMPORT_LOCAL..." |
| 34 | + time curl -k ${curlAuth:-} "$IMPORT_REMOTE" > "$IMPORT_MOUNT" || exit 98 |
| 35 | + importFromCache='true' |
| 36 | + |
| 37 | + # IMPORT_LOCAL is not empty - assume it is a valid cache |
| 38 | + else |
| 39 | + echo "host:$IMPORT_LOCAL is not empty - assuming it contains a cached copy of $IMPORT_REMOTE." |
| 40 | + importFromCache='true' |
| 41 | + fi |
| 42 | + |
| 43 | + # IMPORT_LOCAL is not set - so there is no local cache |
| 44 | + else |
| 45 | + echo "IMPORT_LOCAL is not set - caching disabled." |
| 46 | + echo "Importing from $IMPORT_REMOTE into $ARCHIVE_DIR..." |
| 47 | + time curl -k ${curlAuth:-} "$IMPORT_REMOTE" | tar xzf - --strip-components ${IMPORT_LEVEL} -C $ARCHIVE_DIR || exit 98 |
| 48 | + fi |
| 49 | + |
| 50 | + # IMPORT_REMOTE is not set, so check that IMPORT_LOCAL is not empty |
| 51 | + else |
| 52 | + [ -f "$IMPORT_MOUNT" -a ! -s "$IMPORT_MOUNT" ] && echo "ERROR: IMPORT_LOCAL file host:$IMPORT_LOCAL is empty." && exit 98 |
| 53 | + [ -d "$IMPORT_MOUNT" -a $(ls -1 "$IMPORT_MOUNT" | wc -l) -eq 0 ] && echo "ERROR: IMPORT_LOCAL directory host:$IMPORT_LOCAL is empty." && exit 98 |
| 54 | + importFromCache='true' |
| 55 | + fi |
| 56 | + |
| 57 | + # IMPORT_LOCAL should now have the correct content - import it into the archive |
| 58 | + if [ -n "$IMPORT_LOCAL" ]; then |
| 59 | + [ -n "$IMPORT_REMOTE" -a "$importFromCache" = 'true' -a -s "$IMPORT_MOUNT" ] && echo "Using host:$IMPORT_LOCAL as a cached copy of $IMPORT_REMOTE..." |
| 60 | + if [ -d "$IMPORT_MOUNT" ]; then |
| 61 | + echo "Importing directory host:$IMPORT_LOCAL into $ARCHIVE_DIR..." |
| 62 | + time nuodocker restore archive --origin-dir $IMPORT_MOUNT --restore-dir $ARCHIVE_DIR --db-name "$DB_NAME" --clean-metadata || exit 98 |
| 63 | + elif [ "$importFromCache" = 'true' -a -s "$IMPORT_MOUNT" ]; then |
| 64 | + echo "Importing file host:$IMPORT_LOCAL into $ARCHIVE_DIR..." |
| 65 | + time tar xf "$IMPORT_MOUNT" --strip-components ${IMPORT_LEVEL} -C "$ARCHIVE_DIR" || exit 98 |
| 66 | + else |
| 67 | + echo "ERROR: IMPORT_LOCAL has been specified, but host:$IMPORT_LOCAL is not a valid import source - IMPORT_LOCAL must be a directory, an initially empty file, or a cached copy of IMPORT_REMOTE - import aborted..." |
| 68 | + exit 98 |
| 69 | + fi |
| 70 | + fi |
| 71 | + |
| 72 | + # sanity check the imported content in the archive |
| 73 | + [ -d "$ARCHIVE_DIR/full" ] && echo "ERROR: Imported data looks like a BACKUPSET (in which case IMPORT_LOCAL must be a DIRECTORY): $(ls -l $ARCHIVE_DIR | head -n 10)" && exit 98 |
| 74 | + [ ! -f "$ARCHIVE_DIR/1.atm" ] && echo "ERROR: Imported archive does not seem to contain valid data: $(ls -l $ARCHIVE_DIR | head -n 10)" && exit 98 |
| 75 | + echo "Imported data looks good: $(ls -l $ARCHIVE_DIR | head -n 5)" |
| 76 | + |
| 77 | + # if the archive was not imported from a dir, then clean the meta-data in the archive |
| 78 | + if [ ! -d "$IMPORT_MOUNT" ]; then |
| 79 | + nuodocker restore archive --origin-dir "$ARCHIVE_DIR" --restore-dir "$ARCHIVE_DIR" --db-name "$DB_NAME" --clean-metadata || exit 99 |
| 80 | + fi |
| 81 | +fi |
0 commit comments