
Flash Advertise
| Linux Backups For Real People, Part 3 | ||
| 摘自: www.linuxplanet.com 被阅读次数: 95 | ||
由 yangyi 于 2007-12-20 23:47:59 提供 | ||
1. Simple Network Backups In part 1 of this series we learned how to format USB storage devices for maximum portability, and how to configure udev so they would always have the same names when you plug them in, and in part 2 we learned a simple, efficient way to use the excellent rsync command for easy single-user backups. Today we're going to create menu icons for launching our backups whenever we darned well feel like it, set up a simple network backup scheme, and create automatic scheduled backups. First we'll take our rsync incantation from last week and enshrine it in a shell script, and give it an imaginative name like, oh, what about backupscript: #!/bin/sh ################################## # simple rsync script for making backups # to a USB storage device, formatted in # FAT16/32 ################################## # this must be one long unbroken line rsync -rlvt --modify-window=1 --include-from=/home/carla/rsync-includes \ /home/carla/* /media/BACKUP1 If you are having trouble using include and exclude files, there is a simpler way: just list all the files and directories you want to backup like this: rsync -rlvt --modify-window=1 \ /home/carla/thisdirectory \ /home/carla/thatdirectory \ /home/carla/filename \ /media/BACKUP1 You don't have to list them one per line; that's just how I like to do it. The backslashes tell Bash that you are spreading the command out over several lines. If you put it all on one line, be sure it's one long unbroken line. Make your script executable: $ chmod +x backupscript 2. Making Menu Icons Gnome and KDE both have menu editors that we'll use to attach our backup script to nice menu icons. Here's how to do it in Gnome: find menu layout in your system menu, or run the alacarte command. Click on the menu you want to put it in, then click New Item. The Icon button takes you to vast herds of Gnome icons to choose from. Enter a name, like MyBackup or something. Then copy this line for the Command box exactly, except you'll substitute your own script name: gnome-terminal -x /home/carla/backupscript What if you need root permissions to run your backup script? Then do this: gnome-terminal -x su - -c '/home/carla/backupscript' root Figure 1 shows what it should look like. Do not check 'Run command in a terminal'. Now open gnome-terminal and go to the 'Edit-Current Profile'. On the 'Title and Command' tab select 'When command exits: Hold the terminal open.' If you don't do this, you won't see the output of your backup command because the terminal will disappear when it's finished. If you would rather not have this be the default behavior, use 'File-New Profile' to create a custom profile and give it a helpful name like stayopen. Then use it this way: gnome-terminal --window-with-profile=stayopen -x su - -c '/home/carla/backupscript' root KDE users, open the menu editor, which is the kmenuedit command. Or right-click on the application launcher. Select the menu you want it to be in and click the New Item button. Enter the full path to your scriptname, check the 'Run in Terminal' box, and add the --noclose option. If you need root permissions check the 'Run as a different user' box, and enter 'root.' It should look like Figure 2. Save your changes, and you're done. Click on your new backup button, and you'll see something like Figure 3. 3. Automated and Network Backups An inexpensive way to perform network backups is to an external hard drive attached to one of your PCs. You'll need an SSH server running on its attached PC, and SSH clients for everyone else. Rather than giving everyone a login on the backup PC, create a special backup account for everyone to use with a clever, catchy name like "backup". Keep in mind that everyone who has access to the backup drive can easily read all the files on it. Suppose the drive is attached to a PC named Penguina. Assuming you have local DNS like a good Linux geek, you can test logging in remotely like this: $ ssh backup@penguina backup@penguina's password: You may also use the IP address, if it's a static address. We don't want to be prompted for a password because that defeats automatic scheduled backups, so instead we'll use SSH public key authentication. It's easy to set up- just follow the "Public-Key Authentication" section of The (Practically) Ultimate OpenSSH/Keychain Howto, except do not set a passphrase! Now let's fix up our backup script to use the remote drive. Change the last line to look like this: backup@penguina:/media/BACKUP1/carla Notice a new addition, the "carla" directory. Since this is a shared network drive you'll probably want to give each user their own directory. rsync will create them for you. Remember, do not have a trailing slash! (See Part 2 for more information.) The syntax is simple: login-name@hostname:/full-file-path-of-the-backup-directory. Now what about scheduling unattended backups? Piece of cake--we'll make an entry in /etc/crontab, which is a delightfully simple way of using cron. This example runs the backup script every weeknight at 11:05pm: 23 5 * * 1-5 carla /home/carla/backupscript man 5 crontab tells all of your options. 4. Troubleshooting and References Remember to boost the verbosity of rsync's output with more -vv to get more information when things don't work correctly, and use the --dry-run option for test-drives. man rsync is thorough and detailed, and a bit of Web searching will find hundreds of articles and tips about rsync. rsync gurus like to add all manner of complexities and refinements, which of course you are welcome to do as well. What we did in this series was take advantage of universal Linux utilities (OpenSSH, rsync, scripting, and udev) to create a simple, portable backup method that we can easily replicate on any Linux system. And perhaps even more important, the backed-up files are easily recoverable- you can plug your backup drive into any Linux or Windows PC to access them. This also works for a dedicated backup server, though there are better and more secure methods for those that we'll get to sometime down the road. Resources * rsync * OpenSSH * Easy Automated Snapshot-Style Backups with Linux and Rsync * My very own Linux Cookbook has a good chapter on backup and recovery 原文链接: http://www.linuxplanet.com/linuxplanet/reviews/6437/1/ |