Go Premium for a chance to win a PS4. Enter to Win


Write linux script for automated backup and restore

Posted on 2010-09-04
Medium Priority
Last Modified: 2013-12-15

I have fedora 13 64bit on a sata 320 GB hard disk (sda). I have a second 512 GB sata hard disk sdb.

I want to partition sdb in such way that I have about 2 50 gb partitions of which one is ntfs for windows installation (mounted on fedora as /media/cdrive ) and other 50 gb for linux use from fedora. The remaining space 412 GB is used for backup as detailed below.
LVM scheme can be used on sdb. Lets call this 412 GB physical partition as sdb3.

Please write a backup script that would achieve the following tasks:

1) Runs automatically every night at 3 am on fedora if fedora machine is on.
2) Makes bit for bit disk image or snapshot of entire /dev/sda used space ( not the free space on /dev/sda) and save on sdb3 disk.

3) Must do some type of compression like gzip.

4) The program should backup in such a way that sdb3 contains disk images of most recent 5 days ( 5 disk images). That means on day 6, the disk image that was created on day 1 is overwritten and on day 7 , disk image that was created on day 2 is overwritten and so on. Should also account for days in which power went out or simply the system was off and skip that day automatically.

5) This program must be reliable to withstand sudden power outage in case that happens while its running.

Please write a restore script that would achieve the following tasks:

In case sda is fried up, I install a new hard disk in its place and boot the system using any linux live cd like fedora or ubuntu. From this live cd I access and run the restore script.
Restore script should achive the following:

1) I can choose which disk image ( of the 5 images in sdb3)  to use as the source for restore
2) sda now should have exactly same partions/logical volumes and data at the time the backup program above created
the disk image.
3) after this I remove the live cd and boot the system and should be able to boot to fedora.

Question by:zenguru
  • 4
  • 2
  • 2
  • +4
LVL 35

Expert Comment

by:Duncan Roe
ID: 33605169
Is this homework?

Author Comment

ID: 33609262
No its not.

Expert Comment

ID: 33609606
so you better take a dedicated backup product like veritas Netbackup (symantech).
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  


Author Comment

ID: 33612419
Please provide the solution exactly as described instead pointing me to enterprise products costing $1000s. I dont need that for this purpose as you should know.

I expect some one who is expert in this subject matter ( an expert having extensive experience
in linux backup and restore -- if he did not extensive experience he would not be called
expert) in 15 minutes or less or he may already have such a script that needs some editing
to achieve the solution to the problem.

Please do not post comment unless you have extensive experience in linux based backup and restore.

Expert Comment

ID: 33619087
Read through some of your other posts too. What an attitude !
Now good luck with your answers.
LVL 18

Expert Comment

ID: 33621525

Look at Bacula. It's a free backup program that's very powerful.

Ref: http://www.bacula.org/en/

Regards, Tobias

Author Comment

ID: 33622214
about bacula:  its from bacula website:

 However, if you are new to Unix systems or do not have offsetting experience with a sophisticated backup package, the Bacula project does not recommend using Bacula as it is much more difficult to setup and use than tar or dump.

so it does not suit my needs. Thanks anyway. Again please do not point me to complex enterprise programs. I just need that linux script which will meet the conditions given.

LVL 14

Expert Comment

ID: 33628364
Dear zenguru,

You should probably ask a bit more specific question and not ask people to do all of your work for you. And also not bash people if they recommend other solutions then the ones you had in mind. (IMHO)

But anyway...

"A simple script" - if you want to do a bit-wise comparison there are no simple things available. If you are OK with a copy of the data, then your script might look like this:

(see code)

I haven't tested this at all, it's up to you...

DATE=$(date %Y%m%d)
mkdir -p ${TARGET}/${DATE}
mkdir -p ${SNAP}

# main backup
rsync -avz /source/* /${TARGET}/${DATE}/

# do "snapshot" if successful

if [ $? -eq 0 ]; then
tar -zcf ${SNAP}/${DATE}.tar.gz ${TARGET}/${DATE}
echo "didnt work"

Open in new window

LVL 25

Expert Comment

ID: 33644205
you can use multiple tools to image/backup linux system  in my datacenter, i use the following:
1- use Mondo Rescue to clone linux systems:

2- You can use dd also. dd if=/dev/somedisk of=/dev/somedestination

3- rsync scripting

4- netcat
On the source server: (cd / && tar -cvf - ) | nc 2000
On the destination:: nc -l -p 2000 | tar -xvf -

5- http://sourceforge.net/projects/g4l/

6- http://www.bacula.org/en/

7- http://clonezilla.org/

8- ibm tools
IBM® Tivoli® Provisioning Manager for OS Deployment helps organizations reduce the time and costs associated with desk-side support and direct interaction with target systems.

IBM Tivoli® Storage Manager 6 provides a wide range of storage management capabilities from a single point of control, helping companies ride the information tidal wave.
LVL 25

Accepted Solution

madunix earned 2000 total points
ID: 33644216
as said rsync is a good tool to build a relation between master and slave, for example my qmail server master and slave are replucated by
copying  these directories:
- - /home/vpopmail/
- - /var/qmail/control/
- - /var/qmail/alias
and other directories that exists in both the servers and needs to be replicated, using rsync command:
rsync -avz -e ssh -R /home/vpopmail user@host:/home/rsync/backupdir

on the other-hand it depends on you application, If the files and file systems are too heavy I/O _very_ busy or locked by applications copy/ftp/rsync... commands are not useful specially for DB and it is better to use mirror/shadow/standby features of database.

regarding rsync i have created my own script to move data, feel free to change it
the script i have tested on multiple Linux server RHEL, Centos

#  This script uses Rsync the backup files
#  to a remote server. To use this script
#  you first have to setup ssh to use keys.
#  This elimates the need for a password but
#  still provides security I have ssh on
#  a non standard port so I had to specify
#  the port (ssh -p 2998).
#  Simply change the variables list below
#  to match your specifications.
#  The Script also logs the transfer time.
#  For automated backup add script to crontab.
# Setup Variables
#The user you created with ssh_keys
STARTTIME=$(date +"%s")
# Verfiy the last rsync is not still running
if [ -f $LOCKFILE ]; then
ERRORTIME=$(date +"%d/%b/%Y:%H:%M:%S %Z")
echo "[$ERRORTIME] WARNING rsyncbackup.lock exists, aborting..." >> $LOGFILE
exit 1
# If it finished, create a new lock file
# The rsync command (for explanation type rsync --help)
rsync -aqrpogtu --delete --bwlimit=512 --rsh='ssh -p 22' $SOURCEPATH $DESTUSER@$DESTHOST:$DESTPATH 2>&1 >> $LOGFILE
# Log when the script was run
DONE=$(date +"%d/%b/%Y:%H:%M:%S %Z")
# Log how long it took the script to run
STOPTIME=$(date +"%s")
echo "[$DONE]finished: - Tranfer took:$(((SECONDS/60)/60)) hours $(((SECONDS/60)%60)) minutes $((SECONDS%60)) seconds" >> $LOGFILE
# Remove lock file

Open in new window


Author Comment

ID: 33653076
Thanks for ur post madunix.  Does rsynch creates compressed disk image of the source drive and does it include bit for bit copy including partitions and MBR ( master boot record)  while source is live and running ( not offline mode ) ?
LVL 18

Expert Comment

ID: 33654568
>Does rsynch creates compressed disk image


>does it include bit for bit copy including partitions and MBR


rsync copies files just as they are. No compression are made to the destination file, no partition copy are available.

Regards, Tobias

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question