Written by Indigo Tech Tutorials
August 13, 2025
1 views
Backing up your database is an important thing when you have a website online especially when you want to migrate your app to a new server for things like reducing costs and if you found a better service to use. Whatever the reason is, once you want to migrate your app you are going to also want to move your database as well so that you don't lose your users and apps data. This guide will tell you how to do so using Ruby on Rails and Kamal First of all we will start in the terminal on your computer. We are going to SSH into are app
ssh -i ~/ssh-key user@server-ip
Next we are going to get the docker container id that are app is using and store it as a shell variable so we can use it for the following commands. ( Replace your-app-web with the name of your service found in config/deploy.yml)
CONTAINER_ID=$(docker ps --filter name=your-app-web --format '{{.ID}}')
Next we are going to create a backup of our database inside of the docker container which is where the sqlite3 database is running
docker exec $CONTAINER_ID sqlite3 /rails/storage/production.sqlite3 ".backup /tmp/backup.sqlite3"
Then we copy the backup file from the container onto the server outside of the docker container
docker cp $CONTAINER_ID:/tmp/backup.sqlite3 /root/backup.sqlite3
Now you can exit out of the SSH and go back to your local terminal. We will now copy the file from the server to our local machine so we can store it until we move it to the new server
scp -i ~/ssh-key root@your-server:/root/backup.sqlite3 ./backup.sqlite3
Congrats! You now have a backup of your apps database on your machine. Next I will show you how to migrate the database to the new server. You are going to want to acquire your new server and run the kamal setup command so that your app is running. You should have an empty database on the new app with no data yet. To migrate the database to the new server we will do a similar process from the backup but in reverse order. First of all you will copy the local backup file to the new server
scp -i ~/ssh-key ./backup.sqlite3 root@your-server:/root/backup.sqlite3
Then we will SSH into the new server
ssh -i ~/ssh-key user@server-ip
Then we want to get the container id again for the docker container of our new app
CONTAINER_ID=$(docker ps --filter name=your-app-web --format '{{.ID}}')
Then we can copy the backup file which we have on the server into the docker container and into the place that rails is using for the production sqlite3 file
docker cp /root/backup.sqlite3 $CONTAINER_ID:/rails/storage/production.sqlite3
Finally you want to reboot your app so that it will use the new database file. Exit out of the SSH and run this command on your local terminal ( inside of your app folder so that the kamal command works )
kamal app boot
Migrate from Rails built in authentication to Devise Gem
Today I am migrating my app from the rails built in authentication to use the...
The problem with Rails built in authentication generator
The rails authentication generator was added in Rails version 8 and it seemed...
Upgrade Ruby version with Mise
Ruby 3.4.5 was released last month but I had been using 3.4.4 still and now R...
How to backup & restore SQLite3 database with Ruby on Rails 8 & Kamal
Backing up your database is an important thing when you have a website online...
Get a custom tutorial for Ruby on Rails
Do you have an idea to build a website but you don't know where to start? Are...
Ruby on Rails mentorship for $10
This is your opportunity to get exclusive mentorship and guidance with your R...
How to permit nested attributes in Rails 8
To permit nested attributes in rails 8 with the new .expect method you need t...
What will you do before GTA 6 is released?
GTA 6 is a game that is very highly awaited from all gamers. The phrase "We g...
New videos soon