Dump and backup a database on shutdown
I’m using Multipass as the virtualisation tool for quickly setting up virtual development machines on my Mac Studio using cloud-init for configuration and setting everything up. This really works great and has saved me several times where stuff crashed and burned, it was really easy just to tear everything down and re-run the setup scripts. (You can read more on my setup in the repository I use for this. This works fine as my development stuff is mostly in stored in Git and the data in a shared MySQL virtual server but as I recently found out this is not lways the case. Sometimes there is local data on the virtual server that you would like to keep.
The solution I came up with to prevent the loss of data is to trigger a script on the shutdown of the server that would copy the relevant data to a safe location. In my case that would be an S3 bucket. I took some digging, searching and testing but I got it working. So if you are looking for something similar, here how I did it:
We use a system service that runs at the start of the shutdown proces, so that other services that we rely on are still running. I’ve named it my S3shutdown.service which is the name of a file which you need to create in
/etc/systemd/system/ with the follwing content:
[Unit] Description=Save database to S3 Before=shutdown.target reboot.target halt.target [Service] Type=oneshot RemainAfterExit=true ExecStop=/home/ubuntu/projects/dumpandstore.sh [Install] WantedBy=multi-user.target
Where the first line is a descriptive title which you will see used in syslog when it is executed. The last line defines the runtime, so before the multi user mode ends. Referenced by
ExecStop you reference the shell script that should be run at the moment the server is going down.
dumpandstore.sh script looks like:
#! /bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin /usr/bin/mysqldump -uuser -ppassword databasename > /home/ubuntu/projects/databasedump.sql; today=$(date +%Y%m%d); cp /home/ubuntu/projects/databasedump.sql.sql /home/ubuntu/projects//databasedump$today.sql /usr/bin/gzip /home/ubuntu/projects//databasedump.sql$today.sql /usr/local/bin/aws s3 cp /home/ubuntu/projects/databasedump.sql$today.sql.gz s3://mybucketname/ /usr/local/bin/aws s3 cp /home/ubuntu/projects/databasedump.sql s3://mybucketname/
I’ve used a dump with a data to build some historic perspective, the other file without data is so to speak the last copy and is also referenced in the build script of the server. So that when I rebuild the server the database is filled with the last used dataset.
To activate the service you’ll need to run the command:
sudo systemctl enable S3shutdown.service Reboot the machine and everything should be working as intended. Some problem struggled with was the aws comfiguration. I had setup the aws configuration including credentials as a normal user but the shutdown service runs as root and therefore the aws command cloud not locate the proper credentials. This was quicky solved by copying the
~/.aws directory to
/root Not ideal but it made it work for the moment, I need to do more research for a more elegant and safer solution.