Automating Server Backup on Shutdown
In the world of server administration, ensuring that your data is backed up regularly is crucial to maintaining the integrity and availability of your services. One way to streamline this process is by automating server backups. In this guide, we’ll walk you through the steps to set up an automated backup system using Systemd and Shell Scripts, making it easier than ever to protect your data.
Step 1: Create a Systemd Service Unit
Systemd is a powerful init system that allows us to manage system services, including our backup script. To start, we’ll create a Systemd service unit by running:
sudo nano /etc/systemd/system/santoshm-shutdown.service
smbackup.sh file with following commands.
#!/bin/bash PROJECT_DIR="$(dirname "$(readlink -f "$0")")" cd "$PROJECT_DIR" || exit php artisan online-backup-system-command echo "...Successful..."
This file will define our backup service.
[Unit] Description="santoshm database backup" After=network.target mysql.service Before=poweroff.target shutdown.target reboot.target [Service] Type=oneshot ExecStart=/var/www/santoshm/smbackup.sh [Install] WantedBy=shutdown.target reboot.target poweroff.target exit.target
Here’s what each section of the service unit file does:
- [Unit] Section:
Description: This is a human-readable description of your service. It provides a brief explanation of what the service does. In your case, it describes that this service is related to SantoshM database backup.
After: This directive specifies the units that must be started before this service can start. You’ve listed
mysql.service, indicating that this service should start after the network is up and the MySQL service is started.
Before: This directive specifies the units that should be started after this service. You’ve listed
reboot.target, indicating that this service should start before the system is powered off, shut down, or rebooted.
- [Service] Section:
Type: This directive specifies the service type. In your case, it’s set to
oneshot, which means the service will be considered active until the script (specified in
ExecStart: This is the command or script that will be executed when the service is started. In your case, it’s
/var/www/santoshm/smbackup.sh, which is your backup script.
- [Install] Section:
WantedBy: This specifies the target units for which this service is a wanted dependency. In your case, it’s set to
exit.target. This means your service is wanted when the system is shutting down, rebooting, powering off, or exiting.
systemctl daemon-reload # to check the log. If something goes wrong, run this command and you will see issue at top but in reverse style. journalctl -u santoshm-shutdown.service --reverse
Step 2: Save and Exit
Save the file and exit the text editor.
Step 3: Enable the Service
Now, let’s enable our newly created service:
sudo systemctl enable santoshm-shutdown.service
This command tells Systemd to start our service when the system boots up and stop it when the system is shutting down.
If you want to if the service is working or not use this command.
sudo systemctl start santoshm-shutdown.service # restart if existed sudo systemctl restart santoshm-shutdown.service
Step 4: Create Your Backup Script
Before we can take advantage of our new service, we need a backup script. Create your backup script (e.g.,
/var/www/santoshm/smbackup.sh) and make sure it contains the necessary backup logic. Adjust the path and content as per your requirements.
Step 5: Make the Script Executable
Ensure that your backup script is executable by running following command. `Please note, this is important, else .sh file wont execute.`
chmod +x /var/www/santoshm/smbackup.sh
Step 6: Testing the Configuration
You can now test your backup system by rebooting your server:
During the shutdown process, Systemd will trigger your backup script, creating a backup of your data.
In this guide, we’ve shown you how to automate server backups using Systemd and Shell Scripts. By creating a Systemd service unit and a backup script, you can ensure that your data is regularly backed up, enhancing the reliability and security of your server. Automating backups not only saves time but also provides peace of mind, knowing that your data is safe even in unexpected situations.