Compare commits
3
Commits
09a4d6485f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8fe5a19fc | ||
|
|
f859b776a2 | ||
|
|
087983bd4a |
+5
-6
@@ -25,7 +25,6 @@ main() {
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# Helper functions
|
|
||||||
lxc() { command "$cmd" "$@"; }
|
lxc() { command "$cmd" "$@"; }
|
||||||
list_vms() { lxc list -c n -f csv; }
|
list_vms() { lxc list -c n -f csv; }
|
||||||
|
|
||||||
@@ -53,9 +52,8 @@ Options:
|
|||||||
-d Directory where backups will be stored (default: current directory)
|
-d Directory where backups will be stored (default: current directory)
|
||||||
-t Manager type: lxc or incus (default: lxc)
|
-t Manager type: lxc or incus (default: lxc)
|
||||||
-x Compression algorithm (e.g. gzip, zstd, none)
|
-x Compression algorithm (e.g. gzip, zstd, none)
|
||||||
-c Cleanup old backups keeping a set number (default: 2)
|
-c Cleanup old backups and keep last <n> (default: keep all)
|
||||||
-h Show this message
|
-h Show this message
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
@@ -66,14 +64,13 @@ start_backup() {
|
|||||||
local compression="$3"
|
local compression="$3"
|
||||||
local ext=".${compression}"
|
local ext=".${compression}"
|
||||||
|
|
||||||
if [[ "$compression" == 'none' ]]; then
|
if [[ "$compression" == 'none' ]]; then
|
||||||
ext=""
|
ext=""
|
||||||
else
|
else
|
||||||
command -v "$compression" >/dev/null 2>&1 || { echo "$compression command not found"; exit 1; }
|
command -v "$compression" >/dev/null 2>&1 || { echo "$compression command not found"; exit 1; }
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local vm_dir="${backup_dir}/${vm}"
|
local vm_dir="${backup_dir}/${vm}"
|
||||||
|
|
||||||
local backup_name
|
local backup_name
|
||||||
backup_name="${vm}_$(date +"%Y-%m-%d_%H-%M-%S").tar${ext}"
|
backup_name="${vm}_$(date +"%Y-%m-%d_%H-%M-%S").tar${ext}"
|
||||||
|
|
||||||
@@ -89,10 +86,12 @@ prune_backups() {
|
|||||||
local backup_dir="$2"
|
local backup_dir="$2"
|
||||||
local retention="$3"
|
local retention="$3"
|
||||||
mapfile -t backup_files < <(find "${backup_dir}/${vm}" -maxdepth 1 -type f -printf '%T@ %p\n' | sort -n | head -n "-${retention}" | cut -d' ' -f2-)
|
mapfile -t backup_files < <(find "${backup_dir}/${vm}" -maxdepth 1 -type f -printf '%T@ %p\n' | sort -n | head -n "-${retention}" | cut -d' ' -f2-)
|
||||||
|
|
||||||
if [[ ${#backup_files[@]} -gt 0 ]]; then
|
if [[ ${#backup_files[@]} -gt 0 ]]; then
|
||||||
echo "Removing ${#backup_files[@]} old backup files for $vm (keeping last $retention files)"
|
echo "Removing ${#backup_files[@]} old backup files for $vm (keeping last $retention files)"
|
||||||
printf '%s\0' "${backup_files[@]}" | xargs -0 rm -fv > /dev/null
|
printf '%s\0' "${backup_files[@]}" | xargs -0 rm -fv > /dev/null
|
||||||
|
elif [[ "${retention}" -eq 0 ]]; then
|
||||||
|
echo "Keeping all existing backups for $vm"
|
||||||
else
|
else
|
||||||
echo "No old backups to remove for $vm"
|
echo "No old backups to remove for $vm"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in new issue
Block a user