Last active
April 17, 2024 15:59
-
-
Save saderi/aa1acd2c9021190592c1fd61afbdd45d to your computer and use it in GitHub Desktop.
Backup all databases in vestacp server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Creating a backup user, we will create a backup user with the minimum permissions necessary to run mysqldump | |
# CREATE USER 'backupuser'@'localhost' IDENTIFIED BY 'p455w0rd'; | |
# GRANT SELECT, SHOW VIEW, LOCK TABLES, RELOAD, REPLICATION CLIENT ON *.* TO 'backupuser'@'localhost'; | |
# FLUSH PRIVILEGES; | |
DB_USERNAME=backupuser | |
DB_PASSWORD=p455w0rd | |
REMOTE_STORAGE_URL= | |
REMOTE_STORAGE_USERNAME= | |
REMOTE_STORAGE_PATH= | |
RIGHTNOW=$(date +"%Y-%m-%d"); | |
BACKUP_PATH=${RIGHTNOW} | |
mkdir -p $BACKUP_PATH | |
DATABASE_LIST=$(ls -l /var/lib/mysql | grep drwx | awk {'print $NF'} | grep -v 'admin_default\|roundcube\|phpmyadmin\|performance_schema\|mysql' | xargs) | |
for DB_NAME in $DATABASE_LIST | |
do | |
mysqldump --user=${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} > ${BACKUP_PATH}/${DB_NAME}.sql | |
gzip -9 ${BACKUP_PATH}/${DB_NAME}.sql | |
echo "Backup was successful :: ${DB_NAME}" | |
done | |
# This part works when you set REMOTE STOGRAFE environments | |
[ ! -z "$REMOTE_STORAGE_URL" ] && scp -r ${BACKUP_PATH} ${REMOTE_STORAGE_USERNAME}@${REMOTE_STORAGE_URL}:${REMOTE_STORAGE_PATH} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment