Last active
August 6, 2018 00:39
-
-
Save andromedarabbit/02c6473b61c60b2c30367b4f52cfa07e to your computer and use it in GitHub Desktop.
Backup my local Mac to Google Drive File Stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -n "${SKIP_BACKUPS}" ]; then | |
exit 0 | |
fi | |
type pigz || brew install pigz | |
type rsync || brew install rsync | |
type rsync-time-backup.sh || brew install rsync-time-backup | |
SRC_DIRS=( | |
"${HOME}/Movies" | |
"${HOME}/Music" | |
"${HOME}/Downloads" | |
"${HOME}/Desktop" | |
) | |
for ix in ${!SRC_DIRS[*]} | |
do | |
SRC_DIR=${SRC_DIRS[$ix]} | |
BASENAME="$(basename ${SRC_DIR})" | |
TARGET_DIR="/Volumes/GoogleDrive/My Drive/백업/Snapshots" | |
BACKUP_DIR="/Volumes/GoogleDrive/My Drive/백업/Backups/${BASENAME}$(date +\%Y-\%m-\%d)" | |
rsync -av -delete --backup --backup-dir="${BACKUP_DIR}" "${SRC_DIR}" "${TARGET_DIR}" --exclude-from "${THIS_DIR}/rsync-exclude-patterns.txt" | |
done | |
# rsync_tmbackup.sh는 hardlink를 활용하는데 Google File Stream에선 하드링크된 파일을 복사본으로 취급하기 때문에 사실상 매번 모든 파일을 다 복사하는 꼴이 된다. | |
# rsync_tmbackup.sh는 공백 문자가 들어간 경로를 잘 처리 못하는 버그가 있는 듯 하다. https://github.com/laurent22/rsync-time-backup/issues/116 | |
SRC_DIRS=( | |
# "${HOME}/dotfiles" | |
) | |
for ix in ${!SRC_DIRS[*]} | |
do | |
SRC_DIR=${SRC_DIRS[$ix]} | |
TARGET_DIR="/Volumes/GoogleDrive/My Drive/백업/Snapshots" | |
mkdir -p -- "${TARGET_DIR}" | |
touch "${TARGET_DIR}/backup.marker" | |
rsync_tmbackup.sh "${SRC_DIR}" "${TARGET_DIR}" "${THIS_DIR}/rsync-exclude-patterns.txt" | |
done | |
# 용량이 작은 텍스트 파일이 많은 경우에는 작업 폴더 전체를 압축해서 백업하는 편이 시간이 덜 걸린다. 병렬 압축을 지원하는 pigz 같은 도구를 이용하면 파일 숫자가 많아도 걱정할 필요가 없다. | |
# See https://www.reddit.com/r/linux/comments/3cnn54/just_a_reminder_on_multi_core_systems_use_pigz/ | |
SRC_DIRS=( | |
"${HOME}/Documents" | |
"${HOME}/dotfiles" | |
) | |
for ix in ${!SRC_DIRS[*]} | |
do | |
SRC_DIR=${SRC_DIRS[$ix]} | |
BASENAME="$(basename ${SRC_DIR})" | |
TARGET_DIR="/Volumes/GoogleDrive/My Drive/백업/${BASENAME}" | |
TARGET_FILENAME="$(date +%Y-%m-%d).tar.gz" | |
# Processor 중 절반만 동원한다. | |
THREADS=$[ $(nproc --all) / 2 ] | |
mkdir -p "${TARGET_DIR}" | |
pushd "${SRC_DIR})/.." | |
tar -c "${BASENAME}" | pigz --fast -p ${THREADS} -c | split -a 5 -d -b "3000M" - "${TARGET_DIR}/${TARGET_FILENAME}" | |
popd | |
find "${TARGET_DIR}" -mtime +30 -type f -delete | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment