#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: http://mirrors.slickstack.io/bash/ss-dump-database.txt ################################ #### path: /var/www/ss-dump-database ############################################################### #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Dumps the live MySQL database to /var/www/meta/ (SFTP user can access it) ############ #### module version: MySQL 8.0.x ################################################################### #### sourced by: ss core cron jobs, ss core bash scripts ########################################### #### bash aliases: ss dump, ss dump database, ss dump db ########################################### #################################################################################################### ## KEEP IN MIND THAT SS-DUMP MIGHT ONLY FUNCTION PROPERLY ON LOCALHOST DATABASES ## ## REMOTE MYSQL AND SERVER SETTINGS MIGHT PREVENT REMOTE DATABASE DUMPING ## ## include SlickStack configuration ## source /var/www/ss-config ## include SlickStack functions ## source /var/www/ss-functions #################################################################################################### #### SS-Dump-Database: Message (Begin Script) ###################################################### #################################################################################################### ## this is a simple message that announces to the shell the purpose of this bash script ## ## it will only be noticed by sudo users who manually call ss core bash scripts ## ## echo message ## echo -e "${PURPLE}Running ss-dump-database: Dumps the live MySQL database to /var/www/meta/ (SFTP user can access it)... ${NOCOLOR}" sleep "$SLEEP_MESSAGE_BEGIN" #################################################################################################### #### SS-Dump-Database: Export The Live MySQL Database (mysqldump) ################################## #################################################################################################### ## here we use mysqldump to dump a copy of the live production database to /var/www/meta ## ## to ensure support for remote databases we force the TCP protocol to be used ## ## dump production MySQL database (overwrites existing) ## # if [[ "$SS_DUMP_MYSQL_DB" != "false" ]]; then # mysqldump --databases $DB_NAME > /var/www/meta/db.sql # fi if [[ "$SS_DUMP_MYSQL_DB" != "false" ]] && [[ "$SS_APP" == "wordpress" || -z "$SS_APP" ]]; then mysqldump --databases wordpress > /var/www/meta/wp.sql ## change name to wordpress later fi ## DO NOT ENABLE THIS IN SS-CONFIG UNLESS YOUR SERVER HAS A TON OF EXTRA DISK SPACE ## ## (also in future should stop mysql-server before ss-dump to avoid corrupted mysql data) ## if [[ "$SS_DUMP_MYSQL_FILES" == "true" ]]; then cp /var/lib/mysql /var/www/meta/mysql.bak # cp /var/lib/mysql-files /var/www/meta/mysql-files.bak # cp /var/lib/mysql-keyring /var/www/meta/mysql-keyring.bak fi #################################################################################################### #### SS-Dump-Database: Reset Permissions (Allows SFTP User To Access The SQL Dump File) ############ #################################################################################################### ## here we do a quick permissions reset to ensure SFTP users can access the dump file ## ## keep in mind that root/sudo users will have access to this file regardless ## ## reset permissions ## chown -R $SFTP_USER:www-data /var/www/meta/wp.sql chmod 0775 /var/www/meta/wp.sql #################################################################################################### #### SS-Dump-Database: SUCCESS (OR FAIL) MESSAGE ################################################### #################################################################################################### ## FUTURE: if = file exists + recent touch time + file contents not zero ## echo -e "${YELLOW}The MySQL database was successfully dumped to: /var/www/meta/wp.sql${NORMAL}" >&2 #################################################################################################### #### SS-Dump-Database: Touch Timestamp File (End Script) ########################################### #################################################################################################### ## this is a dummy timestamp file that will remember the last time this script was run ## ## it can be useful for developer reference and is sometimes used by SlickStack ## ## script timestamp ## touch /var/www/meta/.timestamp-ss-dump-database #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## Ref: https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html ## Ref: https://unix.stackexchange.com/questions/293966/suppress-warning-messages-from-mysql-in-shell-script-but-allow-errors ## Ref: https://github.com/cytopia/devilbox/issues/212 ## Ref: https://stackoverflow.com/questions/32133353/unable-to-connect-to-mysql-database-in-ubuntu ## Ref: https://dba.stackexchange.com/questions/35847/mysqldump-flush-privileges-option ## Ref: https://www.sqlshack.com/how-to-install-mysql-on-ubuntu-18-04/ ## Ref: https://stackoverflow.com/questions/2989724/how-to-mysqldump-remote-db-from-local-machine ## Ref: https://dev.mysql.com/doc/refman/8.0/en/transport-protocols.html ## Ref: https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html ## Ref: https://snapshooter.io/learn/mysqldump-ultimate-guide ## Ref: https://www.interserver.net/tips/kb/ultimate-guide-mysqldump-database-backup-program/ ## Ref: https://serverfault.com/questions/547438/mysqldump-has-a-quick-option-why-isnt-this-enabled-by-default ## Ref: https://bugs.mysql.com/bug.php?id=100219 ## Ref: https://serversforhackers.com/c/mysqldump-with-modern-mysql ## Ref: https://dan.langille.org/2020/07/21/mysqldump-error-access-denied-you-need-at-least-one-of-the-process-privileges-for-this-operation-when-trying-to-dump-tablespaces/ ## Ref: https://forums.cpanel.net/threads/cpanel-33473-mysql-dump-process-privilege-error-after-5-7-31-update.675657/ ## Ref: https://dba.stackexchange.com/questions/271981/access-denied-you-need-at-least-one-of-the-process-privileges-for-this-ope ## Ref: https://stackoverflow.com/questions/37805316/what-is-a-tablespace-and-why-is-it-used ## Ref: https://stackoverflow.com/questions/2482491/is-copying-var-lib-mysql-a-good-alterntive-to-mysqldump ## Ref: https://serverfault.com/questions/659048/can-i-copy-the-entire-var-lib-mysql-folder-to-a-different-server-mysql-vs-mar/659049 ## Ref: https://serverfault.com/questions/81241/making-backup-by-copying-mysql-data-folder-is-ok-as-long-as-both-source-and-tar ## SS_EOF