Advertisement
Guest User

backup only select dotfiles from home directory

a guest
Mar 22nd, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Define default values for variables
  2. SOURCE_DIR="/path/to/source/home/"
  3. DEST_DIR="/path/to/destination/"
  4. INCLUDE_FILE="includefile.txt"
  5. LOG_FILE="/path/to/backup.log"
  6.  
  7. # Parse command line options
  8. while getopts ":s:d:i:l:" opt; do
  9.   case ${opt} in
  10.     s ) SOURCE_DIR=$OPTARG ;;
  11.     d ) DEST_DIR=$OPTARG ;;
  12.     i ) INCLUDE_FILE=$OPTARG ;;
  13.     l ) LOG_FILE=$OPTARG ;;
  14.     \? ) printf "Invalid option: -$OPTARG\n" >&2; exit 1 ;;
  15.     : ) printf "Option -$OPTARG requires an argument.\n" >&2; exit 1 ;;
  16.   esac
  17. done
  18.  
  19. # Check that the source and destination directories exist
  20. if [ ! -d "$SOURCE_DIR" ]; then
  21.   printf "Error: Source directory '$SOURCE_DIR' not found.\n" >&2
  22.   echo "$(date): Error: Source directory '$SOURCE_DIR' not found." >> "$LOG_FILE"
  23.   exit 1
  24. fi
  25.  
  26. if [ ! -d "$DEST_DIR" ]; then
  27.   printf "Error: Destination directory '$DEST_DIR' not found.\n" >&2
  28.   echo "$(date): Error: Destination directory '$DEST_DIR' not found." >> "$LOG_FILE"
  29.   exit 1
  30. fi
  31.  
  32. # Check that the includefile.txt exists
  33. if [ ! -f "$INCLUDE_FILE" ]; then
  34.   printf "Error: Include file '$INCLUDE_FILE' not found.\n" >&2
  35.   echo "$(date): Error: Include file '$INCLUDE_FILE' not found." >> "$LOG_FILE"
  36.   exit 1
  37. fi
  38.  
  39. # Check if the log file exists, create it if it doesn't
  40. if [ ! -f "$LOG_FILE" ]; then
  41.   touch "$LOG_FILE"
  42. fi
  43.  
  44. # Run rsync to exclude all dot files and dot folders in the home folder but copy all
  45. # other folders recursively except those listed in excludelist.txt
  46. rsync -av --exclude='/.*' --exclude-from='excludelist.txt' "$SOURCE_DIR" "$DEST_DIR" --delete-before >> "$LOG_FILE" 2>&1
  47.  
  48. # 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
  49.  
  50. rsync -av  --exclude='/.*' --exclude-from='excludelist.txt' \
  51. --include-from="$INCLUDE_FILE" "$SOURCE_DIR" "$DEST_DIR" --update >> "$LOG_FILE" 2>&1
  52.  
  53.  
  54.  
  55. # Check if rsync command succeeded
  56. if [ $? -eq 0 ]; then
  57.   printf "Backup completed successfully.\n"
  58.   echo "$(date): Backup completed successfully." >> "$LOG_FILE"
  59. else
  60.   printf "Error: Backup failed. Check log file for details.\n" >&2
  61.   echo "$(date): Error: Backup failed. Check log file for details." >> "$LOG_FILE"
  62. fi
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement