Sie sind auf Seite 1von 10

Creating backup/restore images using dd

Create a hard disk image: dd if=/dev/hda1 of=/home/hda1.bin


Create a compressed disk image: dd if=/dev/hda1 | gzip > /home/hda1.bin.gz
Back up the MBR: dd if=/dev/hda of=/home/hda.boot.mbr bs=512 count=1
Restore MBR (from a Live CD): dd if=/mnt/hda1/home/hda.boot.mbr of=/dev/hda bs=512
count=1
Backup a drive to another drive: dd if=/dev/hda of=/dev/hdb conv=noerror,sync bs=4k
The command:
dd -if /dev/hda1 > partitionimage.dd
will backup "/dev/hda1" partition. A whole drive (including the MBR) could be backed up using
just /dev/hda as the input "file". Restoring is done by: dd -if partitionimage.dd -of /dev/hda1
If you have a complete new harddrive and want to restore the backup (or copy your old system to
the new drive). First, the new drive has to be bigger or exactly the same size as the old one. First
go superuser and switch to runlevel 1 so that you can fumble around with the harddisk without
other services interfering
restore either the whole disk to the new drive or one partition (depending on how you made the
backup): dd -if partitionimage.dd -of /dev/hda1
If you restored the whole drive (/dev/hda), the system will not automatically create the devices
(/dev/hda1, /dev/hda2) if you just restored the whole drive. If you know how to make the devices
show up without reboot, write it here, otherwise this is a good moment to reboot.
If you restored the system to a new drive, and your device names changed (for example from
/dev/hda to /dev/sda) then you must adapt the bootloader and the mount points. While still on
runlevel 1, edit these files:
/boot/grub/menu.list
/etc/fstab
After your system is able to boot and runs again, you can resize your partitions to fill the rest of
the new harddisk (if you want that) as described

Disk usage
This script will be useful to analyze the disk usage and if the reported disk space is more than 90
% an email will be sent to the administrator. The script is taken from here.
#!/bin/sh
# set -x
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free available) percentage of space
is >= 90%.
# ------------------------------------------------------------------------# Set admin email so that you can get email.
ADMIN="root"
# set alert level 90% is default
ALERT=90
# Exclude list of unwanted monitoring, if several partions then use "|" to
separate the partitions.
# An example: EXCLUDE_LIST="/dev/hdd1|/dev/hdc5"
EXCLUDE_LIST="/auto/ripper"
#
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::
#
function main_prog() {
while read output;
do
#echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
partition=$(echo $output | awk '{print $2}')
if [ $usep -ge $ALERT ] ; then
echo "Running out of space \"$partition ($usep%)\" on server $(hostname),
$(date)" | \
mail -s "Alert: Almost out of disk space $usep%" $ADMIN
fi
done
}
if [ "$EXCLUDE_LIST" != "" ] ; then
df -H | grep -vE "^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5
" " $6}' | main_prog
else
df -H | grep -vE "^Filesystem|tmpfs|cdrom" | awk '{print $5 " " $6}' |
main_prog
fi

Incremental Backup Scripts


This script will do the incremental backup into an external mounted hard-drive. It is to take a
backup of the /home directory. However, it can be modified to suit the requirements. The script is
taken from here.
#!/bin/bash
# ---------------------------------------------------------------------# mikes handy rotating-filesystem-snapshot utility
# ---------------------------------------------------------------------# this needs to be a lot more general, but the basic idea is it makes
# rotating backup-snapshots of /home whenever called
# ---------------------------------------------------------------------unset PATH

# suggestion from H. Milz: avoid accidental use of $PATH

# ------------- system commands used by this script -------------------ID=/usr/bin/id;


ECHO=/bin/echo;
MOUNT=/bin/mount;
RM=/bin/rm;
MV=/bin/mv;
CP=/bin/cp;
TOUCH=/bin/touch;
RSYNC=/usr/bin/rsync;
# ------------- file locations ----------------------------------------MOUNT_DEVICE=/dev/hdb1;
SNAPSHOT_RW=/root/snapshot;
EXCLUDES=/usr/local/etc/backup_exclude;
# ------------- the script itself -------------------------------------# make sure we're running as root
if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root.
} fi

Exiting..."; exit;

# attempt to remount the RW mount point as RW; else abort


