CentOS 8.1下MariaDB关机自动备份、开机恢复及Vagrant自动化实现
Let's tackle your two requirements step by step—first automating MariaDB backups when your Vagrant VM shuts down, then setting up CentOS 8.1 to auto-backup on shutdown and auto-restore on boot.
Vagrant has built-in trigger support that lets you run scripts before or after VM actions like halt/shutdown. Here's how to set it up:
Step 1: Secure Your Database Credentials (Recommended)
Storing passwords directly in commands is risky. Create a .my.cnf file in the VM's root home directory to store MariaDB credentials:
# Inside your Vagrant VM, run this: cat > ~/.my.cnf << EOF [mysqldump] user=root password=your_actual_password [mysql] user=root password=your_actual_password EOF chmod 600 ~/.my.cnf # Restrict access to the file
Step 2: Add Shutdown Trigger to Your Vagrantfile
Edit your project's Vagrantfile to add a trigger that runs the backup command before the VM halts or is destroyed:
Vagrant.configure("2") do |config| # ... your existing VM config ... # Trigger backup before shutdown/halt/destroy config.trigger.before :halt, :destroy do |trigger| trigger.name = "MariaDB Full Backup on Shutdown" trigger.run = { inline: "mysqldump --lock-tables -e --all-databases > ~/dbs.sql" } end end
Step 3: Test It
Run vagrant halt to shut down the VM. When you boot it back up with vagrant up, check if ~/dbs.sql exists in the VM—it should contain your full database backup.
CentOS 8 uses systemd for service management, so we'll create custom systemd services to handle both tasks.
2.1 Set Up Shutdown-Time Full Backup
Step 1: Create the Backup Script
Create a reusable backup script at /usr/local/bin/mariadb-backup.sh:
cat > /usr/local/bin/mariadb-backup.sh << EOF #!/bin/bash # Full MariaDB backup script BACKUP_FILE="/root/dbs.sql" mysqldump --lock-tables -e --all-databases > \$BACKUP_FILE # Optional: Compress the backup to save space # gzip \$BACKUP_FILE EOF chmod +x /usr/local/bin/mariadb-backup.sh
Step 2: Create a Systemd Service for Shutdown Backup
Create a systemd service file at /etc/systemd/system/mariadb-shutdown-backup.service:
[Unit] Description=MariaDB Full Backup on Shutdown DefaultDependencies=no Before=shutdown.target reboot.target halt.target [Service] Type=oneshot ExecStart=/usr/local/bin/mariadb-backup.sh User=root [Install] WantedBy=shutdown.target reboot.target halt.target
Step 3: Enable the Service
Reload systemd and enable the service so it runs automatically on shutdown:
systemctl daemon-reload systemctl enable mariadb-shutdown-backup.service
2.2 Set Up Auto-Restore on Boot
Step 1: Create the Restore Script
Create a restore script at /usr/local/bin/mariadb-restore.sh:
cat > /usr/local/bin/mariadb-restore.sh << EOF #!/bin/bash BACKUP_FILE="/root/dbs.sql" # Only restore if the backup file exists if [ -f \$BACKUP_FILE ]; then mysql < \$BACKUP_FILE # Optional: Delete the backup after restore to avoid repeated imports # rm \$BACKUP_FILE fi EOF chmod +x /usr/local/bin/mariadb-restore.sh
Step 2: Create a Systemd Service for Boot Restore
Create a systemd service file at /etc/systemd/system/mariadb-boot-restore.service:
[Unit] Description=MariaDB Auto-Restore on Boot After=mariadb.service Requires=mariadb.service [Service] Type=oneshot ExecStart=/usr/local/bin/mariadb-restore.sh User=root [Install] WantedBy=multi-user.target
Step 3: Enable the Service
Reload systemd and enable the restore service:
systemctl daemon-reload systemctl enable mariadb-boot-restore.service
Key Notes for Both Setups
- Security: Always use the
.my.cnffile instead of passing passwords in plaintext commands—this prevents credential exposure in logs or process lists. - Backup Location: Consider moving the backup file to a dedicated directory like
/var/backups/mariadb/instead of/root/for better organization. Just make sure the directory exists and has proper permissions. - Vagrant Provisioning: If you want to automate the CentOS 8.1 setup in your Vagrant VM, add all the script and service creation commands to your Vagrantfile's
config.vm.provisionsection—this way the config is applied automatically when you runvagrant up.
内容的提问来源于stack exchange,提问作者user163831

