Newer versions of Moodle now provide the ability to put a site into maintenance mode through the CLI:
This can be found under the standard admin CLI location
php admin/cli/maintenance.php --enable php admin/cli/maintenance.php --disable
NOTE this disables all web access to the site and can only be restored by disabling maintenance mode through the CLI again. This is exactly what you want if you need to take a proper site backup. The message displayed will be the maintenance mode message set up under Site Administration > Server > Maintenance Mode > Optional maintenance message (maintenance_message).
There is also a cool feature to enable it later e.g.
php admin/cli/maintenance.php --enablelater=60
Which would enable it after 60 minutes. This will tell exactly when maintenance mode will be enabled too. Good for making sure the site is in maintenance mode when you are ready to start an upgrade for example.
Finally there is an option called “enableold”
php admin/cli/maintenance.php --enableold
This is meant to do the same thing as the user interface Site Administration > Server > Maintenance Mode > Maintenance mode setting. That is site admins still have access to the site but the site is otherwise in maintenance mode for everyone else. This works in Moodle 3.2 but I’ve had problems with it in prior versions of moodle. It could possibly be around caching and might be important to also clear the moodle cache at the same time.