ownCloud/Nextcloud OCC命令行工具详解(1)

Posted by 橙叶 on Tue, Jan 24, 2017
occ命令是ownCloud/Nextcloud的命令行界面。您可以使用occ执行许多常见的服务器操作,例如安装和升级ownCloud/Nextcloud,管理用户,加密,密码,LDAP设置等。

OCC命令行工具详解

[ssbluelist] [/ssbluelist]

使用OCC命令

OCC文件是使用PHP编写的,首先你要找到OCC文件在哪里

它在ownCloud/Nextcloud根目录里,比如/var/www/html/owncloud/OCC,请确认OCC文件的用户和组均为网页服务器用户,且用户和组权限可读可执行

Ubuntu/Debian用户可以直接在ownCloud/Nextcloud根目录下执行:

sudo -u www-data php occ #以www-data用户身份执行OCC文件

会输出以下内容(以Nextcloud为例):

Nextcloud version 11.0.1

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --no-warnings     Skip global warnings, show command output only
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  _completion                         BASH completion hook.
  check                               check dependencies of the server environment
  help                                Displays help for a command
  list                                Lists commands
  status                              show some status information
  upgrade                             run upgrade routines after installation of a new release. The release has to be installed before.
 app
  app:check-code                      check code to be compliant
  app:disable                         disable an app
  app:enable                          enable an app
  app:getpath                         Get an absolute path to the app directory
  app:list                            List all available apps
 background
  background:ajax                     Use ajax to run background jobs
  background:cron                     Use cron to run background jobs
  background:webcron                  Use webcron to run background jobs
 config
  config:app:delete                   Delete an app config value
  config:app:get                      Get an app config value
  config:app:set                      Set an app config value
  config:import                       Import a list of configs
  config:list                         List all configs
  config:system:delete                Delete a system config value
  config:system:get                   Get a system config value
  config:system:set                   Set a system config value
 dav
  dav:create-addressbook              Create a dav addressbook
  dav:create-calendar                 Create a dav calendar
  dav:sync-birthday-calendar          Synchronizes the birthday calendar
  dav:sync-system-addressbook         Synchronizes users to the system addressbook
 db
  db:convert-type                     Convert the Nextcloud database to the newly configured one
  db:generate-change-script           generates the change script from the current connected db to db_structure.xml
 encryption
  encryption:change-key-storage-root  Change key storage root
  encryption:decrypt-all              Disable server-side encryption and decrypt all files
  encryption:disable                  Disable encryption
  encryption:enable                   Enable encryption
  encryption:encrypt-all              Encrypt all files for all users
  encryption:list-modules             List all available encryption modules
  encryption:set-default-module       Set the encryption default module
  encryption:show-key-storage-root    Show current key storage root
  encryption:status                   Lists the current status of encryption
 federation
  federation:sync-addressbooks        Synchronizes addressbooks of all federated clouds
 files
  files:cleanup                       cleanup filecache
  files:scan                          rescan filesystem
  files:transfer-ownership            All files and folders are moved to another user - shares are moved as well.
 files_external
  files_external:applicable           Manage applicable users and groups for a mount
  files_external:backends             Show available authentication and storage backends
  files_external:config               Manage backend configuration for a mount
  files_external:create               Create a new mount configuration
  files_external:delete               Delete an external mount
  files_external:export               Export mount configurations
  files_external:import               Import mount configurations
  files_external:list                 List configured admin or personal mounts
  files_external:notify               Listen for active update notifications for a configured external mount
  files_external:option               Manage mount options for a mount
  files_external:verify               Verify mount configuration
 group
  group:adduser                       add a user to a group
  group:list                          list configured groups
  group:removeuser                    remove a user from a group
 integrity
  integrity:check-app                 Check integrity of an app using a signature.
  integrity:check-core                Check integrity of core code using a signature.
  integrity:sign-app                  Signs an app using a private key.
  integrity:sign-core                 Sign core using a private key.
 l10n
  l10n:createjs                       Create javascript translation files for a given app
 log
  log:file                            manipulate logging backend
  log:manage                          manage logging configuration
 maintenance
  maintenance:data-fingerprint        update the systems data-fingerprint after a backup is restored
  maintenance:mimetype:update-db      Update database mimetypes and update filecache
  maintenance:mimetype:update-js      Update mimetypelist.js
  maintenance:mode                    set maintenance mode
  maintenance:repair                  repair this installation
  maintenance:singleuser              set single user mode
  maintenance:update:htaccess         Updates the .htaccess file
 security
  security:certificates               list trusted certificates
  security:certificates:import        import trusted certificate
  security:certificates:remove        remove trusted certificate
 trashbin
  trashbin:cleanup                    Remove deleted files
  trashbin:expire                     Expires the users trashbin
 twofactorauth
  twofactorauth:disable               Disable two-factor authentication for a user
  twofactorauth:enable                Enable two-factor authentication for a user
 user
  user:add                            adds a user
  user:delete                         deletes the specified user
  user:disable                        disables the specified user
  user:enable                         enables the specified user
  user:info                           show user info
  user:lastseen                       shows when the user was logged in last time
  user:list                           list configured users
  user:report                         shows how many users have access
  user:resetpassword                  Resets the password of the named user
  user:setting                        Read and modify user settings
 versions
  versions:cleanup                    Delete versions
  versions:expire                     Expires the users file versions

