LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2017, 09:49 PM   #1
TheEzekielProject
Member
 
Registered: Dec 2016
Distribution: arch
Posts: 668

Rep: Reputation: 190Reputation: 190
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!

Last edited by TheEzekielProject; 05-13-2017 at 09:58 PM.
 
Old 05-13-2017, 10:17 PM   #2
TheEzekielProject
Member
 
Registered: Dec 2016
Distribution: arch
Posts: 668

Original Poster
Rep: Reputation: 190Reputation: 190
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

Last edited by TheEzekielProject; 05-13-2017 at 10:26 PM.
 
Old 05-19-2017, 12:02 AM   #3
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
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.
 
Old 05-19-2017, 12:46 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Backup Script Help..............! Greenberet88x Programming 5 09-24-2009 07:17 AM
Bash - backup script - need help with my if-then-else :D fruitwerks Programming 12 04-11-2009 01:34 PM
bash backup script kristof_v Programming 4 07-24-2007 07:33 PM
Need help with a bash script that does a backup jamtech Programming 8 06-07-2007 11:36 PM
Backup script in bash gauge73 Programming 13 10-17-2005 06:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration