Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to simply realize scheduled backup in Linux system

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to simply achieve scheduled backup in the Linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Run a simple script

Suppose you have a script called / usr/local/bin/myscript, and you want to run it every other hour.

Service file

The first step is to create a service file and place it in the appropriate system directory according to your Linux distribution (in Arch, this directory is / etc/systemd/system/ or / usr/lib/systemd/system)

Myscript.service

The code is as follows:

[Unit]

Description=MyScript

[Service]

Type=simple

ExecStart=/usr/local/bin/myscript

Note that be sure to set the value of the Type variable to "simple" instead of "oneshot". Use "oneshot" to make the script run only for the first time, and then the system will assume that you don't want to run it again, thus turning off the Timer we created next.

Timer file

The second step is to create a timer file and place it in the directory where the service file was placed in the first step.

Myscript.timer

The code is as follows:

[Unit]

Description=Runs myscript every hour

[Timer]

# run for the first time 10 minutes after startup

OnBootSec=10min

# interval between each run

OnUnitActiveSec=1h

Unit=myscript.service

[Install]

WantedBy=multi-user.target

Authorization / operation

The timer file, not the service file, is licensed and run.

The code is as follows:

# start the timer as root

Systemctl start myscript.timer

# enable this timer after the system boots

Systemctl enable myscript.timer

Run multiple scripts on the same Timer

Now let's assume that you want to run multiple scripts at the same time. In this case, you need to make appropriate changes in the above file.

Service file

Create your service file to run your script, but include the following at the end of each service file:

The code is as follows:

[Install]

WantedBy=mytimer.target

If you have some dependency order in your service file, make sure you use the values in the Description field to specify the parameters in After=something.service and Before=whatever.service.

Another option (or even simpler) is to create a wrapper script to run the commands in the correct order and use the script in your service file.

Timer file

All you need is a timer file to create a mytimer.timer, as I pointed out above.

Target file

You can create more than one target file that all scripts depend on.

Mytimer.target

The code is as follows:

[Unit]

Description=Mytimer

# Lots more stuff could go here, but it's situational.

# Look at systemd.unit man page.

Authorization / startup

You need to authorize all service files and timer files.

The code is as follows:

Systemctl enable script1.service

Systemctl enable script2.service

...

Systemctl enable mytimer.timer

Systemctl start mytimer.service

Good luck.

This is the end of the content of "how to simply achieve scheduled backup in the Linux system". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report