Backup Mysql Database Script

Hi together

i was looking for some Backups for Database but all Apps are outdated.
I made today a simple Bash script to upload on the server and run it with a CronJob

maybe someone can use it, have some better ideas for and can Fork it or we can work together and make it better :slight_smile:

Github MYSQL backup

Regards Alex

3 Likes

Thanks! You saved the day!

A suggestion: If you revise the script, could you add “localhost” as the default HOSTNAME? It makes more sense to have, especially for those of us who aren’t MySQL experts :slight_smile:

Also, it seems you’ve exposed a password, but I assume you have already updated that…

:+1:

Thank you for the feedback.

I have made the necessary adjustments. I would be grateful if you could review the script (the changes) and let me know if you notice anything else or have any other suggestions.

The password was random :blush:

Regards,
Alex.

1 Like

Very nice it is difficult to make universal backup script.

I personally backup system wide so nextcloud is included in my system backup.

never the less there are many options to think off.
If there is access to occ then all your variables can be fetched from the config. It is not a good idea to keep blanc passwords inside scripts.

DATABASE_NAME=nextcloud
USER_NAME=cloud
PASSWORD=youPassword

 BACKUP_DIR='/media/nextcloud-backup'
 ## Nextcloud installation Directory
 NCPATH='/var/www/nextcloud'
 ## Apache user
 HTUSER='www-data'
 HTGROUP='www-data'
 
 ### No edit beyond here
 occ_cmd="sudo -u ${HTUSER} php -f ${NCPATH}/occ"
 dbname="$($occ_cmd config:system:get dbname)"
 dbuser="$($occ_cmd config:system:get dbuser)"
 dbpassword="$($occ_cmd config:system:get dbpassword)"

and Personally use

mysqldump --single-transaction --default-character-set=utf8mb4 -u ${dbuser} -p${dbpassword} ${dbname} > ${BACKUP_PATH}.sql

as per manual Enabling MySQL 4-byte support — Nextcloud latest Administration Manual latest documentation note at the bottom

and here

Backup — Nextcloud latest Administration Manual latest documentation

backup_path as not defined here could be BACKUP_PATH=${BACKUP_DIR}/${DATE}_${TIME}

Other things to consider. How large is the datebase ? How long does your backup take. Do you use maintenance.

you suggest on github a restore that will not work. you must first drop database then create a new one and then restore your backup

Restoring backup — Nextcloud latest Administration Manual latest documentation