Backup With Systemd and Rsync

Tumbleweed comes already with two vendor backup services and I added a system one for /home:

3400G:~ # systemctl list-unit-files backup*
UNIT FILE                STATE   VENDOR PRESET
backup-home.service      static  -            
backup-rpmdb.service     static  -            
backup-sysconfig.service static  -            
backup-home.timer        enabled disabled     
backup-rpmdb.timer       enabled enabled      
backup-sysconfig.timer   enabled enabled      

6 unit files listed.
3400G:~ # 

1. Prerequisites:

Backup drive is mounted through /etc/fstab:

3400G:~ # grep /HDD /etc/fstab 
UUID=08fb3e4e-133d-4b2d-96a0-0a1e0a3381d8  /HDD                    ext4   noauto                        0  0
3400G:~ # 

systemd generates a service:


3400G:~ # systemctl cat HDD.mount 
# /run/systemd/generator/HDD.mount
# Automatically generated by systemd-fstab-generator

[Unit]
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
SourcePath=/etc/fstab
After=blockdev@dev-disk-by\x2duuid-08fb3e4e\x2d133d\x2d4b2d\x2d96a0\x2d0a1e0a3381d8.target

[Mount]
Where=/HDD
What=/dev/disk/by-uuid/08fb3e4e-133d-4b2d-96a0-0a1e0a3381d8
Type=ext4
Options=noauto
3400G:~ # 

2. Actions performed:

Create system units for backup of /home:

3400G:~ # systemctl cat backup-home*        
# /etc/systemd/system/backup-home.timer 
[Unit] 
Description=Backup of /home 
After=local-fs.target 

[Timer] 
OnCalendar=daily 
AccuracySec=1m 
Persistent=true 

[Install] 
WantedBy=timers.target 

# /etc/systemd/system/backup-home.service 
[Unit] 
Description=Backup /home 
Requires=HDD.mount 
After=HDD.mount 
After=local-fs.target 

[Service] 
Type=oneshot 
ExecStart=/usr/bin/rsync -a --exclude=.cache /home/ /HDD/ 
3400G:~ # 

Enable timer:

3400G:~ # systemctl list-unit-files backup-home*
UNIT FILE           STATE   VENDOR PRESET
backup-home.service static  -            
backup-home.timer   enabled disabled     

2 unit files listed.
3400G:~ # systemctl list-units backup-home*
  UNIT              LOAD   ACTIVE SUB     DESCRIPTION    
  backup-home.timer loaded active waiting Backup of /home

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
3400G:~ # 

3. Monitor backup activities

Heavy activity due to btrfs balance, defrag, scrub and trim maintenance and backup services running at the same time:

3400G:~ # systemd-analyze blame|head -11
1min 52.602s backup-home.service                            
1min 51.698s mlocate.service                                
     10.930s backup-rpmdb.service                           
       907ms dracut-pre-udev.service                        
       747ms mandb.service                                  
       720ms display-manager.service                        
       652ms dracut-initqueue.service                       
       479ms postfix.service                                
       349ms initrd-switch-root.service                     
       213ms HDD.mount                                      
       177ms initrd-parse-etc.service                       
3400G:~ # 

However display manager is ready for login at 1.6 seconds in userspace:


3400G:~ # systemd-analyze critical-chain display-manager.service
The time when unit became active or started is printed after the "@" character.
The time the unit took to start is printed after the "+" character.

display-manager.service +720ms
└─apache2.service @733ms +165ms
  └─time-sync.target @715ms
    └─chronyd.service @639ms +74ms
      └─nss-lookup.target @637ms
        └─systemd-resolved.service @460ms +174ms
          └─systemd-networkd.service @405ms +51ms
            └─systemd-udevd.service @315ms +88ms
              └─systemd-tmpfiles-setup-dev.service @290ms +10ms
                └─kmod-static-nodes.service @254ms +23ms
                  └─systemd-journald.socket
                    └─-.mount
                      └─system.slice
                        └─-.slice
3400G:~ # 

The machine stays responsive during startup and users won’t even notice the background activity.