I used this script for years on my VPS. And it\'s still working.
DBLIST=`mysql -uroot -pROOT_PASSWORD -ANe\"SELECT GROUP_CONCAT(schema_name) FROM information
I was using mysqldump
from the CLI and trying to pipe to gzip and/or a file and getting a "permission denied" error.
Even as sudo
, I was getting an error because although I was running mysqldump
as sudo
, the pipe was still trying to use the user account I was logged in to the shell as to write the output. In this case, my shell user account did not have permissions to write to the target directory.
To get around this, you can use the tee
command in conjunction with sudo
:
mysqldump --single-transaction --routines --events --triggers --add-drop-table --extended-insert -u backup -h 127.0.0.1 -p --all-databases | gzip -9 | sudo tee /var/backups/sql/all_$(date +"%Y_week_%U").sql.gz > /dev/null
The | sudo tee /var/backups/...
is what lets us pipe to a directory that is only writable by root
. The > /dev/null
suppresses tee
from dumping its output directly to the screen.