3

I want to be able to backup and restore my home directory of a remote account.

The command I use are:

Backup

ssh myuser@myuser.server.com "tar jcf - ." > backup.tar.bz2

Restore

cat backup.tar.bz2 | ssh myuser@myuser.server.com "tar jxf - ."

I'd like to be sure that all the files are restored or none, even if the server gets rebooted in the middle of a restore.

Any way to achieve what I want? I'd like to maintain the commands shown above as a base, but other ideas are appreciated as well.

  • 1
    Sounds like a job for rsync. – Tim Nov 15 '12 at 15:32
6

To get true atomicity, you would need to use filesystem-level features like btrfs snapshots.

  • I accepted your answer because it's true that if atomicity is not supported at lower level, neither user tools can guarantee it. – Paolo Nov 15 '12 at 19:05
4

You can use some marker file, when the restore is complete. When the restore is incomplete and the marker file is missing, you know you must remove the incomplete restore.

  • Thanks, good idea. Any hint about how to keep track of which files have been restored and not (in order to do the rollback)? – Paolo Nov 15 '12 at 15:27
  • 1
    You can add option v to your tar command and redirect the output to a log file. But if you restore the complete home directory, you could just remove the whole directory. – Olaf Dietsche Nov 15 '12 at 15:30
4

I suggest you to use rsync instead of backuping your files manually. With this tool, you can do exactly what you're doing with some extra features. For example, you can pass the --progress argument to know the last file copied.

Another feature is that you can copy only the new files or the modified ones, which will reduce the amount of data transmitted through the network. If you set the --delete argument, it will delete the files that you removed in your source folder. Finally, -z argument enables compression.

The command has the following structure:

$ rsync options source destination

Example

rsync --progress -azv -e ssh --delete /home/guandalino/ \
                myuser@myuser.server.com:/home/guandalino/backup/guandalino/

where

  • --progress displays detailed progress of rsync execution
  • -z enables compression
  • -v means verbose
  • -a indicates recursive. Also, it preserves permissions, owner, group etc.
  • --delete deletes files that are not there in source directory.

Take a look at this examples.

Scheduled Backup

This part is extra. This is how I use it to backup my home folder to my server automatically. I put the above example in a shell script file and enabled cron to run it every day, at 00:00AM.

00  00 *   *   *     /home/guandalino/backup_home_folder.sh 2>&1 >> /var/log/backup_home_folder.log

I redirect the output of script to a file in order to keep a log of the transmission. You'll have to generate a public key with ssh-keygen -t rsa if you don't have one yet, so your client will be allowed to connect to the server without entering your password.

  • 1
    I'll surely use rsync. This article also explains a good backup strategy using rsync. Though, I'm still unsure about the ability of rsync to operate atomically. – Paolo Nov 15 '12 at 16:26
3

Not directly, no. The best you can do is restore and verify to a temporary directory, then rename. The rename will move the files to their proper position atomically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.