$MOUNT -o remount,rw $MOUNT_DEVICE $SNAPSHOT_RW ;
if (( $? )); then
{
$ECHO "snapshot: could not remount $SNAPSHOT_RW readwrite";
exit;
}
fi;
# rotating snapshots of /home (fixme: this should be more general)
# step 1: delete the oldest snapshot, if it exists:

if [ -d $SNAPSHOT_RW/home/hourly.3 ] ; then
$RM -rf $SNAPSHOT_RW/home/hourly.3 ;
fi ;

\
\

# step 2: shift the middle snapshots(s) back by one, if they exist


if [ -d $SNAPSHOT_RW/home/hourly.2 ] ; then
\
$MV $SNAPSHOT_RW/home/hourly.2 $SNAPSHOT_RW/home/hourly.3 ; \
fi;
if [ -d $SNAPSHOT_RW/home/hourly.1 ] ; then
\
$MV $SNAPSHOT_RW/home/hourly.1 $SNAPSHOT_RW/home/hourly.2 ; \
fi;
# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot,
# if that exists
if [ -d $SNAPSHOT_RW/home/hourly.0 ] ; then
\
$CP -al $SNAPSHOT_RW/home/hourly.0 $SNAPSHOT_RW/home/hourly.1 ; \
fi;
# step 4: rsync from the system into the latest snapshot (notice that
# rsync behaves like cp --remove-destination by default, so the destination
# is unlinked first. If it were not so, this would copy over the other
# snapshot(s) too!
$RSYNC
\
-va --delete --delete-excluded
\
--exclude-from="$EXCLUDES"
\
/home/ $SNAPSHOT_RW/home/hourly.0 ;
# step 5: update the mtime of hourly.0 to reflect the snapshot time
$TOUCH $SNAPSHOT_RW/home/hourly.0 ;
# and thats it for home.
# now remount the RW snapshot mountpoint as readonly
$MOUNT -o remount,ro $MOUNT_DEVICE $SNAPSHOT_RW ;
if (( $? )); then
{
$ECHO "snapshot: could not remount $SNAPSHOT_RW readonly";
exit;
} fi;

High CPU Usage Script


4

At times, we need to monitor the high CPU usage in the system. We can use the below script to
monitor the high CPU usage. The script is taken from here.
#!/bin/bash
while [ true ] ;do
used=`free -m |awk 'NR==3 {print $4}'`
if [ $used -lt 1000 ] && [ $used -gt 800 ]; then
echo "Free memory is below 1000MB. Possible memory leak!!!" | /bin/mail -s
"HIGH MEMORY ALERT!!!" user@mydomain.com
fi
sleep 5
done

Adding new users to a Linux system


This script allows the root user or admin to add new users to the system in an easier way by just
typing the user name and password (The password is entered in an encrypted manner). The
below script is taken from here.
#!/bin/bash
# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed
to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi

Database Backup
This script is a pretty basic script useful in backing up the database. The script is taken from
here.
#!/bin/sh
now="$(date +'%d_%m_%Y_%H_%M_%S')"
filename="db_backup_$now".gz
backupfolder="/var/www/vhosts/example.com/httpdocs/backups"
fullpathbackupfile="$backupfolder/$filename"
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt

echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"


mysqldump --user=mydbuser--password=mypass --default-character-set=utf8
mydatabase | gzip > "$fullpathbackupfile"
echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
chown myuser "$fullpathbackupfile"
chown myuser "$logfile"
echo "file permission changed" >> "$logfile"
find "$backupfolder" -name db_backup_* -mtime +8 -exec rm {} \;
echo "old files deleted" >> "$logfile"
echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "*****************" >> "$logfile"
exit 0

BACKUP and RESTORE


==================
make a bootable copy of a disk onto a second disk and

then copy / from /dev/sda1 to /dev/sdb1


