1. Backup of old moodle
Take the backup of your existing moodle directory, moodle filefolder and database. Either you can make use a database kept in your pen drive otherwise you can apply following command to take the backup,
First install the zip command using apt command or apt-get command. Open the terminal and type the following command:
Archive the current moodle and moodledata directories
cd /var/www/html
sudo zip -r moodlerecovery.zip moodle moodledata
sudo mv moodlerecovery.zip /home/mahesh/Downloads
sudo su
mysqldump -uroot -p moodle | xz > moodle.sql.xz
Provide MySQL root password, when it asks.
2. Install latest Moodle
Follow the instructions in Moodle document with a MySQL/MariaDB user and password for moodle database. then remove new moodledata folder
sudo rm -rf /var/www/html/moodledata
3. Restoration of old Moodle database to a new one
Copy your database backup from your pen drive to home folder. Extract moodlerecovery.zip and the backup file (moodle.sql.xz), backup the extension will be .sql. Then move old moodledata to new web document root and restore the backup to the new Moodle installation.
sudo mv /moodlerecovery/moodledata /var/www/html/
sudo chmod 777 -R /var/www/html/moodledata
sudo cp moodlerecovery/moodle/config.php /var/www/html/moodle
sudo cp -pr moodlerecovery/moodle/theme/mytheme
/var/www/html/moodle/theme/mytheme
sudo cp -pr moodlerecovery/moodle/mod/mymod /var/www/html/moodle/mod/mymod
sudo chown -R root:root /var/www/html/moodle
sudo chmod 775 -R /var/www/html/moodle
Proceed with onsite wizad and can upgrade database etc..
Reference: https://docs.moodle.org/39/en/Upgrading#Install_the_new_Moodle_software