Backup to external HDD with terminal commands - macos

Is there any terminal command that allows me to backup all my files to an external HDD. My OSX update failed so I need to backup my files some how, ASAP!

I would go with rsync as it only copies files changed since the last time you ran it, making it quicker on second and subsequent backups.
Assuming your username is Freddy Frog and your external HDD is called My External Drive, you could do this:
rsync -av "/Users/Freddy Frog" "/Volumes/My External Drive"

cp -R /Path-to-folder /Path-to-external-HDD
This wil copy the folder from (Path-to-folder) to the external HDD
If you want to make this an recurring command, the are a few steps to be made:
Open texteditor and write:
http://pastebin.com/fNuCMMr4
Save it as Backup.plist
Now navigate to /Library/LaunchDaemons and place the file in the folder
Restart your machine
Every day at 15:00 the folder of /Path-to-folder will be copied to the external HDD (if the mac is running at that time)
And yes, ofcourse you need to modify the code I gave you for the .plist file

backup to a single (optionally compressed) disk image file that can be restored using Apple’s Disk Utility software with hdiutil
sudo hdiutil create dst_image.dmg -format UDZO -nocrossdev -srcdir src_directory
OSXDaily also provided examples in 2009: for ditto, rsync, & asr
sudo ditto -X src_directory dst_directory
sudo rsync -xrlptgoEv --progress --delete src_directory dst_directory
sudo asr -source src_directory -target dst_directory -erase -noprompt

With new macs which use AFS (Apple file system), do:
diskutil list
to get a list of disks and their identifiers. Then, unlock your disk with:
diskutil apfs unlock <disk_identifier>
and provide your password. You should now view your disk under /Volumes/. For me, the disk of interest was Macintosh HD - Data.

Related

When syncing files between a MAC and a PC with Rsync, do I need to consider character code differences with --iconv?

I am syncing files between a PC and MAC, always from the MAC. Rsync is being used and I wonder should I consider using the --iconv option to account for any differences between the two systems?
I am new to the MAC and its BSD Unix operating system so have never had to consider this.
#!/bin/bash
mkdir /Users/Chris/Desktop/Jobs
mount -t smbfs //username:password#IPaddress/Jobs /Users/Chris/Desktop/Jobs
rsync -av --delete /Users/Chris/Jobs/ /Users/Chris/Desktop/Jobs/
umount /Users/Chris/Desktop/Jobs
rmdir /Users/Chris/Desktop/Jobs
I would like exact copies of the synced folders but their are hundreds of files so it is hard to verify if there is a character set problem that needs to be addressed with rsync's -iconv option.

Symlink not being created

I'm running Fedora on a laptop with a small SSD and large HDD. I've got the OS installed on the SSD and my data on the HDD.
All my files are located at /run/media/kennedy/data/Kennedy
What I had before (and want again) is a symlink from /home/kennedy to that location. That way I'm not messing with actual /home, but when I am in /home as normal user, all my things are easily accessed and stored with plenty of space. Right now /home/kennedy has the standard directories; desktop, documents, downloads, and so forth. No files worth worrying about.
So I opened a shell, logged in as su, and entered
ln -s /home/kennedy /run/media/kennedy/data/Kennedy
expecting that when I cd /home/kennedy and ls, I would see all my lovelies. Instead, I see that standard folders and nothing more. Whisky Tango Foxtrot, over.
edit to add: I'm pretty sure the permissions are right, but only pretty sure. How do I check and correct that (if off)?
You have to reverse the arguments:
ln -s /run/media/kennedy/data/Kennedy /home/kennedy
This will:
run successfully if /home/kennedy doesn't exist (kennedy would be the new symlink)
fail if /home/kennedy exists and it is not a directory (symlink or a regular file); need add -f flag in such a case - ls -sf ...
if /home/kennedy is a directory, then the symlink will be created as /home/kennedy/kennedy
See this related post: How to symlink a file in Linux?
You have the command backwards, it should be:
ln -s /run/media/kennedy/data/Kennedy kennedy
Invoke the command while you are in your /home directory, then you should be set.

rsync backup to external hard disk exFat fails

I tried to back up data from my macbook to an external hard drive - formatted with exFat (bacause of the Windows and Linux/Mac compatibility).
With Automator I will create a little Program, to backup my data easily. It works fine on the local drive and from local drive to an SD-Card. But it do not work from local drive to an external hard drive. What's wrong?
SOURCE=/Users/username/Pictures/test
TARGET=/Volumes/Backup/
LOG=~/Documents/AutomatorLogs/BackupSync.log
rsync -aze "ssh" --delete --exclude=".*" "$SOURCE" "$TARGET" > "$LOG"
I got this Error:
rsync: recv_generator: mkdir "/Volumes/Backup/test" failed: Permission
denied (13)
I know this is older but I just ran into this and I wanted to make sure this info was included. I know the OP is a little different, but I'm using a macbook and ran into the error I describe so I don't know how even with changing the disk name it worked.
rsync can't use -a when the target is an exfat drive. It will make the directories and seem to be backing up but no files are actually created. You need to use:
rsync -rltDv [SRC] [DESTINATION]
where:
-v, --verbose increase verbosity
-r, --recursive recurse into directories
-l, --links copy symlinks as symlinks
--devices preserve device files (super-user only)
--specials preserve special files
-D same as --devices --specials
-t, --times preserve times
The reason is because rsync doesn't handle permissions on exfat. You will see an rsync error (in syslog or if you control-c out):
chgrp [file] failed: Function not implemented (38)
It looks like the user that you're running the command as doesn't have permission to make a new directory in the /Volumes/Backup/ directory.
To solve this, you will probably need to change the permissions on that directory so that your script will be able to write to it and create the new directory it uses to make the backup.
Here are some links about permissions:
http://linuxcommand.org/lts0070.php
http://www.perlfect.com/articles/chmod.shtml
I think, I've got it:
It is related to the name of the external hard disk.
With the name "Backup" for my external hard drive, it does not work.
If I changed the name to anything else, it works.
(I tested some other exFat formatted external hard drives with other names and it worked. So I changed the name of this external drive to anything else and now it works. Crazy...)

How to copy .app or .prefPane (special) directory in OS X

Whenever I copy via cp or rsync a .app or .prefPane (special) directory to another location in OS X, it only copies the Contents directory. How do you copy these from the CLI and preserve the directory "look" in the GUI?
I found that if you copy the file to a directory with the same name it works correctly. If you don't specify a special directory destination it will NOT copy with the GUI preserved "look".
The command below does NOT work. It ONLY copies the Contents directory.
sudo rsync -a --progress Retrospect\ Client.prefPane/ ~/Desktop/
The command below works and copies correctly.
sudo rsync -a --progress Retrospect\ Client.prefPane/ ~/Desktop/Retrospect\ Client.prefPane
Leave it to Apple to screw up something so basic. I give thumbs down to Apple for this atrocious file naming convention. Awful.
I give another thumbs up to Debian and RHEL for keeping it simple.

Extract from DMG

How to extract content of DMG without mounting it?
I want add autoupdate system to my application. It downloads DMG from website, then extract new version of application from it.
Some .dmg files can be extracted by using 7-zip.
$ 7z x your.dmg
$ ls
your.dmg
0.MBR
1.Primary GPT Header
2.Primary GPT Table
3.free
4.hfs
5.free
6.Backup GPT Table
7.Backup GPT Header
...and after extracted the 4.hfs file:
$ 7z x 4.hfs
...you'll get the content of the .dmg file.
You could also mount the .dmg in Mac OS X using hdiutil command (which is also used by Homebrew Cask).
Please refer to this Ask Ubuntu question for more use cases on Linux.
Dmg is just a format used for MacOS. It's a not compressed file format like zip or tar.gz
You have several choices to mount it. Here are some options.
Double click on it to mount it.
Use hdiutil attach your.dmg to mount the dmg file. After mounting on it,
operating your command line to extract the files you want out.
Doing that is working counter to the design of DMGs, so it's very painful. DMGs are designed to be mounted, that is their whole raison d'être. You would need to rely on experimental code if you wanted to do something like that. You are probably better served by using a non-DMG archive such as a zip file, or by making your automatic updating process download, mount, and unmount the DMG file you provide.

Resources