How can I create daily backups of my Mysql database?

Hi everyone! I want to create daily backups of my Mysql database, I'm not sure if I should create an app with a cronjob or something like that. Can anyone help me with this? There's no option in the project settings. Thanks. ProjectID f4ffaf00-bf8e-4fc7-8e95-9d64d9995b8d
Solution:
you could easily modify the template linked in this tutorial to do a mysql backup instead https://blog.railway.app/p/automated-postgresql-backups...
Jump to solution
84 Replies
Percy
Percy13mo ago
Project ID: f4ffaf00-bf8e-4fc7-8e95-9d64d9995b8d
Solution
Brody
Brody13mo ago
you could easily modify the template linked in this tutorial to do a mysql backup instead https://blog.railway.app/p/automated-postgresql-backups
danielthames
danielthames13mo ago
Hey I want to work on this now! @Brody I saw that the example contains a dockerfile with the postgree config inside is that correct? the postgree db runs inside the docker? Do I need a docker file?
danielthames
danielthames13mo ago
this is my repo... independent mysql db I plan to create the project and then add it to this project
Brody
Brody13mo ago
no what you are referring to would just be the postgres client library and the dump utility
danielthames
danielthames13mo ago
I have my node cron app ready to get the mysql db and then upload it to cloudinary - this is based in the example with postgree here you can see the screenshot to dump the file
danielthames
danielthames13mo ago
Do you see something wrong? Should it work with the database shown above?
Brody
Brody13mo ago
looks like it could work to me, but it's not like I've ever done this type of thing before
danielthames
danielthames13mo ago
ok what I'm going to have is something like this
danielthames
danielthames13mo ago
I will push the project and see what happens
danielthames
danielthames13mo ago
I'm going to use a test project just in case
Brody
Brody13mo ago
sounds good
danielthames
danielthames13mo ago
It doesn't work
danielthames
danielthames13mo ago
basically the mysql db doesn't exists for my cron job
Brody
Brody13mo ago
are you installing mysqldump into the container?
danielthames
danielthames13mo ago
it's not a container Should I work with docker? I need to migrate my db from standalone railway mysql to a docker container with mysql + cronApp ?
Brody
Brody13mo ago
railway uses docker, so you can just install mysqldump the same way that the postgres backup cron job app installs pg_dump
danielthames
danielthames13mo ago
there's not a pg_dump; but I got you I'm going to run -> npm i mysqldump
Brody
Brody13mo ago
pg_dump is installed with postgresql-client in the templates dockerfile
danielthames
danielthames13mo ago
ok I see that it doesn't work only installing it; but I see what I can do with this package I will make some modifications and try it
Brody
Brody13mo ago
you don't need to change your code lol just install mysqldump in the container whether you do that with a dockerfile or a railway.json file is up to you
danielthames
danielthames13mo ago
ok, I'm going to create this exact same file in my cronApp; changing pg_dump to mysqldump
danielthames
danielthames13mo ago
https://github.com/danielthames360/cronJob by the way, this is my repo
GitHub
GitHub - danielthames360/cronJob
Contribute to danielthames360/cronJob development by creating an account on GitHub.
Brody
Brody13mo ago
mysqldump doesn't appear to be an alpine package
danielthames
danielthames13mo ago
yup!
danielthames
danielthames13mo ago
I'm almost there
danielthames
danielthames13mo ago
29c518cd-5f54-47a7-841e-9a5f4c8b2c3d project test ID
danielthames
danielthames13mo ago
final docker file just change the postgre client to mysql-client
Brody
Brody13mo ago
are you checking if these are actually available alpine packages?
danielthames
danielthames13mo ago
it's trying to make the bkp; I'm getting an error now
danielthames
danielthames13mo ago
Brody
Brody13mo ago
let's not resort to chat gpt lol
danielthames
danielthames13mo ago
lol
Brody
Brody13mo ago
add mysql in line 22
danielthames
danielthames13mo ago
RUN apk add --update --no-cache mysql mysql-client nodejs npm same error after deploy it https://chrisshennan.com/blog/fixing-authentication-plugin-cachingsha2password-cannot-be-loaded-errors I'm not sure Hey man! we can continue tomorrow if you want; I don't want to waste your Friday time cryingman I'll delete this message...
Brody
Brody13mo ago
nah it's good my Friday night has already come to an end and tbh I don't actually know why the original template uses a dockerfile, everything it does can easily be configured through a railway.json file and built with nixpacks once I'm back on my computer in a bit I can try to slap together a railway.json file for you
danielthames
danielthames13mo ago
I appreciate your time! and help If you know anything I can do to make it work would be fantastic. I am not in any hurry. So, I can wait pattienly
Brody
Brody13mo ago
in the mean time remove mysqldump from your package.json
danielthames
danielthames13mo ago
Done! I've also moved the dev dependencies to their correct place
Brody
Brody13mo ago
delete your Dockerfile and give this railway.json file a try
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"nixpacksPlan": {
"phases": {
"setup": {
"nixPkgs": ["...", "mysql80"]
}
}
}
},
"deploy": {
"numReplicas": 1,
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"nixpacksPlan": {
"phases": {
"setup": {
"nixPkgs": ["...", "mysql80"]
}
}
}
},
"deploy": {
"numReplicas": 1,
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
danielthames
danielthames13mo ago
weird error
Brody
Brody13mo ago
one moment change your start script to node dist/index.js
Brody
Brody13mo ago
the postgre backup template used node 18, try using node 18 set engines.node = 18 in your package.json
danielthames
danielthames13mo ago
I think I'm almost there
Brody
Brody13mo ago
lol what solved those es5 errors
danielthames
danielthames13mo ago
it was... Firstly, I was working with javascript only. Then, I migrated the project to typescript and I don't needed the type: module on package.json anymore
Brody
Brody13mo ago
removed the module type, makes sense since node 16 probably doesnt support the module type
danielthames
danielthames13mo ago
Brody
Brody13mo ago
well is the file in cloudinary valid?
danielthames
danielthames13mo ago
danielthames
danielthames13mo ago
I'm gonna give it a try in my local db
Brody
Brody13mo ago
yeah was just about to say to do a test restore
danielthames
danielthames13mo ago
oh; these are the lines maybe I need to add/update some lines of my mysqldump instruction in order to have a fully backup with schema creation, data and so on.
danielthames
danielthames13mo ago
by the way, this was an empty db only for test purpose .
Brody
Brody13mo ago
sounds plausible, ive never used mysqldump
danielthames
danielthames13mo ago
lol the first instrucctions says -> --no-create-db --no-create-info --skip-comments mildpanic
danielthames
danielthames13mo ago
after
Brody
Brody13mo ago
yeah that sounds like stuff you'd want
danielthames
danielthames13mo ago
awesome awesome awesome!!
Brody
Brody13mo ago
you should totally make this into a template, if that's something you'd want to do
danielthames
danielthames13mo ago
I will update the code to work with a specific folder or some specific things of my liking (cloudinary stuff too). Tomorrow morning I will test this project with the real production application, and make the cron work based on a configuration. With that I think I'll be all set and the project will be fully configurable. I'll attach some screenshots here
Brody
Brody13mo ago
awesome
danielthames
danielthames13mo ago
thank you very much Brody! have a good rest
Brody
Brody13mo ago
haha no problem, happy to help!
danielthames
danielthames13mo ago
definitely! I'm not sure how to do that but just let me know what I can do tomorrow, I'll clean the code and do what I described above.
Brody
Brody13mo ago
https://railway.app/account/templates New Template add your public repo, and configure all the variables and such, if you need any help with that tomorrow feel free to ask me
danielthames
danielthames13mo ago
hey Brody! I have the app ready to create the template https://github.com/danielthames360/mysqlCloudinaryBackups Would you suggest something?
GitHub
GitHub - danielthames360/mysqlCloudinaryBackups: Node.js applicatio...
Node.js application that performs scheduled backups of your MySQL database and uploads them to Cloudinary for secure storage. - GitHub - danielthames360/mysqlCloudinaryBackups: Node.js application ...
danielthames
danielthames13mo ago
Should I have to add the variables I use?
Brody
Brody13mo ago
just add all the variable names you have in your .env.example file, with no default value like how this does it https://railway.app/new/template/I4zGrH
danielthames
danielthames13mo ago
Railway
Deploy mysqlCloudinaryBackups on Railway
Railway is an infrastructure platform where you can provision infrastructure, develop with that infrastructure locally, and then deploy to the cloud.
Brody
Brody13mo ago
Brody
Brody13mo ago
now you just have to write info text for it and publish it
danielthames
danielthames13mo ago
where? how?
Brody
Brody13mo ago
ohhh publishing templates is a beta feature so just type /beta here so are you gonna upgrade to the dev plan too? otherwise you won't be able to run the backup 24/7
danielthames
danielthames13mo ago
yeap! my client has their credit card in his account we have the production project there I don't need a plan for my personal account yet.
danielthames
danielthames13mo ago
Brody
Brody13mo ago
sounds good
danielthames
danielthames13mo ago
Ok done! thanks for the patience Brody
Brody
Brody13mo ago
no problem, im happy i could help!