Remote backups with rsync (MacOS => Ubuntu)
http://www.dedoimedo.com/computers/rsync-guide.html http://www.evbackup.com/support-rsync-setup/ http://superuser.com/questions/286911/save-output-to-a-text-file-from-mac-terminal
TODO: UPDATE EXAMPLE CODE
Test you can log in without password prompt.
sudo ssh -i /backup/ssh_key firstname.lastname@example.org
If you don't look the appendix
#Notice we are using --dry-run :) sudo rsync -avz --dry-run -e "ssh -i /Users/emilianoburgos/.ssh/id_rsa.pub" ./lib email@example.com:/root/tmp
-avs - All objects, verbose output, do not allow remote shell to interpret characters; in other words, file names with spaces and special characters will not be translated, which is what you want most likely, especially if you have Windows files, too.
--delete will delete files at the target (destination), if they do not exist in the source. This means you will always keep an up to date list of files and the source and destination will match, plus the destination will not slowly grow in size with older, perhaps irrelevant content.
There are a million, literally. So here's a sampling of good things:
-l (lowercase L)when symlinks are encountered, recreate the symlink on the destination.
--exclude=PATTERNexclude files matching PATTERN
--exclude-from=FILEread exclude patterns from FILE
--include=PATTERNdon't exclude files matching PATTERN
--include-from=FILEread include patterns from FILE
Likewise, the option
--files-from=FILE allows you to specify a detailed list of directories you wish to include in your backup. Please note that if you write down directory paths without trailing slash, they will be recreated blank, and if you do add the trailing slash, their content will also be copied.
Read about pipes
We are appending both standard output and standard error to a log file sync.log. If you only want to output standard error, use
If you use
> instead of
>> you will be overriding instead of appending- so that your file does not grow to big.
#!/bin/bash echo "Start rsync job" sudo rsync -avz --dry-run -e "ssh -i /Users/emilianoburgos/.ssh/id_rsa.pub" ./lib firstname.lastname@example.org:/root/tmp &>> sync.log exit 0
* */1 * * * /home/roger/rsync-backup.sh
- How to create ssh keys:
#Notice the command ends in '' sudo ssh-keygen -f /backup/ssh_key -t rsa -N ''
#Port defaults to 22 ssh-copy-id <username>@<host> -p <port_nr>
If you do not have
ssh-copy-id on your machine, you can either
brew it or do the following:
Another alternative is to copy the public key file to the server and concatenate it onto the authorized_keys file manually. It is wise to back that up first:
cp authorized_keys authorized_keys_Backup cat id_rsa.pub >> authorized_keys
Read more at Ubuntu's page