How to back up Managed MySQL to Object Storage
This guide describes how to set up a Cloud Server that dumps an UpCloud Managed MySQL database with mysqldump every hour, saves the dumps in an UpCloud Managed Object Storage bucket, and removes dumps that are older than seven days. It also describes how to restore a dump into a new service.
The dumps are independent of the service's automatic backups, and you can keep them for longer than the backup retention. For the automatic backups, see Managed MySQL Backups.
Before you start
You need:
- A Managed MySQL service.
- A Managed Object Storage instance with a bucket for the dumps, and a user with full access to that bucket only. To create the user, see How to get started with UpCloud Managed Object Storage, and for a policy that limits it to one bucket, see How to restrict user access in Managed Object Storage.
- An SDN Private Network with a router attached, in the database's location. Databases and Object Storage instances can only be attached to a network that has a router. To create a network, see How to configure SDN Private Networks, and to attach it to a router, see How to connect private networks using SDN Router.
- A Cloud Server to run the backups (Ubuntu, Debian, AlmaLinux, Rocky Linux or Fedora). If you don't have one yet, create it after the database and the Object Storage instance are on the network, as described in Connect the services to the network.
Connect the services to the network
Put the database, the Object Storage instance and the Cloud Server that runs the backups on the same SDN Private Network. This keeps the backup traffic, including the dumps, off the public internet.
First, attach the database and the Object Storage instance to the network. If the database is already on an SDN Private Network, use that network and attach only the Object Storage instance.
- New database: when you create it, click Attach private network under Connection access and select the network.
- Existing database on the Utility network: first connect the Cloud Servers that use the database to the SDN Private Network. Then, on the database's Overview page, under Connection, click Switch to SDN Networks, enter a name, and select the network. The switch rebuilds the service with its data. Afterwards, the service no longer accepts connections over the Utility network, and it can't be switched back.
- Object Storage instance: on its Overview page, under Private access, click Attach private network, select the network, and enter a name.
Wait until both services are running. Then create the Cloud Server on the same network, so that it gets the routes to the services when it starts. To use an existing Cloud Server that's already on the network, restart it so that it gets the routes.
For more about connecting to Object Storage over a private network, see How to connect to Managed Object Storage over an SDN Private Network.
Run the commands in the rest of this guide as root on the Cloud Server that runs the backups.
Install the MySQL client and rclone
Install the MySQL 8.4 client and rclone, and create the folder for rclone's configuration file. The commands install the client from MySQL's own packages, so that mysqldump matches the service's MySQL version. The commands work on Ubuntu 22.04, 24.04 and 26.04, Debian 12 and 13, AlmaLinux and Rocky Linux 8, 9 and 10, and Fedora 43.
apt-get update && apt-get install -y curl ca-certificates
curl -fsSo /usr/share/keyrings/mysql.asc https://repo.mysql.com/RPM-GPG-KEY-mysql-2025
. /etc/os-release
echo "deb [signed-by=/usr/share/keyrings/mysql.asc] https://repo.mysql.com/apt/${ID} ${VERSION_CODENAME} mysql-8.4-lts" > /etc/apt/sources.list.d/mysql.list
apt-get update && apt-get install -y mysql-client rclone
mkdir -p /root/.config/rcloneSave the connection details
Create a file for the database's connection details that only root can read:
install -m 600 /dev/null /etc/mysql-backup.cnfSave the following in it, with the service's private host and port and the
upadminpassword. In the Control Panel, you'll find them on the database's Overview page: the host and port under Connection on the Private tab, and the password under Auth, where you click Show to see it. In the API, they're in the service'sservice_uri_params.[client] host = <hostname> port = <port> user = upadmin password = <password> ssl-mode = REQUIREDssl-mode = REQUIREDmakes the client encrypt the connection with TLS. The service also accepts connections without TLS, so keep this line.Check that the Cloud Server reaches the database by listing the service's databases:
mysql --defaults-extra-file=/etc/mysql-backup.cnf -e 'SHOW DATABASES;'Output+--------------------+ | Database | +--------------------+ | defaultdb | | information_schema | | mysql | | performance_schema | | sys | +--------------------+Unless you've created other databases, your data is in
defaultdb.Save the following as
/root/.config/rclone/rclone.conf, with the Object Storage user's access keys and the instance's private endpoint:[dumps] type = s3 provider = Other access_key_id = <access-key> secret_access_key = <secret-key> endpoint = https://<private-endpoint> region = <region>Check that the Cloud Server reaches the bucket by listing the bucket's contents. The command finishes without an error, and prints nothing if the bucket is empty:
rclone lsf dumps:<bucket>
Schedule the backup
Save the backup script as
/usr/local/bin/mysql-backup:#!/bin/bash set -euo pipefail name=<database-name>-$(date -u +%Y%m%dT%H%M%SZ).sql.gz mysqldump --defaults-extra-file=/etc/mysql-backup.cnf --single-transaction --set-gtid-purged=OFF --routines --events --databases <database-name> | gzip | rclone rcat dumps:<bucket>/$name.partial rclone moveto dumps:<bucket>/$name.partial dumps:<bucket>/$name rclone delete --min-age 7d dumps:<bucket>Replace
<database-name>in both places with the name of the database you back up, for exampledefaultdb.The
mysqldumpoptions give you a consistent dump that you can restore into a new service.--single-transactionmakes the dump consistent without locking the tables. Along with the database's tables, views and triggers, the dump includes its stored routines and events, which--routinesand--eventsadd.--set-gtid-purged=OFFleaves out the source service's transaction IDs, because a new service can't apply them.Each dump is saved as a
.partialfile first and gets its final name only whenmysqldumpsucceeds. The script then removes the dumps that are older than seven days. To keep them for longer, for example beyond the database's backup retention, change7don the last line. To let Object Storage delete old dumps instead, remove the last line from the script and add a lifecycle rule to the bucket, as described in How to configure lifecycle policies.Save the systemd service that runs the script as
/etc/systemd/system/mysql-backup.service:[Unit] Description=Back up MySQL to Object Storage [Service] Type=oneshot ExecStart=/usr/local/bin/mysql-backupSave the systemd timer that starts the backup every hour as
/etc/systemd/system/mysql-backup.timer:[Unit] Description=Back up MySQL every hour [Timer] OnCalendar=hourly Persistent=true [Install] WantedBy=timers.targetMake the script executable, start the timer, and run the first backup:
chmod 755 /usr/local/bin/mysql-backup systemctl daemon-reload systemctl enable --now mysql-backup.timer systemctl start mysql-backup.service
Check the dumps
To check that the backups run, list the dumps in the bucket:
rclone lsl dumps:<bucket> 15774 2026-11-27 03:00:15.456023291 defaultdb-20261127T030015Z.sql.gz
15774 2026-11-27 04:00:15.455457202 defaultdb-20261127T040015Z.sql.gz
15774 2026-11-27 05:00:12.724812019 defaultdb-20261127T050012Z.sql.gzA .partial file in the list is a backup that failed or is still running. If a dump is missing, or a .partial file stays in the list, run journalctl -u mysql-backup.service to see why.
Restore a dump
Create a new Managed MySQL service for the restore. You run the restore from a Cloud Server on a network that's attached to both the new service and the Object Storage instance. On that Cloud Server, install the MySQL client and rclone as described in Install the MySQL client and rclone, and save rclone's configuration file as described in Save the connection details.
Save the new service's connection details in a second file, the same way as in Save the connection details:
install -m 600 /dev/null /etc/mysql-restore.cnf[client] host = <target-hostname> port = <target-port> user = upadmin password = <target-password> ssl-mode = REQUIREDRestore the dump:
rclone cat dumps:<bucket>/<file> | gunzip | sed -E 's/DEFINER=[`"][^`"]*[`"]@[`"][^`"]*[`"]//g' | mysql --defaults-extra-file=/etc/mysql-restore.cnfThe dump creates the database and its tables, views, routines, triggers and events in the new service. The
sedcommand removes theDEFINERclauses, so thatupadminowns the views, routines, triggers and events in the new service. Without it, the restore stops withERROR 1227 (42000) ... you need (at least one of) the SUPER or SET_ANY_DEFINER privilege(s)when it reaches an object that another user created.
The dump doesn't include the service's users. In the new service, create the users that your applications connect with, as described in How to restrict user access in Managed MySQL.