How-to: Local Backup with rsync


A backup using the rsync command can be very useful because it:

Takes less time to replicate

rsync will replicate the whole content between the source and the destination directory only once. Consecutive rsync’s will transfer only the changed blocks or bytes, which makes it very fast

uses Less bandwidth usage

rsync will use compression of data block by block at the sending end and decompress at the receiving end

is Secure

rsync uses ssh protocol during transfers and hence allows encryption of data.

The script broken down

I will create a script which will backup my current web directory /data to the same server on a disk mounted at /backup. The script if broken down, can be viewed as:

rsync -azvu –progress /data/ /backup

wher options

  • z is for compress mode
  • v is for verbose mode
  • a is to preserve symbolic links, permissions, timestamp and to be recursive
  • u is to preserve unmodified files at the destination
  • progress is to show the progress during transfer

An extract of the transfer start is:

sending incremental file list
data/
data/log/
data/log/gulshan.beejan.log
8713 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1004/1010)
data/lost+found/

ending with:

sent 284122142 bytes  received 230478 bytes  1914832.46 bytes/sec
total size is 379003084  speedup is 1.33

Now, if I perform a second transfer, the result at the end will be much faster. Here’s a extract of that transfer:

[root@sdb backup]# rsync -avzu –progress /data /backup
sending incremental file list

sent 278200 bytes  received 1778 bytes  50905.09 bytes/sec
total size is 379003084  speedup is 1353.69

 The script

Let’s convert that into a nice and clean script, shall we?

Create a file in

vi /scripts/localrsync.sh

Paste the following:

#!/bin/bash

# declare variables

SOURCE_DIR=/data

DESTINATION_DIR=/backup

#rsync command, make sure the user you run this file as has the required permissions to the source/dest folder

#we dont need progress or verbose, this runs in background mode

rsync -azu  $SOURCE_DIR $DESTINATION_DIR

Chmod 777 the sh file

chmod 777 /scripts/localrsync.sh

Test the file

sh /scripts/localrsync.sh

[root@sdb ~]# sh /scripts/localrsync.sh
sending incremental file list

sent 278200 bytes  received 1778 bytes  62217.33 bytes/sec
total size is 379003084  speedup is 1353.69

Add it to crontab,

 crontab -e

Add this line at the end to run it daily, every 2AM

*    2    *    *    *    sh /scripts/localrsync.sh

 

Read More

Offline Backup: Oracle Application Server 10g


This post is a follow up on the Stop & Start script I made for Oracle Application Server 10g.

You will need to create a new bash script which would basically have the following lines and should be placed in /home/oracle/scripts/backupoas.sh

#THIS SCRIPT MUST BE RUN AS ROOT

time_start=`date +%s`

source /home/oracle/scripts/oas/settings.conf

##SHUTDOWN APPSERVER

su – oracle -c “cd /home/oracle/scripts/;./stopas.sh”

#Create Backup on this Machine in /backup/daily.tar

tar cvf – /backup/daily.tar /u01″

##END OF BACKUP SCRIPT

##START APPSERVER

su – oracle -c “cd /home/oracle/scripts/;./startas.sh”

time_end=`date +%s`

time_exec=`expr $(( $time_end – $time_start ))`

echo “Application Server Backup Execution time is $time_exec seconds”

To make the backup script better, put that in a crontab by issuing, crontab -e and adding the line 21 30 * * * /home/oracle/scripts/backupoas.sh.

DO NOT FORGET TO CHMOD 777 those script files

Read More

Backup/Restore MySQL Database Linux


Initiating a backup from phpMyAdmin is not always the best solution when are you running a medium to massive web database. If you are running your mysql server on a vps or a dedicated server on Linux, those commands will definitely be useful to you.

Backup:

mkdir -p /backup

cd /backup

mysqldump -u root -p database_name > database_name.dmp

Restore:

cd /backup

mysql -u root -p database_name < database_name.dmp

Read More

Beginner: Full Linux Server Backup


This has been tested with a working server of Debian5.

Login as root

Make a backup directory. It is best if you have the backup directory on another partition or another hard disk

mkdir -p /backup

Create a backup sh file with arguments to backup the whole system while excluding contents of proc sys dev and backup directory

vi /backup/backup.sh

tar -cvpf /backup/fullbackup.tar –directory=/ –exclude=proc –exclude=sys –exclude=dev/pts –exclude=backup .

Create a restore sh file which moves the fullbackup.tar file to / and then extracts it

vi /backup/restore.sh

mv /backup/fullbackup.tar /fullbackup.tar

tar -xvpf /fullbackup.tar

Change the file permissions to make it executable

chmod -R 750 /backup

Add an argument to crontab to make it run at 22:10 daily.

crontab -e

10 22 * * * /backup/backup.sh

Done!

To restore this backup, install a minimal version of the same linux flavor and then run /backup/restore.sh

Read More