by Ken Task.
Congrats! You are on your way to becoming a CLI Ninja!
When including what is a shell (operating system) variable, like the date/time stamp, one has to use the options provided - usually best not to be 'inventive' with things like an 'underscore' cause an 'underscore' isn't defined in the options for the variable - unless you want to recompile the kernel/command ... hard core programming then.
"I think not to mention having a password stored in plain text in the system must be insecure, it must be better not to apply the password in the script?"
Security experts would frown on including passwords in scripts, yes. And the warning is valid. Let's remember you have a single Moodle instance so you could strip out the password in the script. I have multiple Moodles on one server .. many times a 'stealthy' dev clone of production. I'd have to remember unique passwords for X number of instances.
Besides that ... the script cannot be run from Web ... only way to run it is via ssh. So a cracker would have to gain ssh access to run it. If they can do that, then you are in more trouble than you know so what diff would it really make. If they gained root access they can change the password anyway. Most crackers now a days are not after defacing your site nor leaving clues that they've been into your box, but they do want to use resources of your server as part of a bot network.
Yes, could extend the script capabilities ... now you are thinking! ;)
With Linuxes there might be a memory limitation using the cp -pr command + doing that on moodledata could take a very long time AND eat up space on the partition of the drive where you are storing the backups.
IF there is enough room on the HD, one could do full tar ball of moodledata, but think I'd use rsync to clone only what is needed from moodledata to re-coup from a serious loss and I'd rsync ONLY the moodledata/filedir directory. First run of rsync takes time but subsequent runs if using the right 'swtches' to the command deletes things no longer present in adtive moodledata/filedir and updates only files/diretories that are new - thus taking a lot less time to complete.
Ya didn't think I'd feed you fish all the time, did ya? ;) man rsync and man tar is your online manual friend ... so is a google search
For the code directory, I'll let you dive into the usage of the 'tar' command ... just save to the same directory where you are doing the sql dump to have all things in one place.
Feel free to say 'Thanks' at any time by rating postings useful. ;)
'spirit of sharing', Ken