This guide provides step-by-step instructions to set up a MySQL backup script using Docker containers and 'systemd' on a Linux server. The script performs a full backup every 12 hours and a differential backup every hour. It also ensures old backups are deleted after seven days.
- Docker installed and running
- MySQL container running
- Basic knowledge of shell scripting and systemd
Clone the backup script repository from the following GitHub link:
git clone https://github.com/omidx/MySQL-Backup-Service.gitOnce downloaded, make the script executable:
chmod +x backup_script.shCreate a systemd service file named 'backup_service.service' with the following content:
[Unit]
Description=MySQL Backup Service
After=docker.service
Requires=docker.service
[Service]
ExecStart=/path/to/backup_script.sh
Restart=always
RestartSec=10
User=your_user_name
WorkingDirectory=/path/to/
StandardOutput=append:/var/log/mysql_backup.log
StandardError=append:/var/log/mysql_backup_error.log
[Install]
WantedBy=multi-user.targetCopy the service file to the systemd directory:
sudo cp backup_service.service /etc/systemd/system/Reload systemd to recognize the new service:
sudo systemctl daemon-reloadEnable the service to start on boot:
sudo systemctl enable backup_serviceStart the service:
sudo systemctl start backup_serviceCheck the status of the service to ensure it is running correctly:
sudo systemctl status backup_serviceYou should see output indicating that the service is active and running. If there are any issues, check the logs specified in the service file ('/var/log/mysql_backup.log' and '/var/log/mysql_backup_error.log') for more details.
Your MySQL backup script is now set up to run automatically using 'systemd'. It will create full and differential backups at specified intervals and clean up old backups as needed. This setup ensures your MySQL data is regularly backed up and managed efficiently.