Check disk usage before running scheduled Virtualmin backup

Hey all,

I’m running Virtualmin on a VPS with 200G disk space with Virtualmin scheduled backups set to one weekly backup on Sunday, and daily Mon-Sat backups with 4 days retention:

Destination	                                    Backup contents         Description	            Backup enabled?	                 Level

Local file /backup/%Y-%m-%d_%H-%M_incremental 	All virtual servers 	All-accs-incremental 	Yes, at cron time 0 3 * * 1-6    Incremental 	
Local file /backup/%Y-%m-%d_%H-%M_FULL 	        All virtual servers 	All-accs-full 	        Yes, weekly (on Sunday) 	     Full

So far, this schedule has been working pretty well until I archived some old emails which filled up the whole disk during incremental backup generation. Temporary fix was to exclude “*.bak.tar.gz” files in backups.

Having a separate backup partition or remote destination would be a better solution, but for local backups, I was wondering if there is a way to “fool-proof” this by running some sort of a pre-check that would prevent backup from being generated if there isn’t enough space on the disk available.

For example:

  • Run a dry run backup or similar to get the estimated backup size
  • Compared the given size with current available disk/partition space
  • Start the scheduled backup only if there is enough disk space

Or a more simple way, start the backup only if disk use percentage doesn’t exceed the given value.
There is ‘Command to run before backup’ option in Virtualmin >> Backup and Restore >> Scheduled Backups section, could we put an if statement there which would prevent the backup process from continuing?
The command below is an idea to prevent the backup if more than 60% disk is used, besides the obvious last important part:

if [[ $(df -h / | awk '{print $5}' | grep -Eo '[0-9]{1,4}') -gt 60 ]]; then echo "Backup failed - Disk space insufficient" | sendmail admin@example.com; command-to-interrupt-backup; fi

Any advice or ideas are welcome. Thanks!

SYSTEM INFORMATION
OS type and version AlmaLinux Linux 8.5
Webmin version 1.990
Virtualmin version 6.17
1 Like

backing up to the same disk your data is on isn’t a backup. Virtualmin has a great backup system to send the backups everywhere off server.

1 Like

This is a very good idea. I’ve been stung many times by a backup using all of the disk space whilst archiving, resulting in the database service crashing.

1 Like

Success! Putting this as a pre-backup command worked for me:

disk_use_percent=$(df / | awk '{print $5}' | grep -Eo '[0-9]{1,4}'); if [[ ${disk_use_percent} -gt 60 ]]; then echo "Backup failed - Disk space insufficient (${disk_use_percent})" | sendmail admin@example.com; exit 1; done

The pre-backup command will simply exit with error code(1) if disk usage is higher than 60% and interrupt the backup process.

In case you have “Only send email on failure” enabled, this should work so you don’t get two emails:

disk_use_percent=$(df / | awk '{print $5}' | grep -Eo '[0-9]{1,4}'); if [[ ${disk_use_percent} -gt 60 ]]; then echo "Backup failed - Disk space insufficient (${disk_use_percent})"; exit 1; done

This should also work with a separate partition ( e.g.use df /backup instead of df /h) or with additional tweaking, for a remote destination as well if you use SSH.

1 Like

@ElVee - this might be better raised on github.

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.