Skip to content

Instantly share code, notes, and snippets.

@psiborg
Last active January 19, 2023 23:54
Show Gist options
  • Save psiborg/063a92bda246d78901d5d8b8e41e7448 to your computer and use it in GitHub Desktop.
Save psiborg/063a92bda246d78901d5d8b8e41e7448 to your computer and use it in GitHub Desktop.
Backup files on Mac using command line
#!/bin/bash
currdate=`date +"%Y-%m-%d %T"`
folders=("Backup Documents Sites")
echo -e "\n---[${currdate}]---\n" >> ~/backup_stdout.log
echo -e "\n---[${currdate}]---\n" >> ~/backup_stderr.log
for folder in $folders
do
echo "Copying $folder..."
#ls ~/$folder 1>> ~/backup_stdout.log 2>> ~/backup_stderr.log
rsync -ahP ~/$folder /Volumes 1>> ~/backup_stdout.log 2>> ~/backup_stderr.log
done

Backup folder to a network folder

Syntax:

rsync -ahP {source} {destination} 2> ~/Desktop/rsyncErrors.txt

Example:

rsync -ahP ~/Documents /Volumes/backup 2> ~/Desktop/rsyncErrors.txt

Backup:

Backup folder

tar --exclude=.DS_Store --no-mac-metadata --totals -cvzf ~/MagPi.tar.gz ~/Bookshelf/MagPi

Split archive into parts

split -d -b 1G MagPi.tar.gz "MagPi.tar.gz.part"

Restore:

Join parts into archive

cat MagPi.tar.gz.part* > MagPi.tar.gz

Check archive

tar -tvzf ~/MagPi.tar.gz

Extract archive

tar -xvzf ~/MagPi.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment