sqlite3 /var/lib/pve-cluster/config.db .dump > ~/config.dump.$(date --utc +%Z%Y%m%d%H%M%S).sql
.service
)The service file specifies the command to be executed. Create the file in /etc/systemd/system/
. You can name it backup-sqlite.service
:
sudo nano /etc/systemd/system/backup-sqlite.service
The content of the file should be:
[Unit]
Description=Backup sqlite database of Proxmox
[Service]
Type=oneshot
ExecStart=/bin/bash -c "/usr/bin/sqlite3 /var/lib/pve-cluster/config.db .dump > /backup/config.dump.$(date --utc +%Y%m%d%H%M%S).sql"
# ExecStartPost runs after ExecStart, and this will remove older backups, keeping only the 35 most recent ones.
ExecStartPost=/bin/bash -c "ls -tp /backup/config.dump.*.sql | grep -v '/$' | tail -n +36 | xargs -I {} rm -- {}"
This service will execute the backup and then clean up older backup files, keeping only the latest 35.
Type=oneshot
: Specifies that this service runs a single task and then terminates.ExecStart
: Specifies the actual command to execute.ExecStartPost
: This script will run right after the first one and will delete the oldest copies (only the newest 35 copies will be left).timer
)The timer file defines when to execute the service. For instance, if you want to run the backup every day at 02:00, create a file called backup-sqlite.timer
:
sudo nano /etc/systemd/system/backup-sqlite.timer
The content should be:
[Unit]
Description=Timer to run the SQLite database backup
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target
OnCalendar=*-*-* 02:00:00
: This runs the service every day at 02:00.Persistent=true
: Ensures the task will run at the next boot if the system was powered off when it was scheduled to run.Once the service and timer files are in place, enable and start the timer.
Reload systemd configuration:
sudo systemctl daemon-reload
Enable the timer to start on boot:
sudo systemctl enable backup-sqlite.timer
Start the timer:
sudo systemctl start backup-sqlite.timer
To check the timer's status:
systemctl status backup-sqlite.timer
To view all active timers:
systemctl list-timers
.service
file defines the actual task: the SQLite dump command, which creates a timestamped backup of the Proxmox configuration..timer
file controls when the task runs. In this example, it will execute the task every day at 2 AM.Persistent=true
option ensures that the task runs even if the system was off during the scheduled time.This method allows you to automatically back up the SQLite database using systemd
timers, ensuring that the task runs periodically without the need for manual intervention or cron jobs.