在CentOS中,命令是这样的:

sudo -u apache php occ

区别就是Ubuntu和CentOS的http服务器用户不同

使用它!

1,遇到一个新命令行工具,我们首先要help一下。
sudo -u www-data php occ -h
输出如下
Usage:
  help [options] [--] [<command_name>] #格式:help [选项] [--] [命令名]

Arguments: command The command to execute command_name The command name [default: “help”] #命令名(对应使用格式中的“命令名”)

Options: #选项 –format=FORMAT The output format (txt, xml, json, or md) [default: “txt”] #输出格式,默认为txt,下文中有讲 –raw To output raw command help #输出原始命令帮助 -h, –help Display this help message #显示帮助信息 -q, –quiet Do not output any message #不输出任何信息 -V, –version Display this application version #显示程序版本 –ansi Force ANSI output #强制使用ANSI格式输出 –no-ansi Disable ANSI output #禁用ANSI输出 -n, –no-interaction Do not ask any interactive question #不要询问任何问题(所有选项默认,大概这个意思) –no-warnings Skip global warnings, show command output only #跳过错误信息 -v|vv|vvv, –verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

增加消息的详细程度:1用于正常输出,2用于更详细的输出,3用于调试

Help: The help command displays help for a given command: #详细地,查看一个命令的使用方法,例如:

php occ help list

You can also output the help in other formats by using the –format option:

php occ help –format=xml list

To display the list of available commands, please use the list command.

使用-h选项获取一个命令的详细信息,比如我要查询maintenance:mode(维护模式)命令的使用方式

输入

sudo -u www-data php occ help maintenance:mode
Usage:
 maintenance:mode [options] #格式: maintenance:mode+选项(即maintenance:mode后面跟选项)

Options: #选项(对应上面格式中的“选项”)
     --on              enable maintenance mode #激活维护模式
     --off             disable maintenance mode #关闭维护模式
 -h, --help            Display this help message #显示帮助信息,即本段信息
 -q, --quiet           Do not output any message #不输出任何信息
 -V, --version         Display this application version 
     --ansi            Force ANSI output
     --no-ansi         Disable ANSI output
 -n, --no-interaction  Do not ask any interactive question
     --no-warnings     Skip global warnings, show command output only
 -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output,
                       2 for more verbose output and 3 for debug

2.显示ownCloud/Nextcloud版本

以Ubuntu、Nextcloud为例

sudo -u www-data php occ -V #选项为-V,注意大写
  Nextcloud version 9.0.0 #Nextcloud版本为 9.0.0

3.查询您的Nextcloud服务器状态