# copy partitioning from sda to sdb
sfdisk -d /dev/sda | sfdisk /dev/sdb
# make file systems on new disk (sdb):
mkswap /dev/sdb2
mkfs -t ext4 /dev/sdb1
# mount backup disk (sdb) at /sdb1
mkdir /sdb1
mount /dev/sdb1 /sdb1
# dump from / (sda1) to sdb1 mounted at /sdb1
cd /sdb1
/sbin/dump -z -0 -f - / | restore -rf BACKUP
======
dump to another disk in the computer (or could be remote if you like):
cd /
/sbin/dump -0 -f /backup/hda2.dump / 2>>/var/log/manual_backup_os
in the above / is /dev/hda2 and swap and /dev/hda1.
/backup is /dev/hdd2.
backup partition info to another disk:
sfdisk -d /dev/hda > /backup/partition_info
sfdisk -l /dev/hda >> /backup/partition_info
Can schedule a regular(ish) dump using cron (maybe once per month?)
You might also want a copy of sfdisk (not always on boot CDs)
cp /sbin/sfdisk /backup
OS used for these examples is Suse9.1 Pro.
RESTORE
=======
disk dead and need to restore OS, so a new disk is fitted:
boot from cd/floppy (Knoppix/Tom's/Suse install CD)
mkdir /backup
mount -t ext3 /dev/hdd2 /backup
cd /backup
Now to partition new disk:
cat partition_info | sfdisk /dev/hda
or
fdisk /dev/hda
and do it manually.
Make file systems on new disk:
mkswap /dev/hda1

mkfs -t ext3 /dev/hda2


mount /dev/hda2 /hda2
cd /hda2
restore rf /backup/hda2.dump
Note: you may need to edit:
/etc/fstab
/boot/grub/menu.lst
if you are restoring to a different partition setup.
Should have a bootable system disk, now we need to configure grub to boot it:
boot from grub floppy/cdrom (boot restored / on /dev/hda2 which is hd(0,1) to
grub)
(link at bottom of page to example grub iso boot CD image) or a Linux boot CD
like knoppix
You might need to install grub:
grub-install /dev/hda
or
grub-install /dev/sda
if initrd and vmlinuz are in /boot, otherwise you will need:
grub-install --root-directory=/mnt /dev/sda
If you are not sure which partition actually holds this directory, use the
command
find, like this:
grub
grub> find /boot/grub/stage1
Define the root device (boot partition)
grub> root (hd0,1)
Once you've set the root device correctly, run the command setup (see setup):
grub> setup (hd0)
Now try booting your newly recovered system disk and it should work.
:)

Backing
Up and
Restore
Your
Server

Backup Your Server's OS


Making a backup of your Linux Operating System is a very simple process that
uses tools included in every linux installation.
The first step is to create a location to store the backup. For this article we're
going to store the backup on the same hard drive as the installed operating
system, but you can attach USB devices like thumb drive and external hard
drives and even special storage like iSCSI and NFS mounted device to store the
backup.
Once you are logged into the server and at a command line make the directory
to store the backup in an organized way such as:
mkdir /backups
Now we will create a compressed version of the Operating System in one
single file (tarball) using the tar command.
For RedHat, CentOS and Fedora or any Operating System based on these linux
flavors run the following command:
tar cvpzf /backups/backup.tgz --exclude=/proc --exclude=/lost+found
--exclude=/backups --exclude=/dev --exclude=/sys --exclude=/boot/grub
--exclude=/etc/fstab --exclude=/etc/sysconfig/network-scripts/
--exclude=/etc/udev/rules.d/70-persistent-net.rules /
For Debian or Ubuntu run the following command:
tar cvpzf /backups/backup.tgz --exclude=/proc --exclude=/lost+found
--exclude=/backups --exclude=/dev --exclude=/sys --exclude=/boot/grub
--exclude=/etc/fstab --exclude=/etc/network/interfaces
--exclude=/etc/udev/rules.d/70-persistent-net.rules /

Once the command completes the tarball will be located at /backups/backup.tgz


***HINT: You can change the name of the tarball file with a date identifier and
keep multiple versions or "snapshots" of your server's configuration.

Restore Your Server's OS From a Backup


In order to restore your server from the previously created tarball the server
must have the same Operating System version loaded on it. This particular
method of backup and restore is not meant for bare metal projects that need to
restore an Operating System to an empty hard drive. It was actually designed to
move a configured Operating System from one hardware platform to another,
but also works well for rolling back an operating system to a previous
configuration.
Once you have a working Operating System either on a new hardware platform
or the same hardware platform move the tarball to the server you want to
restore. If you stored the tarball on a USB device or other external storage just
reattach the device and mount it. If the tarball is on another linux server use
commands like this to copy it to the new servers hard drive:
#mkdir /backups

#scp root@original_server:/backups/backup.tgz /backups


Of course replace "original_server" with the appropriate IP address.
Enter the root users password and the transfer will begin.
Once the transfer has completed run this command to extract the tarball thereby
restoring the Operating System that the tarball contains:
tar xvpfz /backups/backup.tgz -C /
Complete the process with a reboot and troubleshoot any errors that may come
up.

10

Das könnte Ihnen auch gefallen