LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash system backup script help (https://www.linuxquestions.org/questions/programming-9/bash-system-backup-script-help-4175605889/)

TheEzekielProject 05-13-2017 09:49 PM

Bash system backup script help
 
Hello all,

I'm looking for someone with a little more experience in scripting than I have to look over this script and confirm that it will do what I expect. It is for backing up my system

Code:

#! /bin/bash
if grep -qs '/media/3tb' /proc/mounts; then
    mkdir "/media/3tb/.sysbak/$(date +'%d%b%Y')"; rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/home"} / /$_
else
    : 
fi

The script is supposed to check that my backup drive is mounted and if it is, make a directory in the specified directory appending the days date, then rsync my system to the created directory. If the backup device is not mounted the script just exits

Any edits and/or suggestions appreciated!

TheEzekielProject 05-13-2017 10:17 PM

After thinking about it some more, I'm thinking the destination directory should be written as just "$_", and I can drop the "v" option to rsync since it's a script

Beryllos 05-19-2017 12:02 AM

It wouldn't hurt to just run it and see what happens. It looks all right, but here are a few minor suggestions:

Rather than test whether /media/3tb is mounted, you could test whether /media/3tb/.sysbak exists.

You could use newlines instead of semicolons. It's more readable, or am I missing the reason for semicolons?

You don't need the mkdir. You can get rsync to create the directory with:
Code:

rsync -aAX --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/home"} / /media/3tb/.sysbak/$(date +'%d%b%Y')
date +%F will be useful if you ever have to sort backup sets by name (alphabetically) rather than by timestamp. Then, for example, a simple 'ls' would list the backup sets in chronological order.

You don't need the else block, unless you plan to add functionality there later.

When backing up a live system, rsync is likely to generate non-fatal error messages as files are modified or deleted while rsync is running. Redirect stderr to a logfile if you want to examine the error messages, or to /dev/null if you want to ignore them.

If you plan to back up often, consider using the rsync --link-dest option. It will save a lot of disk space by using hardlinks instead of duplicating unchanged files. It requires a destination filesystem which supports hardlinks. For optimum efficiency of disk space, the script would need to identify the most recent backup which ran to completion (that is, which was not interrupted), and use that as the --link-dest directory.

MadeInGermany 05-19-2017 12:46 AM

Perhaps you want to ensure that the mkdir was successful then you can chain the next command with a &&
Code:

if grep -qs '/media/3tb' /proc/mounts; then
    mkdir "/media/3tb/.sysbak/$(date +'%d%b%Y')" &&
    rsync ...
fi

rsync -H option also keeps hardlinks!
rsync creates many files. If you want to keep several backups then tar or cpio or dump archives would be more efficient.


All times are GMT -5. The time now is 09:11 PM.