#!/bin/bash # share fix # use this script to rebuild default configs for shares if the ._share becomes # broken for whatever reason. unset readynasd number x volume found iscsi shares found=0 shares=0 iscsi=0 share_create() { found=$((found+1)) shares=$((shares+1)) path="/$volume/._share/$item" echo -n "=== Found share at /$volume/$item... recreating share configs...". mkdir -p "$path" echo "share" > "$path"/datasettype.conf echo "0,0" > "$path"/recycle.conf echo "#dashboard:available=0 \"/$volume/$item\" 127.0.0.1(insecure,insecure_locks,no_subtree_check,crossmnt,anonuid=99,anongid=99,root_squash,ro,async)" > "$path"/nfs.conf echo "" > "$path"/snapdir.conf echo "1 0 0 * * * 0" > "$path"/snapshot.conf cat > "$path"/afp.conf << EOF [$item] available = 1 path = /$volume/$item admin group = admin EOF cat > "$path"/custom_snapshot_management.conf << EOF enabled = no retention_rule = lifetime lifetime = 2592000 number = 30 nonempty_snapshots = yes prev_versions = no EOF cat > "$path"/custom_snapshot_schedule.conf << EOF ........................ ........................ ........................ ........................ ........................ ........................ ........................ EOF cat > "$path"/fs.conf << EOF compression = 0 bitrotprotection = 0 autodefrag = 0 refquota = 0 EOF cat > "$path"/ftp.conf << EOF available 0 default_access disabled mask fmask dmask user_rw group_rw host_rw user_ro group_ro host_ro EOF cat > "$path"/samba.conf << EOF [$item] path = /$volume/$item comment = "" spotlight = 0 guest ok = 1 admin users = +admin writeable = 1 follow symlinks = 1 EOF cd "$path" chown root.root datasettype.conf recycle.conf snapdir.conf snapshot.conf chown admin.admin afp.conf fs.conf ftp.conf nfs.conf samba.conf custom_snapshot_management.conf custom_snapshot_schedule.conf chmod 644 datasettype.conf recycle.conf snapdir.conf snapshot.conf afp.conf fs.conf ftp.conf nfs.conf samba.conf custom_snapshot_management.conf custom_snapshot_schedule.conf echo " done with /$volume/$item." } check_existing_backups() { if [ -d "/var/backups/shares/" ]; then echo "NOTE: This version of readynasd creates share backups in /var/backups/shares." echo " When possible, you should try and restore a valid backup config from this directory." echo " This share config gets rebuilt every time readynasd restarts. Because of this, we'll make a backup of the existing share backups before proceeding." sleep 3 echo -n "Generating backup config..." && sleep 1 && tar -cf "/var/backups/backup_share_configs.$(date +%s).tar" /var/backups/shares 2>/dev/null && sleep 1 && echo "... done! Backup complete!" sleep 3 echo "=============================================" echo "====== Listing Available Share Backups ======" echo "=============================================" sleep 2 ls -lah /var/backups/shares/ sleep 1 echo "=============================================" echo "See the last modified dates and determine if you can restore from that config or not instead of using this script." echo "It is best to pick a date before the shares disappeared from readynasd." echo "=============================================" sleep 5 echo "If you want to cancel and manually restore from one of these configs, CTRL-C now..." echo -n "Otherwise, continuing in 10..."; sleep 1; echo -n " 9..."; sleep 1; echo -n " 8..."; sleep 1; echo -n " 7..."; sleep 1; echo -n " 6..."; sleep 1; echo -n " 5..."; sleep 1; echo -n " 4..."; sleep 1; echo -n " 3..."; sleep 1; echo -n " 2..."; sleep 1; echo -n " 1..."; sleep 1 ; echo " 0..."; sleep 1 fi } iscsi_create() { iscsi=$((iscsi+1)) found=$((found+1)) path="/$volume/._share/$item" echo -ne "=== Found LUN at /$volume/$item... recreating iSCSI configs...". mkdir -p "$path" cat > "$path"/iscsi.conf << EOF comment alert_threshold 80 EOF cat > "$path"/custom_snapshot_management.conf << EOF enabled = no retention_rule = lifetime lifetime = 2592000 number = 30 nonempty_snapshots = yes prev_versions = no EOF cat > "$path"/custom_snapshot_schedule.conf << EOF ........................ ........................ ........................ ........................ ........................ ........................ ........................ EOF echo "0 0 0 * 0 0 0" > "$path"/snapshot.conf echo " done with /$volume/$item." } backup_configs() { cd "/$volume/._share/" tar -cf "/var/backups/$volume_sharelunconfig.$(date +%s).tar" ./* 2>/dev/null } echo "===============================" echo "Share and iSCSI Rebuild v6.9.4" echo "===============================" check_existing_backups echo "Querying readynasd for list of volumes..." x=$(rn_nml -g volumes 2>/dev/null) # find if readynasd is running if [[ $? -eq 0 ]]; then # if readynasd was successful x=$(echo "$x"| grep "resource-id" | sed 's/^.*id="//;s/".*//;') # then parse it readynasd=1 elif [[ $? -eq 1 ]] ; then # but if it wasn't echo "readynasd not available; doing alternative lookup for shares" # inform intent x=$(find / -maxdepth 2 -name "._share" | sed 's/^\///g;s/\/.*//') # create new list fi number=$(echo "$x" | wc -w) if [[ ! $number ]]; then echo "No volumes found!" exit 1 else echo "$number volume(s) found." echo -n "Beginning in 3..."; sleep 1; echo -n " 2..."; sleep 1; echo " 1..."; sleep 1 echo "===============================" echo "=== All right! Time to go! ===" echo "===============================" for volume in $x; do echo "= Investigating volume /$volume..." objects=$(cd /"$volume"; ls -dA */ 2>/dev/null) if [[ $? == 2 ]]; then echo "No shares or LUNs found." else objects=$(echo "$objects" |egrep -v "^.apps\/$|^home\/$|^.purge\/$|^._share\/$|^.timemachine\/$|^.vault\/$|^.TemporaryItems\/$" | sed 's/\/$//') echo "= Objects found:" $objects backup_configs for item in $objects; do echo "== Reviewing $item" if ls -QdA /"$volume"/"$item"/.iscsi >/dev/null 2>&1; then iscsi_create else share_create fi done fi echo "= done with /$volume" done if [[ $found -gt 0 ]] && [[ $readynasd ]] ; then echo "*** Restarting readynasd to finish changes... ***" systemctl restart readynasd else echo "Not starting readynasd because it returned a bad message earlier; restart manually." fi echo "=================================================" echo "| Completed share creation! We were able to |" echo "| rebuild $shares shares and $iscsi LUNs. |" echo "=================================================" fi