Skip to content

Instantly share code, notes, and snippets.

@itoffshore
Created July 14, 2015 00:36
Show Gist options
  • Save itoffshore/18a9f7fea9992eb6ec49 to your computer and use it in GitHub Desktop.
Save itoffshore/18a9f7fea9992eb6ec49 to your computer and use it in GitHub Desktop.
Selectively restore files & folders
#!/bin/sh
usage() {
local script=$(echo "`basename $0`")
cat <<EOF
$script: Selectively restore files / folders.
Usage: $script [OPTIONS] -- [file / dir names to exclude]
[ -s | --source ] : source directory
[ -d | --destination ] : destination directory
[ -h | --help ] : this help message
Example:
restore -s /backups/etc -d /etc -- fstab passwd
EOF
return 0
}
catch_exits() {
local script=$(echo "`basename $0`")
cat <<EOF
$script: Selectively restore files / folders.
Usage: $script [OPTIONS] -- [file / dir names to exclude]
[ -s | --source ] : source directory
[ -d | --destination ] : destination directory
[ -h | --help ] : this help message
Example:
restore -s /backups/etc -d /etc -- fstab passwd
EOF
return 0
}
catch_exits() {
echo -e "\n$(basename $0): exiting" 1>&2
exit 1
}
###### start #############
trap catch_exits 1 2 15 20
options=$(getopt -o s:d:hs --long source:,destination:,help -- "$@")
if [ $? -ne 0 ] || ! echo $1 |grep ^"-" 1>/dev/null; then
usage
exit 1
fi
eval set -- "$options"
while :; do
case "$1" in
-h | --help) usage && exit 1;;
-s | --source) src=$2; shift 2;;
-d | --destination) dest=$2; shift 2;;
-- ) exclude_list=$@; exclude_list=${exclude_list#-- *}; shift; break ;;
*) break;;
esac
done
if [ ! -d "$dest" ]; then
mkdir -p $dest
fi
if [ -n "$exclude_list" ]; then
for x in $exclude_list; do
ex_str="${ex_str} ! -name \"$x\""
done
fi
if [ -d "$src" ]; then
cmd="find "$src" -maxdepth 1 -mindepth 1 "$ex_str" -exec cp -rfp '{}' "$dest" \;"
eval $cmd
else
echo "$src does not exist"; exit 1
fi
echo "Files copied from: '$src' to: '$dest' excluding: '$exclude_list'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment