Need some help to tune a script to perform backups of my system.
So I have a server 20.04 up and running, every now and then, when I make some updates/changes it breaks something, and I loose some time to correct the problems.
At first I thought in using RAID 1, but then I realized if i break the original the spare it'll have the same problem.
So after some thinking I made a dd copy of the original disk (#1) to another one (#2), and leave the #2 connected but not mounted.
At first I thought in leaving the disk disconnected and with help of a bootable media, I perform a new dd backup... But that doesn't seem very professional. So I decided to make a script that, mount the #2 hd, call rsync and in the end unmount
Will this work? The disk it's a complete sector by sector copy, and after this point, will have the changes of #1 reflected in the #2 with rsync.
I don't have any experience in building shell scripts, so I need help to tune mine.
I need to mount the #2, check if it's really mounted (check if mount folder is not empty?), run rsync and write the complete log to a file, and in the end unmount.
This is what I have now:
#! /bin/bash
# -- VARS
MOUNT_DEV='/dev/sdb2'
MOUNT_FOLDER='/mnt/hdd_backup'
SOURCE='/*'
DESTINATION='/mnt/hdd_backup/'
LOG_FILE='/home/jnap/external/rsync.log'
echo 'Mount the backup hdd'
mount $MOUNT_DEV $MOUNT_FOLDER
echo 'Mount finished'
#Check if it's really mounted? dir is not empty?
echo 'Run Rsync - DryRun only to test'
rsync -aAHXv --dry-run --delete $SOURCE $DESTINATION --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/snap,/home/jnap/external/*,var/lib/lxcfs/*} > $LOG_FILE
echo 'Finished Rsync'
echo 'Unmount the backup hdd'
mount -l $MOUNT_FOLDER
echo 'Unmount finished'
At first this is a manual job.
But later I'm thinking in create a cron job for it, maybe run once a week during the weekend.
Thanks