Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Define default values for variables
- SOURCE_DIR="/path/to/source/home/"
- DEST_DIR="/path/to/destination/"
- INCLUDE_FILE="includefile.txt"
- EXCLUDE_FILE="excludelist.txt"
- LOG_FILE="/path/to/backup.log"
- # Parse command line options
- while getopts ":s:d:i:e:l:" opt; do
- case ${opt} in
- s ) SOURCE_DIR=$OPTARG ;;
- d ) DEST_DIR=$OPTARG ;;
- i ) INCLUDE_FILE=$OPTARG ;;
- e ) EXCLUDE_FILE=$OPTARG ;;
- l ) LOG_FILE=$OPTARG ;;
- \? ) printf "Invalid option: -$OPTARG\n" >&2; exit 1 ;;
- : ) printf "Option -$OPTARG requires an argument.\n" >&2; exit 1 ;;
- esac
- done
- # Check that the source and destination directories exist
- if [ ! -d "$SOURCE_DIR" ]; then
- printf "Error: Source directory '$SOURCE_DIR' not found.\n" >&2
- echo "$(date): Error: Source directory '$SOURCE_DIR' not found." >> "$LOG_FILE"
- exit 1
- fi
- if [ ! -d "$DEST_DIR" ]; then
- printf "Error: Destination directory '$DEST_DIR' not found.\n" >&2
- echo "$(date): Error: Destination directory '$DEST_DIR' not found." >> "$LOG_FILE"
- exit 1
- fi
- # Check that the includefile.txt exists
- if [ ! -f "$INCLUDE_FILE" ]; then
- printf "Error: Include file '$INCLUDE_FILE' not found.\n" >&2
- echo "$(date): Error: Include file '$INCLUDE_FILE' not found." >> "$LOG_FILE"
- exit 1
- fi
- # Check that the excludelist.txt exists
- if [ ! -f "$EXCLUDE_FILE" ]; then
- printf "Error: Exclude file '$EXCLUDE_FILE' not found.\n" >&2
- echo "$(date): Error: Exclude file '$EXCLUDE_FILE' not found." >> "$LOG_FILE"
- exit 1
- fi
- # Check if the log file exists, create it if it doesn't
- if [ ! -f "$LOG_FILE" ]; then
- touch "$LOG_FILE"
- fi
- # Run rsync to exclude all dot files and dot folders in the home folder but copy all
- # other folders recursively except those listed in excludelist.txt
- rsync -av --exclude='/.*' --exclude-from="$EXCLUDE_FILE" "$SOURCE_DIR" "$DEST_DIR" --delete-before >> "$LOG_FILE" 2>&1
- # Run rsync again to include specific dotfiles and dotfolders listed in includefile.txt and all other files and folders recursively, except those in excludelist.txt
- rsync -av --exclude='/.*' --exclude-from="$EXCLUDE_FILE" \
- --include-from="$INCLUDE_FILE" "$SOURCE_DIR" "$DEST_DIR" --update >> "$LOG_FILE" 2>&1
- # Check if rsync command succeeded
- if [ $? -eq 0 ]; then
- printf "Backup completed successfully.\n"
- echo "$(date): Backup completed successfully." >> "$LOG_FILE"
- else
- printf "Error: Backup failed. Check log file for details.\n" >&2
- echo "$(date): Error: Backup failed. Check log file for details." >> "$LOG_FILE"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement