红联Linux门户
Linux帮助

Linux Shell Script for Backup

发布时间:2008-03-31 01:02:45来源:红联作者:boats
Sometimes we need easier command to backup files. Under Linux, I wrote a shell script to backup simplely. The so-called "simplely" means a command near oral sentence and a default backup location. The script is posted below.[code]#!/bin/bash

# backup [objects] [prep.] [device]

if [ -z "$1" ]; then
echo Syntax: $0 OBJECT PREP. DEVICE
exit 0
elif [ -z "$2" ]; then
echo Syntax: $0 OBJECT($1) PREP. DEVICE
exit 0
elif [ -z "$3" ]; then
echo Syntax: $0 OBJECT("$1") PREP.("$2") DEVICE
exit 0
fi

object=""
prep=""
device=""
base=/media

case "$1" in
all | '*' | * )
object=*
;;
* )
object=$1
;;
esac

case "$3" in
[/]* | "" )
device=$3
;;
* )
device="$base/$3"
;;
esac

case "$2" in
from )
object="$device/$object"
device=.
;;
in | to | into )
;;
* )
;;
esac

cp -R "$object" "$device"

exit 0[/code]To install the script, copy the code above, paste it into a new text file, and save as backup.sh in your home folder. Turn on your executive privilege of this script file. Then you type "ln ./backup.sh /bin/backup" command to set the script file into your Linux system and complete the installation. When you want to backup files, try the following commands:

# Type the following to copy thins into your USB device.
# Suppose the USB name as "myusb".

backup all into myusb
backup ./backup.sh to myusb
backup ./backup.sh in /tmp

# Type the following to withdraw files from a device.
# The destination of file copy process is current working folder.

backup all from myusb
backup '*' from myusb
backup * from myusb

# The followings make nonsense.

backup * from myusb
backup all into *

The wildcard *, which means anything, ought to be treated as a list of objects. My backup script does not yet implement this feature.

In my computer the USB device is mounted in /media folder. Please replace the variable base with location where your USB device is mounted.
文章评论

共有 0 条评论