Skip to content

Instantly share code, notes, and snippets.

@gurchik
Created October 22, 2023 04:36
Show Gist options
  • Save gurchik/ee91527de301cffe0bde3fedbadb0c3b to your computer and use it in GitHub Desktop.
Save gurchik/ee91527de301cffe0bde3fedbadb0c3b to your computer and use it in GitHub Desktop.
Backup iCloud directory to USB drive
#!/bin/bash
7zip_installed() {
builtin type -P "7zz" &> /dev/null
}
prompt_with_default() {
read -p "$1 ($2):" INPUT
if [ -z "$INPUT" ]; then
echo "$2"
fi
echo "$INPUT"
}
7zip_installed || { echo "7zz not found, please install using brew"; exit 1; }
SRC="$(prompt_with_default "Source path" "$HOME/Library/Mobile Documents/com~apple~CloudDocs")"
if [[ ! -d "$SRC" ]]; then
echo "Directory does not exist: $SRC"
exit 1
fi
DEST="$(prompt_with_default "Dest path" "$(dirname "$0")")"
if [[ ! -d "$DEST" ]]; then
echo "Directory does not exist: $DEST"
exit 1
fi
ARCHIVE_NAME="$(prompt_with_default "Archive name" "$(date +%Y-%m-%d-%H%M).7z")"
read -s -p "Archive password:" PASS
if [ -z "$PASS" ]; then
echo "You did not enter a password"
exit 1
fi
echo ""
read -s -p "Confirm password:" PASS2
if [ "$PASS" != "$PASS2" ]; then
echo "The two passwords do not match"
exit 1
fi
echo ""
echo "You've entered:"
echo "Source: $SRC"
echo "Dest folder: $DEST"
echo "Archive name: $ARCHIVE_NAME"
echo "This will delete all .7z files with the same name."
read -p "Proceed? (Y/n): " CORRECT
if [ "$CORRECT" != "Y" ]; then
echo "Exiting..."
exit 1
fi
echo "Deleting existing .7z files"
rm -f "$DEST/*.7z.*"
TMP="$(mktemp -d -t 'backup')"
echo "Using $TMP as a scratch space"
cd "$TMP"
echo "Copying files to temp directory"
cp -R "$SRC/." ./
echo "Creating README.txt"
cat <<EOF > ./README.txt
This was created using backup.sh on $(date).
EOF
echo "Creating temp archive files"
7zz -v500m -mhc=on -mhe=on -p"$PASS" a "$ARCHIVE_NAME" "./*"
for archive in "$ARCHIVE_NAME".*; do
echo "Moving $archive to $DEST"
mv "$archive" "$DEST/"
done
echo "Deleting temp directory"
rm -rf "$TMP/"
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment