Database management
I’m very familiar with databases as I started developing websites years ago which back then were connected to Microsoft Access databases using ASP service side code. Nowadays, I utilise tools like PHPMyAdmin or even in some cases use bash scripting to export whole MySQL databases to textfiles so that it is easier to run find and replace commands to replace all instances of an old domain name in favour of a new one. Migrating WordPress can be a hassle and I’ve learnt that plugins which claim to do this are slow, inefficient and sometimes unreliable if the server doesn’t have enough resources to cope with the demand of really large databases.
As I’ve stopped relying on hosting my websites on hosting plans with cPanels, I tend to have one instance of PHPMyAdmin on a secure internal network that allows me to connect and manage any MySQL databases running in their own docker containers.
mkdir -p ${DB_BACKUP_PATH}/${TODAY}
echo "Backup started for database - ${DATABASE_NAME}"
mysqldump -h ${MYSQL_HOST} \
-P ${MYSQL_PORT} \
-u ${MYSQL_USER} \
-p${MYSQL_PASSWORD} \
${DATABASE_NAME} | gzip > ${DB_BACKUP_PATH}/${TODAY}/${DATABASE_NAME}-${TODAY}.sql.gz
if [ $? -eq 0 ]; then
echo "Database backup successfully completed"
else
echo "Error found during backup"
exit 1
fi
##### Remove backups older than {BACKUP_RETAIN_DAYS} days #####
DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
if [ ! -z ${DB_BACKUP_PATH} ]; then
cd ${DB_BACKUP_PATH}
if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
rm -rf ${DBDELDATE}
fi
fi
### End of script ####