So, I have a backup script that cron launches every day, and creates incremental copy of my whole root (except sys, dev, tmp, swapfile etc). Each copy is stored in a separate directory:
Code: Select all
[mctom@Tomusiomat daily]$ ll
inode Permissions Size Blocks User Group Date Modified Name
7866507 drwxr-xr-x - - root root 14 lis 2021 2021-11-13_06:25:01
8524203 drwxr-xr-x - - root root 14 lis 2021 2021-11-14_18:27:22
7880564 drwxr-xr-x - - root root 14 lis 2021 2021-11-15_10:17:01
7880983 drwxr-xr-x - - root root 14 lis 2021 2021-11-16_21:47:01
7881162 drwxr-xr-x - - root root 14 lis 2021 2021-11-17_10:45:01
7881339 drwxr-xr-x - - root root 14 lis 2021 2021-11-18_09:19:01
7881514 drwxr-xr-x - - root root 14 lis 2021 2021-11-19_09:22:01
7881691 drwxr-xr-x - - root root 14 lis 2021 2021-11-20_12:48:01
7881869 drwxr-xr-x - - root root 14 lis 2021 2021-11-21_12:09:01
7882044 drwxr-xr-x - - root root 21 lis 2021 2021-11-22_08:49:01
7885328 drwxr-xr-x - - root root 21 lis 2021 2021-11-23_08:24:02
7886174 drwxr-xr-x - - root root 21 lis 2021 2021-11-24_10:26:01
7889336 drwxr-xr-x - - root root 21 lis 2021 2021-11-25_09:33:01
I'd like to add to my bash script some code that deletes directories older than 1 month, that were not created on Wednesday. This way I'll keep just weekly copies of really old stuff.
The problem is, the script has to extract date from directory name, determine whether that's friday and more than 30 days ago, and then decide whether to keep it or not.
Any tips will be appreciated!
And here's the full script for the curious:
Code: Select all
[mctom@Tomusiomat cron.daily]$ cat backup
#!/bin/bash
# A script to perform incremental backups using rsync
# Logging
exec > /tmp/backup.logfile 2>&1
set -x
set -o errexit
set -o nounset
set -o pipefail
readonly BACKUP_UUID="1ebac215-0666-baca-0000-0000000000ad"
readonly BACKUP_MOUNT_DIR="/mnt/backup"
readonly SOURCE_DIR="/"
readonly BACKUP_DIR="${BACKUP_MOUNT_DIR}/daily"
readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
readonly LATEST_LINK="${BACKUP_DIR}/latest"
# unmount in case it is currently mounted somewhere
umount /dev/disk/by-uuid/${BACKUP_UUID} || true
# mount
mkdir -p ${BACKUP_MOUNT_DIR}
mount /dev/disk/by-uuid/${BACKUP_UUID} ${BACKUP_MOUNT_DIR}
mkdir -p "${BACKUP_DIR}"
nice -n15 rsync -av --delete \
"${SOURCE_DIR}/" \
--link-dest "${LATEST_LINK}" \
--exclude=".cache" \
--exclude="/dev" \
--exclude="/media" \
--exclude="/mnt" \
--exclude="/mount" \
--exclude="/proc" \
--exclude="/run" \
--exclude="/sys" \
--exclude="/tmp" \
--exclude="/var" \
--exclude="/home/swapfile" \
--exclude="/home/mctom/Downloads" \
"${BACKUP_PATH}"
rm -rf "${LATEST_LINK}"
ln -s "${BACKUP_PATH}" "${LATEST_LINK}"
mv /tmp/backup.logfile ${BACKUP_DIR}/backup.log
umount ${BACKUP_MOUNT_DIR}