How to use Systemd and Crontab to regularly back up data in a Linux system
In daily work and life, data backup is very important. Whether you are an individual user or a business user, regular backup of data can avoid the risk of data loss and damage. In Linux systems, we can use Systemd and Crontab to automatically back up data regularly. This article will use specific code examples to introduce how to use Systemd and Crontab to implement scheduled backup.
Systemd is a Linux system initialization system and manager, which provides a more advanced way to manage system processes. By using Systemd's timer function, we can implement scheduled tasks. Crontab is a program for executing tasks on a scheduled basis. We can implement scheduled backup by editing the Crontab configuration file.
The following are specific steps and code examples:
For example, we create a Shell script named backup.sh to back up all files in the /data directory:
#!/bin/bash backup_dir="/path/to/backup/" source_dir="/data/" timestamp=$(date +%Y%m%d%H%M%S) backup_file="${backup_dir}/backup_${timestamp}.tar.gz" tar -czvf ${backup_file} ${source_dir}
This script will backup all files in the /data directory The file is packaged into a tar.gz file named with the current timestamp, and the backup file is saved in the specified directory.
Please modify the path and file name in the backup script according to actual needs.
Execute the following command in the terminal to create a Systemd timer unit file named backup.timer:
sudo nano /etc/systemd/system/backup.timer
In the opened file, enter the following:
[Unit] Description=Backup Service Timer [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.target
Save and close the file.
This timer will perform a backup task once a day. If you need to customize the execution time of scheduled tasks, please modify the parameters behind OnCalendar according to your needs.
Execute the following command in the terminal to create a Systemd service unit file named backup.service:
sudo nano /etc/systemd/system/backup.service
In the opened file, enter the following:
[Unit] Description=Backup Service [Service] ExecStart=/path/to/backup.sh [Install] WantedBy=multi-user.target
Please replace the path in ExecStart with the actual backup script path.
Save and close the file.
Execute the following command in the terminal to enable and start the timer and service:
sudo systemctl daemon-reload sudo systemctl enable backup.timer sudo systemctl start backup.timer
Now, the Systemd timer will automatically perform the backup task according to the configured time.
Execute the following command in the terminal to edit the current user's Crontab configuration file:
crontab -e
Add the following content to the end of the file:
0 0 * * * /path/to/backup.sh
Save and close the file.
This Crontab configuration will perform backup tasks at 12 am every day. You can customize the execution time of backup tasks according to your needs.
Now, we have completed the steps of using Systemd and Crontab to regularly back up data in the Linux system. Whether you use Systemd timer or Crontab, you can implement scheduled automatic backup. Just choose the appropriate method according to actual needs.
I hope this article will be helpful to you, and I wish you good luck with your data backup work!
The above is the detailed content of How to use Systemd and Crontab to regularly back up data in Linux systems. For more information, please follow other related articles on the PHP Chinese website!