sudo -u www-data php occ status
  - installed: true #安装状态:已安装
  - version: 9.0.0.19 #版本:9.0.0.19
  - versionstring: 9.0.0 #版本串:9.0.0
  - edition: #发行版本,社区版不显示,企业版可能会显示

4.命令输出格式,可以输出txt, xml, json或 md格式的信息

比如以json格式输出服务器状态信息:

sudo -u www-data php occ status --output=json

{"installed":true,"version":"9.0.0.19","versionstring":"9.0.0","edition":""}

或者以json_pretty格式输出:

sudo -u www-data php occ status --output=json_pretty
{
   "installed": true,
   "version": "9.0.0.19",
   "versionstring": "9.0.0",
   "edition": ""
}

(这样,OCC命令可以作为一个API来使用)

5.命令自动补全

Nextcloud11后,OCC命令可以使用自动补全命令,你需要以下命令来设置自动补全

# BASH ~4.x, ZSH
source <(/var/www/html/nextcloud/occ _completion --generate-hook)

# BASH ~3.x, ZSH
/var/www/html/nextcloud/occ _completion --generate-hook | source /dev/stdin

# BASH (any version)
eval $(/var/www/html/nextcloud/occ _completion --generate-hook)

设置后,只有提供OCC文件完整目录(比如/var/www/html/nextcloud/occ <tab>)才能使用自动补全。在–generate-hook之后指定–programm occ,便可无需在写完整目录。

这样,当你使用OCC命令行工具时,按下Tab键便可自动补全剩余的命令内容。

例如

输入
sudo -u www-data php occ sta
按Tab键,命令自动补全为:
sudo -u www-data php occ status

插件(APP)管理命令

使用以下命令列出、激活禁用插件
app
app:check-code check code to be compliant #检查代码是否合法
app:disable disable an app #禁用一个插件(app)
app:enable enable an app #激活一个插件
app:getpath Get an absolute path to the app directory #获取应用程序目录的绝对路径
app:list List all available apps #列出所有可用的插件
命令示例:
sudo -u www-data php occ app:list
Enabled:
  - activity: 2.4.1
  - comments: 1.1.0
  - dav: 1.1.1
  - direct_menu: 0.10.0
  - federatedfilesharing: 1.1.1
  - federation: 1.1.1
  - files: 1.6.1
  - files_external: 1.1.2
  - files_markdown: 1.0.0
  - files_pdfviewer: 1.0.1
  - files_sharing: 1.1.1
  - files_texteditor: 2.2
  - files_trashbin: 1.1.0
  - files_versions: 1.4.0
  - files_videoplayer: 1.0.0
  - firstrunwizard: 2.0
  - gallery: 16.0.0
  - logreader: 2.0.0
  - lookup_server_connector: 1.0.0
  - nextcloud_announcements: 1.0
  - notifications: 1.0.1
  - password_policy: 1.1.0
  - provisioning_api: 1.1.0
  - serverinfo: 1.1.1
  - sharebymail: 1.0.1
  - survey_client: 0.1.5
  - systemtags: 1.1.3
  - theming: 1.1.1
  - twofactor_backupcodes: 1.0.0
  - updatenotification: 1.1.1
  - user_saml: 1.2.2
  - workflowengine: 1.1.1
Disabled:
  - admin_audit
  - encryption
  - external
  - files_accesscontrol
  - files_automatedtagging
  - files_retention
  - spreed
  - spreedme
  - templateeditor
  - user_external
  - user_ldap
1.激活一个插件:
sudo -u www-data php occ app:enable files_automatedtagging #激活此插件

files_automatedtagging enabled

禁用插件的方法与此类似

2.app:check-code检查多个项目:它检查应用程序是否使用Nextcloud的公共API(OCP)或私有API(OC_),它还检查已弃用的方法和info.xml文件的有效性。默认情况下启用所有检查。活动应用程式是正确格式的应用程式范例:

sudo -u www-data php occ app:check-code notifications
App is compliant - awesome job

如果插件有错误:

