Score:0

Ubuntu Server Backup Script

co flag

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

sudodus avatar
jp flag
With the `rsync` option `--delete` I would say that it is a synchronizing script, not a backup script. Maybe you should consider `--backup` (and maybe `--backup-dir=`) too, in order to save *versions* of files, that are modified or deleted.
co flag
It's like an RAID alternative, I don't need the previous versions, neither deleted files. I just need a copy of the last functional version of the system.
sudodus avatar
jp flag
I see, then it looks good to me (but I have not tested it or looked into it in a very detailed way). I use `rsync` so I am used to running it with 'a lot of' options, and I think your options look OK. It is a good idea to start by running a dry run and look carefully at the output before running the real copying process :-)
James S. avatar
de flag
This really seems as though you are trying to take periodic snapshots of the filesystem, such that you can revert to a snapshot in the event of a disaster. Is there any reason why you can't use LVM snapshotting capabilities?
co flag
@JamesS. don't know anything about LVM... I'll take a look. Thanks
James S. avatar
de flag
There's a tool called "snapper" which may be what you are looking for: http://snapper.io/overview.html
bac0n avatar
cn flag
I think the exclude should be `--exclude={'/dev/*','/proc/*'}` you don't want any glob expantion
co flag
@JamesS. already saw that tool, but for now I just stick to the rsync, when I have some extra time I'll check the snapper and if it fits my needs, I don't need snapshots at least I think I don't... Anyway thanks for the inputs.
co flag
@bac0n thanks fot the info.
James S. avatar
de flag
I mean, what you are describing here isn't "backups" - it's snapshots.
co flag
@JamesS. okay, that's the correct term :) But the main purpose it's to only have a backup/snapshot/copy of the system when I decided to, loosing the previous one... Thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.