You are not logged in.

#1 2021-10-01 04:08:06

kindusmith
Member
Registered: 2019-12-30
Posts: 16

[SLOVED] I backed up my system with systemd

Here is how I did it:

1. Create a new "backup" folder in my personal /home/kindu/ directory, where the tar.gz archive of the backed up system is saved, and create two new files: backup and exclude.txt. The first one writes the script commands for the backup files, and the second one writes the files to be excluded. Also chmod +x backup to add executable permissions

2. Then write two files backup.service and backup.timer with an editor like vim, the purpose of which is to call systemd timer to start the task of performing backups regularly. Move these two files to /usr/lib/systemd/system or /etc/systemd/system

3. Finally, type sudo systemctl enable backup.timer to activate the task of performing backups periodically

4. periodically use the systemctl list-timers command to check how many days are left before executing  backup

backup:

#!/bin/bash
# full system backup

# Backup destination
backdest=/home/kindu/backup

# Labels for backup name
PC=${HOSTNAME}
distro=arch
type=full
date=$(date "+%F")
backupfile="$backdest/$distro-$PC-$type-$date.tar.gz"

# Exclude file location
prog=exclude # Program name from filename
excdir="/home/kindu/backup"
exclude_file="$excdir/$prog.txt"

# recovery os with tar --acls --xattrs -xpf  -C /mnt 
tar --exclude-from=$exclude_file --acls --xattrs -cpPvf $backupfile /

   
exclude.txt:
                                  
/home                                                                                                                   
/tmp/*
/var/cache/pacman/pkg
/var/log/journal
/proc
/dev
/sys


backup.service:

[Unit]
Description="backup archlinux full"

[Service]
Type=oneshot
ExecStart=/bin/sh -c '/bin/rm /home/kindu/backup/arch*'
ExecStart=/bin/bash /home/kindu/backup/backup
RemainAfterExit=true

backup.timer:

[Unit]
Description="backup archlinux full timer"

[Timer]
OnCalendar=weekly
AccuracySec=12h
Persistent=true

[Install]
WantedBy=timers.target
[kindu@legion ~]$ systemctl list-timers
NEXT                        LEFT          LAST                        PASSED       UNIT                         ACTIVATES        >
Fri 2021-10-01 18:00:00 CST 5h 50min left n/a                         n/a          proxy.timer                  proxy.service
Sat 2021-10-02 00:00:00 CST 11h left      n/a                         n/a          atop-rotate.timer            atop-rotate.servi>
Sat 2021-10-02 00:00:00 CST 11h left      Fri 2021-10-01 00:00:24 CST 12h ago      man-db.timer                 man-db.service
Sat 2021-10-02 00:00:00 CST 11h left      Fri 2021-10-01 12:00:15 CST 9min ago     proxy2.timer                 proxy2.service
Sat 2021-10-02 00:00:00 CST 11h left      Fri 2021-10-01 00:00:24 CST 12h ago      shadow.timer                 shadow.service
Sat 2021-10-02 02:47:16 CST 14h left      Fri 2021-10-01 10:43:59 CST 1h 25min ago updatedb.timer               updatedb.service
Sat 2021-10-02 10:31:22 CST 22h left      Fri 2021-10-01 10:31:22 CST 1h 38min ago systemd-tmpfiles-clean.timer systemd-tmpfiles->
Mon 2021-10-04 00:00:00 CST 2 days left   Mon 2021-09-27 00:00:09 CST 4 days ago   backup.timer                 backup.service

8 timers listed.
Pass --all to see loaded but inactive timers, too.

Last edited by kindusmith (2021-10-01 13:30:21)

Offline

#2 2021-10-01 10:45:17

lmn
Member
Registered: 2021-05-09
Posts: 67

Re: [SLOVED] I backed up my system with systemd

Don't take this the wrong way but

Do not post tutorials or "how to's": documentation belongs in the wiki, where it can be maintained.

from https://wiki.archlinux.org/title/Genera … ow_to_post.
There is already a section on system backups. If you feel something is missing please amend the section. Also note there are already a myraid of backup tools availible.

Ad (1): Backing up the whole system and storing it in a user directory is mixing two different responsibilities.
Maybe separate the backup utility out into the system proper, best done by packaging.

Ad (2): See above.

Ad (4): This should not be necessary if the systemd timer is correctly written. But good for debugging.

In your script you seem to excessively create variables which are then only used immediately once. Maybe just make those things static in the first place.
Also your systemd unit deletes all backups before a new backup is made. For a backup utility that is not ideal to put it mildly.
If your backup script fails your left without or just a partial backup. Probably not ideal.

Why do you set `RemainAfterExit=true`? Reading https://www.freedesktop.org/software/sy … rvice.html and https://www.freedesktop.org/software/sy … timer.html suggests that this will prevent your timer from running again on the same boot cycle.


You seem to want to backup your whole system whereas you could instead just backup configuration and the package list.
These can be used in conjunction to restore a system.
But this approach is online, so I can understand the hesitation.

Edit. added section about `RemainAfterExit`

Last edited by lmn (2021-10-01 11:04:56)

Offline

#3 2021-10-01 13:28:29

kindusmith
Member
Registered: 2019-12-30
Posts: 16

Re: [SLOVED] I backed up my system with systemd

Thank you for guidance, I know my solution is not the best one, but I just want to share my solution to everyone

Offline

Board footer

Powered by FluxBB