sudo -u www-data php occ app:check-code foo_app
Analysing /var/www/nextcloud/apps/files/foo_app.php
4 errors
   line   45: OCP\Response - Static method of deprecated class must not be
   called
   line   46: OCP\Response - Static method of deprecated class must not be
   called
   line   47: OCP\Response - Static method of deprecated class must not be
   called
   line   49: OC_Util - Static method of private class must not be called

3.获取插件的绝对目录

sudo -u www-data php occ app:getpath notifications
/var/www/nextcloud/apps/notifications

选择后台任务执行方式

使用background命令选择要用于控制后台作业(Ajax,Webcron或Cron)的调度程序。这与在您的Nextcloud Admin页面上使用Cron部分相同:
background
 background:ajax       Use ajax to run background jobs
 background:cron       Use cron to run background jobs
 background:webcron    Use webcron to run background jobs
例如,选择ajax为后台任务执行方式
sudo -u www-data php occ background:ajax
  Set mode for background jobs to 'ajax'
其它的:
  • background:cron
  • background:webcron

配置命令

使用config命令来配置服务器:
config
 config:app:delete      Delete an app config value
 config:app:get         Get an app config value
 config:app:set         Set an app config value
 config:import          Import a list of configs
 config:list            List all configs
 config:system:delete   Delete a system config value
 config:system:get      Get a system config value
 config:system:set      Set a system config value
列出所有设置值(即配置信息):
sudo -u www-data php occ config:list
列出的内容中隐藏了诸如用户名、密码等信息,可以后加--private参数来显示:
sudo -u www-data php occ config:list --private
将配置信息导出为指定格式:
sudo -u www-data php occ config:list --private
P.S.:虽然config命令也可以设置插件的状态,但最好还是用app:命令实现。
获取单个配置信息:
sudo -u www-data php occ config:system:get version #获取版本信息
9.0.0.19

sudo -u www-data php occ config:app:get activity installed_version #获取一个插件的版本信息 2.2.1

设置单个配置信息:

sudo -u www-data php occ config:system:set logtimezone --value="Europe/Berlin" #设置时区
System config value logtimezone set to Europe/Berlin

sudo -u www-data php occ config:app:set files_sharing #设置插件状态
incoming_server2server_share_enabled --value="yes" --type=boolean
Config value incoming_server2server_share_enabled for app files_sharing set to yes

如果配置信息不存在,在使用config:system:set命令时会自动创建不存在的值。如果你不想它被创建,在后面加上–update-only

sudo -u www-data php occ config:system:set doesnotexist --value="true"
--type=boolean --update-only
Value not updated, as it has not been set before.

请注意,为了将布尔值,浮点值或整数值写入配置文件,需要在命令中指定类型。这仅适用于config:system:set命令。以下值是已知的:

  • boolean
  • integer
  • float
  • string (默认)
例如,如果你想禁用维护模式
sudo -u www-data php occ config:system:set maintenance --value=false
--type=boolean
Nextcloud is in maintenance mode - no app have been loaded
System config value maintenance set to boolean false

设置不同数组的信息

呃,可能你没理解这是什么意思,以信任域(trust_domains)为例,它在config.php中是这样写的

如图,“0”对应第一个信任域“192.168.3.4”,“1”对应第二个信任域“cloud.nosu.win”

那我们可以在设置信任域中直接指定要修改第几个:

sudo -u www-data php occ config:system:set trusted_domains 1
--value=example.com #指定设置“1”对应的信任域为“example.com”
System config value trusted_domains => 2 set to string example.com

sudo -u www-data php occ config:system:get trusted_domains
localhost
nextcloud.local
example.com

在config.php中体现为:

删除单个设置值

sudo -u www-data php occ config:system:delete maintenance:mode #删除maintenance:mode的设置值
System config value maintenance:mode deleted

sudo -u www-data php occ config:app:delete appname provisioning_api Config value provisioning_api of app appname deleted

如果设置项本来就不存在,将不会有任何输出,你可以在后面加上–error-if-not-exists来显示错误:

sudo -u www-data php occ config:system:delete doesnotexist
--error-if-not-exists
Config provisioning_api of app appname could not be deleted because it did not
exist

 

 



comments powered by Disqus