|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Based on: https://raw.githubusercontent.com/nextcloud-gmbh/mtime_fixer_tool_kit/master/solvable_files.sh |
| 4 | + |
| 5 | +true |
| 6 | +SCRIPT_NAME="Fix 'Could not update metadata due to invalid modified time'." |
| 7 | +# shellcheck source=lib.sh |
| 8 | +source /var/scripts/fetch_lib.sh |
| 9 | + |
| 10 | +# Get needed variables for database management |
| 11 | +ncdb |
| 12 | + |
| 13 | +# Check if root |
| 14 | +root_check |
| 15 | + |
| 16 | +#2023-05-04 Customized the original script to fit the Nextcloud VM users setup. Also fixed some shellcheck issues. |
| 17 | + |
| 18 | +data_dir="$(realpath "$NCDATA")" |
| 19 | +export data_dir |
| 20 | +export db_type=$NCDBTYPE |
| 21 | +export db_host=$NCDBHOST |
| 22 | +export db_user=$NCDBUSER |
| 23 | +export db_pwd=$NCDBPASS |
| 24 | +export db_name=$NCDB |
| 25 | +export action=fix |
| 26 | +export scan_action=noscan |
| 27 | +export use_birthday=use_birthday |
| 28 | +export verbose=verbose |
| 29 | + |
| 30 | +# In case you're using a different database table prefix, set this to your config's `dbtableprefix` value. |
| 31 | +export dbtableprefix="oc_" |
| 32 | + |
| 33 | +# 1. Return if fs mtime <= 86400 |
| 34 | +# 2. Compute username from filepath |
| 35 | +# 3. Query mtime from the database with the filename and the username |
| 36 | +# 4. Return if mtime_on_fs != mtime_in_db |
| 37 | +# 5. Correct the fs mtime with touch (optionally using the files change date/timestamp) |
| 38 | +correct_mtime() { |
| 39 | + filepath=$NCDATA |
| 40 | + |
| 41 | + if [ ! -e "$filepath" ] |
| 42 | + then |
| 43 | + echo "File or directory $filepath does not exist. Skipping." |
| 44 | + return |
| 45 | + fi |
| 46 | + |
| 47 | + relative_filepath="${filepath/#$data_dir\//}" |
| 48 | + mtime_on_fs="$(stat -c '%Y' "$filepath")" |
| 49 | + |
| 50 | + username=$relative_filepath |
| 51 | + while [ "$(dirname "$username")" != "." ] |
| 52 | + do |
| 53 | + username=$(dirname "$username") |
| 54 | + done |
| 55 | + |
| 56 | + relative_filepath_without_username="${relative_filepath/#$username\//}" |
| 57 | + |
| 58 | + base64_relative_filepath="$(printf '%s' "$relative_filepath" | base64)" |
| 59 | + base64_relative_filepath_without_username="$(printf '%s' "$relative_filepath_without_username" | base64)" |
| 60 | + |
| 61 | + if [ "$username" == "__groupfolders" ] |
| 62 | + then |
| 63 | + mtime_in_db=$(sudo -u postgres psql nextcloud_db --tuples-only --no-align -c "SELECT mtime FROM ${dbtableprefix}storages JOIN ${dbtableprefix}filecache ON ${dbtableprefix}storages.numeric_id = ${dbtableprefix}filecache.storage WHERE ${dbtableprefix}storages.id='local::$data_dir/' AND ${dbtableprefix}filecache.path=CONVERT_FROM(DECODE('$base64_relative_filepath', 'base64'), 'UTF-8')") |
| 64 | + else |
| 65 | + mtime_in_db=$(sudo -u postgres psql nextcloud_db --tuples-only --no-align -c "SELECT mtime FROM ${dbtableprefix}storages JOIN ${dbtableprefix}filecache ON ${dbtableprefix}storages.numeric_id = ${dbtableprefix}filecache.storage WHERE ${dbtableprefix}storages.id='home::$username' AND ${dbtableprefix}filecache.path=CONVERT_FROM(DECODE('$base64_relative_filepath_without_username', 'base64'), 'UTF-8')") |
| 66 | + fi |
| 67 | + |
| 68 | + if [ "$mtime_in_db" == "" ] |
| 69 | + then |
| 70 | + echo "No mtime in database. File not indexed. Skipping $filepath" |
| 71 | + return |
| 72 | + fi |
| 73 | + |
| 74 | + if [ "$mtime_in_db" != "$mtime_on_fs" ] |
| 75 | + then |
| 76 | + echo "mtime in database do not match fs mtime (fs: $mtime_on_fs, db: $mtime_in_db). Skipping $filepath" |
| 77 | + return |
| 78 | + fi |
| 79 | + |
| 80 | + if [ -e "$filepath" ] |
| 81 | + then |
| 82 | + newdate=$(stat -c "%w" "$filepath") |
| 83 | + if [ "$newdate" == "-" ] |
| 84 | + then |
| 85 | + newdate=$(stat -c "%z" "$filepath") |
| 86 | + touch -c -d "$newdate" "$filepath" |
| 87 | + else |
| 88 | + touch -c "$filepath" |
| 89 | + fi |
| 90 | + echo mtime for "$filepath" updated to "$(stat -c "%y" "$filepath")" |
| 91 | + elif [ ! -e "$filepath" ] |
| 92 | + then |
| 93 | + echo "File or directory $filepath does not exist. Skipping." |
| 94 | + return |
| 95 | + fi |
| 96 | +} |
| 97 | +export -f correct_mtime |
| 98 | + |
| 99 | +find "$data_dir" -type f ! -newermt "@86400" -exec bash -c 'correct_mtime "$0"' {} \; |
0 commit comments