Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use restic and systemd for automatic backup of linux

Shulou Source: shulou.com Published: 2022-06-01 05:37:08 09月28日 Update

Editor to share with you how linux uses restic and systemd automatic backup, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

If you have not already installed restic, please note that it is in the official Fedora repository. To install it, run this command with sudo:

$sudo dnf install restic backup

First, create ~ / .config/systemd/user/restic-backup.service. Copy and paste the following text into the file for better results.

[Unit] Description=Restic backup service [service] Type=oneshotExecStart=restic backup-- verbose-- one-file-system-- tag systemd.timer $BACKUP_EXCLUDES $BACKUP_PATHSExecStartPost=restic forget-- verbose-- tag systemd.timer-- group-by "paths,tags"-- keep-daily $RETENTION_DAYS-- keep-weekly $RETENTION_WEEKS-- keep-monthly $RETENTION_MONTHS-- keep-yearly $RETENTION_YEARSEnvironmentFile=%h/.config/restic-backup.conf

This service references the environment file to load the key (for example, RESTIC_PASSWORD). Create ~ / .config/restic-backup.conf. Copy and paste the following for better results. This example uses BackBlaze B2 storage. Please adjust the ID, key, repository, and password values accordingly.

BACKUP_PATHS= "/ home/rupert" BACKUP_EXCLUDES= "--exclude-file / home/rupert/.restic_excludes-- exclude-if-present. RETENTION_DAYS=7RETENTION_WEEKS=4RETENTION_MONTHS=6RETENTION_YEARS=3B2_ACCOUNT_ID=XXXXXXXXXXXXXXXXXXXXXXXXXB2_ACCOUNT_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXRESTIC_REPOSITORY=b2:XXXXXXXXXXXXXXXXXX:/RESTIC_PASSWORD=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX _ from_backup" RETENTION_DAYS=7RETENTION_WEEKS=4RETENTION_MONTHS=6RETENTION_YEARS=3B2_ACCOUNT_ID=XXXXXXXXXXXXXXXXXXXXXXXXXB2_ACCOUNT_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXRESTIC_REPOSITORY=b2:XXXXXXXXXXXXXXXXXX:/RESTIC_PASSWORD=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Now that the service is installed, reload systemd:systemctl-user daemon-reload. Try running the service manually to create a backup: systemctl-user start restic-backup.

Because the service type is one-time, it will run once and exit. After verifying that the service is running and creating snapshots as needed, set a timer to run the service periodically. For example, to run restic-backup.service every day, create ~ / .config/systemd/user/restic-backup.timer as shown below. Copy and paste this text again:

[Unit] Description=Backup with restic daily[Timer] OnCalendar=dailyPersistent= true[Install] WantedBy=timers.target

Run the following command to enable:

$systemctl-user enable-now restic-backup.timer cleanup

Although the master service runs the forget command to keep only the snapshots in the retention policy, it does not actually delete the data from the restic repository. The prune command checks the warehouse and the current snapshot and deletes all data that has nothing to do with the snapshot. Because prune can be a time-consuming process, you do not need to run it each time you run a backup. This is the second service and timer scenario. First, create the file ~ / .config/systemd/user/restic-prune.service by copying and pasting the text:

[Unit] Description=Restic backup service (data pruning) [Service] Type=oneshotExecStart=restic pruneEnvironmentFile=%h/.config/restic-backup.conf

Similar to the main restic-backup.service service, restic-prune can be run manually. After setting up the service, create ~ / .config/systemd/user/restic-prune.timer and enable the corresponding timer:

[Unit] Description=Prune data from the restic repository MonthlyTimer] OnCalendar=monthlyPersistent= truth [install] WantedBy=timers.target is all the content of the article "how to use restic and systemd for automatic backup by linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Runs services backups repositories commands snapshots content files articles timers keys manual effects data one-time not much again scenarios most official Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Docker Shulou Technology Shulou Information