Advertisement
Guest User

rsync selective dotfiles and dotfolder

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