User:Charlie Huggard/Linux Hacks

From Wikipedia, the free encyclopedia

As I'm a systems administrator for one of my jobs, I deal with Linux on a daily basis, I'll post here a few tips/hacks that you may find useful.

[edit] Hard Link Backups with Rsync

For those of you who backup your linux machines to a Linux server, this may be helpful. It allows us to keep several full backups, preserving permissions, with a fraction of the disk space because unchanged files are hard linked from the older backups. I found parts of these instructions strewn about the internet, and assembled them here.

  • Make backup account - Using your distribution's user tools, make an account for the linux machine (these instructions assume that it is the same name as the machine's hostname) on the backup server. I also created a group for these accounts to make permissions granting easier.
  • Grant SSH login - If you restrict who can login in your sshd_config file (a good idea no matter what Linux you are running), you must make sure you grant SSH access the previously created user (or user group). If the machine you are backing up has a DNS name / Static IP it would be a good idea to restrict that account to only be able to log in from there. You also want to create a public/private keypair so that the root account of the machine you're backing up can log into the user account on the backup server (espically important for cron that cannot type a password for you).
  • Grant Sudo ability - this is all on 1 line in your sudoers file. In this example we keep 7 concurrent backups (backup.0 being most recent to backup.6 being the oldest), the backup machine accounts are members of the servers group and the backup server name is backupserver. This allows the backup accounts to run rsync (either verbosely or not) and remove the oldest backup. By having a Sudo ability, you can preserve all of the permissions on your linux machine.
%servers  backupserver= NOPASSWD: 
/usr/bin/rsync --server -logDtpr --delete-excluded --delete-after --link-dest ../backup.1 . ./backup.0/ ,
/usr/bin/rsync --server -vlogDtpr --delete-excluded --delete-after --link-dest ../backup.1 . ./backup.0/ ,
/usr/bin/rm -rf backup.6
  • prep.sh - In the home directory on the backup server, create this file:
#!/bin/bash
MAXBACKUP=6 
sudo rm -rf backup.${MAXBACKUP}
rm backup.${MAXBACKUP}.date
for BKUPNUM in `seq ${MAXBACKUP} -1 1`
do
        mv backup.`expr ${BKUPNUM} - 1` backup.${BKUPNUM}
        mv backup.`expr ${BKUPNUM} - 1`.date backup.${BKUPNUM}.date
done
date > backup.0.date
  • backup.sh - This will be the file you execute on the workstation in order to backup your files. Either run manually as root, or have cron run it as root. You need to have root access so that you can read all of the files on the system. You can also add an exclude of /tmp/* if you'd like, and you'd have to make sure you also exclude any mounted devices you do not want backed up (e.g. /mnt/cdrom) if you have them.
#!/bin/bash
mount /boot
ssh `hostname`@backupserver.mydomain.com ./prep.sh
rsync -a --delete --delete-excluded --delete-after --rsh="ssh -l `hostname`" --exclude="/sys/*" --exclude="/proc/*" \
--rsync-path="sudo /usr/bin/rsync" --link-dest=../backup.1 /  backupserver.mydomain.com:./backup.0/
umount /boot
  • Restoration
    1. Boot a livecd
    2. Mount your drives
    3. Rsync from the backup server to your server.