Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- include=""
- exclude=""
- user=""
- host=""
- key=""
- force=false
- target=""
- while getopts u:h:k:s:o:f:e:i: option
- do
- case "${option}" in
- u) user=${OPTARG};;
- i) include=${OPTARG};;
- e) exclude=${OPTARG};;
- h) host=${OPTARG};;
- k) key=${OPTARG};;
- o) target=${OPTARG};;
- f) force=true;;
- *) ;;
- esac
- done
- if [ -z "$user" ] || [ -z "$host" ] || [ -z "$target" ]
- then
- echo "Usage : $0 <-u : user> <-h : host> <-o : output directory> [-f : force full] [-k : key] [-i : include ONLY file] [-e : exclude file]"
- exit 1
- fi
- if [ ! -d "$target/../Backup" ]
- then
- if [ ! -d "$target/Backup" ]
- then
- echo "Creating backup directory on target..."
- if ! mkdir "$target/Backup"
- then
- echo "Could not create the backup directory. Exiting."
- exit 2
- fi
- fi
- target="$target/Backup"
- fi
- #Removing double slashes
- target=$(echo "$target" | sed -e "s#//#/#g")
- rsync_args=(-ravh -H "--rsync-path=\"sudo rsync\"" --progress --safe-links)
- if [ -f "$key" ]
- then
- rsync_args+=(-e \"ssh -i "$key"\")
- fi
- if [ -f "$include" ]
- then
- echo "Including only files from $include"
- rsync_args+=("--files-from=\"$include\"")
- fi
- if [ -f "$exclude" ]
- then
- echo "Excluding files from $exclude"
- rsync_args+=("--exclude-from=\"$exclude\"")
- fi
- if ! $force
- then
- last_backup=$(basename "$(ls -td "$target"/*/ 2>/dev/null | sed "y/\t/\n/" | head -n 1 )" 2>/dev/null)
- if [ -n "$last_backup" ]
- then
- echo "Processing backup based on --> $last_backup"
- rsync_args+=("--link-dest=\"../$last_backup\"")
- else
- echo "Processing full backup"
- fi
- else
- echo "Processing full backup"
- fi
- target=$(echo "$target/$(date '+%Y_%m_%d')" | sed "s#//#/#g")
- rsync "${rsync_args[@]}" "$user@$host:/" "$target" 2> error.log
- CODE=$?
- if [ $CODE -eq 0 ]
- then
- echo "Backup done : $target"
- exit 0
- else
- echo "Rsync stopped with code $CODE"
- exit $CODE
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement