iT邦幫忙

2022 iThome 鐵人賽

DAY 20
0
IT管理

用UNRAID打造AIO全生態系列 第 20

Script:File Auto Balance

  • 分享至 

  • xImage
  •  

前面有談到,UNRAID陣列設定成功後,檔案位置並不會直接反應在新的硬碟
而是需透過unBALANCE Plugin,將硬碟手動調整成正確的設定
但誰想每次都去調整? 必須實現網管自動化
最好能利用自動化腳本來解決,才能真正一勞永逸


因此就有今天的腳本
以後不管怎麼調整,都能在執行此腳本後
自動實現檔案"Balance"
這樣UNRAID Array+Cache的機制就徹底完美了!

使用方法
總共兩腳本,可將兩腳本cron排程安排相同時間
建議頻率Daily(每天)即可

#!/usr/bin/php
<?PHP
function getRsyncReturnValue($returnValue) {
  $returnMessage[0] = "Success";
  $returnMessage[1] = "Syntax or usage error";
  $returnMessage[2] = "Protocol incompatibility";
  $returnMessage[3] = "Errors selecting input/output files, dirs";
  $returnMessage[4] = "Requested action not supported: an attempt was made to manipulate 64-bit files on a platform that cannot support them; or an option was specified that is supported by the client and not by the server.";
  $returnMessage[5] = "Error starting client-server protocol";
  $returnMessage[6] = "Daemon unable to append to log-file";
  $returnMessage[10] = "Error in socket I/O";
  $returnMessage[11] = "Error in file I/O";
  $returnMessage[12] = "Error in rsync protocol data stream";
  $returnMessage[13] = "Errors with program diagnostics";
  $returnMessage[14] = "Error in IPC code";
  $returnMessage[20] = "Received SIGUSR1 or SIGINT";
  $returnMessage[21] = "Some error returned by waitpid()";
  $returnMessage[22] = "Error allocating core memory buffers";
  $returnMessage[23] = "Partial transfer due to error";
  $returnMessage[24] = "Partial transfer due to vanished source files";
  $returnMessage[25] = "The --max-delete limit stopped deletions";
  $returnMessage[30] = "Timeout in data send/receive";
  $returnMessage[35] = "Timeout waiting for daemon connection";
  
  $return = $returnMessage[$returnValue];
  if ( ! $return ) {
    $return = "Unknown Error";
  }
  return $return;
}


$shareList = @array_diff(@scandir("/boot/config/shares"),array(".",".."));

if ( ! is_dir("/mnt/user") ) {
  echo "Array Must Be Started And User Shares Must Be Enabled To Utilize This Script.  Exiting.\n";
  return;
}

if ( ! is_dir("/mnt/cache") ) {
  echo "Cache Drive Not Installed.  Exiting\n";
  return;
}

if ( ! $shareList ) {
  $shareList = array();
}

foreach ( $shareList as $shareCfg ) {
  $config = @parse_ini_file("/boot/config/shares/$shareCfg");
  if ( ! $config ) {
    continue;
  }
  if ( $config['shareUseCache'] != "no" ) {
    continue;
  }
  $share = pathinfo($shareCfg,PATHINFO_FILENAME);
  if ( is_dir("/mnt/cache/$share") ) {
    echo "Share $share set to be array-only, but files exist on the cache drive.  Moving the files.  Depending upon the size and number of files, this may take a bit\n";
    $source = escapeshellarg("/mnt/cache/$share/");
    $destination = escapeshellarg("/mnt/user0/$share");
    $flag = true;
    exec("rsync -avXHq --remove-source-files $source $destination",$output,$returnValue);
    if ( $returnValue ) {
      echo "Errors Occurred: ".getRsyncReturnValue($returnValue)."\n";
      $errors = true;
    } else {
      exec("rm -rf $source");
    }
  }
}
if ( ! $flag ) {
  echo "All cache-only shares already only existed on the cache drive\n";
} else {
  if ( ! $errors ) {
    echo "All shares set to be cache-only have now had their files previously existing on the array to now be on the cache drive\n";
  } else {
    echo "Errors Occurred In The Copying\n";
  }
}
?>
#!/usr/bin/php
<?PHP
function getRsyncReturnValue($returnValue) {
  $returnMessage[0] = "Success";
  $returnMessage[1] = "Syntax or usage error";
  $returnMessage[2] = "Protocol incompatibility";
  $returnMessage[3] = "Errors selecting input/output files, dirs";
  $returnMessage[4] = "Requested action not supported: an attempt was made to manipulate 64-bit files on a platform that cannot support them; or an option was specified that is supported by the client and not by the server.";
  $returnMessage[5] = "Error starting client-server protocol";
  $returnMessage[6] = "Daemon unable to append to log-file";
  $returnMessage[10] = "Error in socket I/O";
  $returnMessage[11] = "Error in file I/O";
  $returnMessage[12] = "Error in rsync protocol data stream";
  $returnMessage[13] = "Errors with program diagnostics";
  $returnMessage[14] = "Error in IPC code";
  $returnMessage[20] = "Received SIGUSR1 or SIGINT";
  $returnMessage[21] = "Some error returned by waitpid()";
  $returnMessage[22] = "Error allocating core memory buffers";
  $returnMessage[23] = "Partial transfer due to error";
  $returnMessage[24] = "Partial transfer due to vanished source files";
  $returnMessage[25] = "The --max-delete limit stopped deletions";
  $returnMessage[30] = "Timeout in data send/receive";
  $returnMessage[35] = "Timeout waiting for daemon connection";
  
  $return = $returnMessage[$returnValue];
  if ( ! $return ) {
    $return = "Unknown Error";
  }
  return $return;
}


$shareList = @array_diff(@scandir("/boot/config/shares"),array(".",".."));

if ( ! is_dir("/mnt/user") ) {
  echo "Array Must Be Started And User Shares Must Be Enabled To Utilize This Script.  Exiting.\n";
  return;
}

if ( ! is_dir("/mnt/cache") ) {
  echo "Cache Drive Not Installed.  Exiting\n";
  return;
}

if ( ! $shareList ) {
  $shareList = array();
}

foreach ( $shareList as $shareCfg ) {
  $config = @parse_ini_file("/boot/config/shares/$shareCfg");
  if ( ! $config ) {
    continue;
  }
  if ( $config['shareUseCache'] != "only" ) {
    continue;
  }
  $share = pathinfo($shareCfg,PATHINFO_FILENAME);
  if ( is_dir("/mnt/user0/$share") ) {
    echo "Share $share set to be cache-only, but files exist within array.  Moving the files.  Depending upon the size and number of files, this may take a bit\n";
    $source = escapeshellarg("/mnt/user0/$share/");
    $destination = escapeshellarg("/mnt/cache/$share");
    $flag = true;
    exec("rsync -avXHq --remove-source-files $source $destination",$output,$returnValue);
    if ( $returnValue ) {
      echo "Errors Occurred: ".getRsyncReturnValue($returnValue)."\n";
      $errors = true;
    } else {
      exec("rm -rf $source");
    }
  }
}
if ( ! $flag ) {
  echo "All cache-only shares already only existed on the cache drive\n";
} else {
  if ( ! $errors ) {
    echo "All shares set to be cache-only have now had their files previously existing on the array to now be on the cache drive\n";
  } else {
    echo "Errors Occurred In The Copying\n";
  }
}
?>

上一篇
Script:Auto Backup flash
下一篇
Script:BTRFS Scrub
系列文
用UNRAID打造AIO全生態31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言