Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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).
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.


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.


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 -




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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

722 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