Skip to content

Instantly share code, notes, and snippets.

@ecormaksin
Last active October 5, 2023 02:12
Show Gist options
  • Save ecormaksin/a1be61831675fe55557a1e81c1d3ecd9 to your computer and use it in GitHub Desktop.
Save ecormaksin/a1be61831675fe55557a1e81c1d3ecd9 to your computer and use it in GitHub Desktop.
rsyncによるフォルダー間のコピー(コピー先のみに存在するものは削除)
#!/usr/bin/env bash
cd `dirname $0`
if [ $# -gt 3 ]; then
echo "呼出時の引数: <コピー元ディレクトリー パス> <コピー先ディレクトリー パス> (<除外パターンファイルのパス>)"
exit 1
fi
if [ $# -lt 2 ]; then
echo "コピー元とコピー先のパスを指定してください。"
exit 1
fi
SRC_PATH=$1
DEST_PATH=$2
EXCLUDE_FROM_FILE_PATH=""
EXCLUDE_FROM_DUMMY_FILE="./dummy_exclude_patterns.txt"
if [ $# -eq 3 ]; then
EXCLUDE_FROM_FILE_PATH=$3
else
touch "${EXCLUDE_FROM_DUMMY_FILE}"
EXCLUDE_FROM_FILE_PATH="${EXCLUDE_FROM_DUMMY_FILE}"
fi
if [ ! -d "${DEST_PATH}" ]; then
mkdir -p "${DEST_PATH}"
fi
rsync \
--stats \
--ignore-times \
--checksum \
--recursive \
--links \
--perms \
--times \
--executability \
--delete-after \
--compress \
--exclude-from="${EXCLUDE_FROM_FILE_PATH}" \
"${SRC_PATH}" "${DEST_PATH}"
if [ -e "${EXCLUDE_FROM_DUMMY_FILE}" ]; then
rm -f "${EXCLUDE_FROM_DUMMY_FILE}"
fi
sudo chown -R $USER:$USER "${DEST_PATH}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment