Skip to content

Instantly share code, notes, and snippets.

@rupeshtiwari
Last active July 25, 2025 09:14
Show Gist options
  • Save rupeshtiwari/bfb1a97d2addae1f0cf9ed584fb599ad to your computer and use it in GitHub Desktop.
Save rupeshtiwari/bfb1a97d2addae1f0cf9ed584fb599ad to your computer and use it in GitHub Desktop.
samsung android mobile copy to mac laptop | backup mobile | copy android data on MACOS
#!/bin/bash
# How to use:
# chmod +x backup_android_all_incremental.sh
# ./backup_android_all_incremental.sh
# ./backup_android_all_incremental.sh --silent OR -s
# --------------
# Parse args
SILENT=0
if [[ "$1" == "--silent" || "$1" == "-s" ]]; then
SILENT=1
fi
# Logging setup
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_FOLDER=~/SamsungBackup_"$TIMESTAMP"
LOG_FILE="$BACKUP_FOLDER/backup_log_$TIMESTAMP.txt"
mkdir -p "$BACKUP_FOLDER"
log() {
echo -e "$1" >> "$LOG_FILE"
if [ "$SILENT" -eq 0 ]; then
echo -e "$1"
fi
}
# Ensure adb is installed
if ! command -v adb &> /dev/null; then
log "❌ adb (Android Debug Bridge) not found. Install it with: brew install android-platform-tools"
exit 1
fi
log "πŸ“ Backup will be saved to: $BACKUP_FOLDER"
log "πŸ“„ Logging to: $LOG_FILE"
# Start ADB
adb kill-server
adb start-server
# Check device connection
if ! adb devices | grep -q "device$"; then
log "❌ No Android device connected. Enable USB debugging and accept the prompt."
exit 1
fi
# Folders from your device
FOLDERS=(
"/sdcard/Alarms"
"/sdcard/Android"
"/sdcard/Audiobooks"
"/sdcard/DCIM"
"/sdcard/Documents"
"/sdcard/Download"
"/sdcard/Movies"
"/sdcard/Music"
"/sdcard/Notifications"
"/sdcard/Pictures"
"/sdcard/Podcasts"
"/sdcard/Recordings"
"/sdcard/Ringtones"
"/sdcard/SmartSwitch"
"/sdcard/Subtitles"
"/sdcard/log"
)
# PDF files to back up from /sdcard
PDF_FILES=(
"/sdcard/Adobe Scan 01 Jul 2025 (2).PDF"
"/sdcard/gd vashisht upay for my family.PDF"
)
log "πŸš€ Starting incremental backup..."
# Folder backup
for FOLDER in "${FOLDERS[@]}"; do
log "πŸ” Scanning $FOLDER..."
if adb shell ls "$FOLDER" >/dev/null 2>&1; then
adb shell "find '$FOLDER' -type f" | while read -r FILE; do
REL_PATH=${FILE#"/sdcard/"}
DEST_FILE="$BACKUP_FOLDER/$REL_PATH"
if [ ! -f "$DEST_FILE" ]; then
mkdir -p "$(dirname "$DEST_FILE")"
log "πŸ“₯ Copying: $FILE"
adb pull "$FILE" "$DEST_FILE" >>"$LOG_FILE" 2>&1 || log "⚠️ Failed to copy: $FILE"
else
log "⏩ Skipping existing: $REL_PATH"
fi
done
else
log "⚠️ $FOLDER not found. Skipping."
fi
done
# Backup root PDFs
log "πŸ“„ Copying PDFs from /sdcard root..."
for FILE in "${PDF_FILES[@]}"; do
FILE_NAME=$(basename "$FILE")
DEST_FILE="$BACKUP_FOLDER/$FILE_NAME"
if [ ! -f "$DEST_FILE" ]; then
log "πŸ“₯ Pulling: $FILE"
adb pull "$FILE" "$DEST_FILE" >>"$LOG_FILE" 2>&1 || log "⚠️ Failed to copy: $FILE"
else
log "⏩ PDF already exists: $FILE_NAME"
fi
done
log "βœ… Backup completed: $BACKUP_FOLDER"
adb kill-server
#!/bin/bash
# How to use:
# chmod +x restore_android.sh
# ./restore_android.sh
# ./restore_android.sh --silent OR -s
# --------------------
# Parse arguments
SILENT=0
if [[ "$1" == "--silent" || "$1" == "-s" ]]; then
SILENT=1
fi
# --------------------
# Ask for source folder if not passed
read -rp "πŸ“‚ Enter path to backup folder (e.g. ~/SamsungBackup_20250725_141000): " BACKUP_SOURCE
BACKUP_SOURCE="${BACKUP_SOURCE/#\~/$HOME}" # Expand ~ to full path
# Log setup
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
LOG_FILE="$BACKUP_SOURCE/restore_log_$TIMESTAMP.txt"
log() {
echo -e "[$(date '+%H:%M:%S')] $1" >> "$LOG_FILE"
if [ "$SILENT" -eq 0 ]; then
echo -e "$1"
fi
}
# --------------------
# Check adb installed
if ! command -v adb &> /dev/null; then
log "❌ adb not found. Install with: brew install android-platform-tools"
exit 1
fi
# Check folder exists
if [ ! -d "$BACKUP_SOURCE" ]; then
log "❌ Backup folder not found: $BACKUP_SOURCE"
exit 1
fi
# Start ADB
adb kill-server
adb start-server
# Check connection
if ! adb devices | grep -q "device$"; then
log "❌ No Android device connected. Enable USB debugging and allow access."
exit 1
fi
log "πŸš€ Starting restore from: $BACKUP_SOURCE"
log "πŸ“„ Log file: $LOG_FILE"
# --------------------
# Restore folders (map local -> device path)
declare -A RESTORE_PATHS=(
["DCIM"]="/sdcard/DCIM"
["Pictures"]="/sdcard/Pictures"
["Music"]="/sdcard/Music"
["Movies"]="/sdcard/Movies"
["Documents"]="/sdcard/Documents"
["Download"]="/sdcard/Download"
["Recordings"]="/sdcard/Recordings"
["Ringtones"]="/sdcard/Ringtones"
["SmartSwitch"]="/sdcard/SmartSwitch"
["Subtitles"]="/sdcard/Subtitles"
["WhatsApp/Media/WhatsApp Images"]="/sdcard/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Images"
)
for LOCAL_SUBDIR in "${!RESTORE_PATHS[@]}"; do
SRC="$BACKUP_SOURCE/$LOCAL_SUBDIR"
DEST="${RESTORE_PATHS[$LOCAL_SUBDIR]}"
if [ -d "$SRC" ]; then
log "πŸ” Restoring $LOCAL_SUBDIR to $DEST"
adb shell "mkdir -p \"$DEST\"" >/dev/null 2>&1
adb push "$SRC" "$DEST" >>"$LOG_FILE" 2>&1 && log "βœ… Done: $LOCAL_SUBDIR" || log "⚠️ Failed: $LOCAL_SUBDIR"
else
log "⏩ Skipped (not found): $LOCAL_SUBDIR"
fi
done
# Restore PDFs from root
for FILE in "$BACKUP_SOURCE"/*.pdf "$BACKUP_SOURCE"/*.PDF; do
[ -e "$FILE" ] || continue
FILE_NAME=$(basename "$FILE")
DEST="/sdcard/$FILE_NAME"
log "πŸ“„ Restoring PDF: $FILE_NAME"
adb push "$FILE" "$DEST" >>"$LOG_FILE" 2>&1 && log "βœ… Copied: $FILE_NAME" || log "⚠️ Failed: $FILE_NAME"
done
log "πŸŽ‰ Restore complete."
adb kill-server

πŸ“¦ Android Incremental Backup Script

Backup your Android media, documents, and WhatsApp images to macOS using ADB via command line β€” incrementally and with optional silent mode.


βœ… Prerequisites

  1. Install ADB (Android Debug Bridge):

brew install android-platform-tools

  1. Enable Developer Mode on Android:

    • Go to: Settings > About phone > Tap "Build number" 7 times

    • Then go to: Settings > Developer options > Enable USB Debugging

  2. Authorize Device:

    • Connect via USB

    • Accept the "Allow USB Debugging" prompt on your phone


πŸš€ Usage

  1. Give execute permission:

chmod +x backup_android_all_incremental.sh

  1. Run script:

./backup_android_all_incremental.sh # Normal mode ./backup_android_all_incremental.sh --silent # Silent mode (logs only)

  1. Backup Location:
    Output saved to: ~/SamsungBackup_<timestamp>/

  2. Log File:
    Full activity log saved inside the backup folder:
    backup_log_<timestamp>.txt

πŸ”„ What It Backs Up

  • Photos, videos: DCIM/, Pictures/

  • Music, recordings, downloads, documents

  • WhatsApp Images:
    Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Images

  • PDFs from device root

πŸ”„ Restore from Backup

Restore your backed-up Android files (photos, videos, documents, WhatsApp images, etc.) from Mac to Android device using:

restore_android.sh

βœ… Prerequisites

  • ADB must be installed:

    brew install android-platform-tools

  • Enable USB Debugging on your Android:

    • Settings > About phone > Tap "Build number" 7 times

    • Then go to Settings > Developer options > Enable USB Debugging


πŸš€ How to Use

chmod +x restore_android.sh ./restore_android.sh # Interactive restore ./restore_android.sh --silent # Silent mode (logs only)

  • Enter the full path to your backup folder when prompted (e.g. ~/SamsungBackup_20250725_141000)

  • Script will push content back to appropriate folders on your Android device


πŸ” What Gets Restored

  • Photos & Videos β†’ /sdcard/DCIM, /sdcard/Pictures

  • Downloads β†’ /sdcard/Download

  • Documents, Music, Ringtones β†’ respective /sdcard/ locations

  • WhatsApp Images β†’ /sdcard/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Images

  • PDF files from backup root β†’ /sdcard/


πŸ“„ Logs

Restore activity is saved inside the backup folder as:

restore_log_<timestamp>.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment