Chinaunix首页 | 论坛 | 博客

qsh

  • 博客访问: 4054915
  • 博文数量: 1015
  • 博客积分: 15904
  • 博客等级: 上将
  • 技术积分: 8572
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-04 19:16
文章分类

全部博文(1015)

文章存档

2019年(1)

2017年(1)

2016年(19)

2015年(27)

2014年(30)

2013年(95)

2012年(199)

2011年(72)

2010年(109)

2009年(166)

2008年(296)

分类: LINUX

2014-10-11 22:44:11


Backing Up with NFS and rsync

Submitted by Paulus on Sun, 01/19/2014 - 00:36

Assumptions & Prerequisites

  • Fresh install of RedHat/CentOS/Fedora on the server
  • minimal install profile
  • Security is not taken into account

Setup

A system running RedHat/CentOS/Fedora, but any Linux distribution will work with the proper modifications. Just to get something up and running, I did a minimal install of CentOS 6.4. The CentOS server will have a partition mounted at /mnt/backup which will be shared via NFS.

The machine that contains the files that we want to back up will mount the NFS share which is found on the server (/mnt/backup). I chose to have static IPs for the computers but you can use your own addressing scheme or DHCP.

  • Server Configuration
    • IP Address: 192.168.1.112
    • Netmask: 255.255.255.0
    • Gateway: 192.168.1.1
    • The backup directory that will be shared via NFS is /mnt/backup
    • IPTables is running
    • SELinux is running
  • Client
    • IP Address: 192.168.1.111
    • Netmask: 255.255.255.0
    • Gateway: 192.168.1.1
    • NFS mount directory: /mnt/nfs

Server Configuration

If need be, change eth0 to the name of your network interface.

?
Edit /etc/sysconfig/network-scripts/ifcfg-eth0
1
2
3
4
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.112
NETMASK=255.255.255.0
?
Edit /etc/sysconfig/network
1
2
HOSTNAME=backup
GATEWAY=192.168.1.1
?
Verify
1
2
3
ifconfig eth0
route -n
ping google.com

IPTables Configuration

By default, iptables is installed and configured to only allow ssh connections. We will need to create a new rule which allows connections to TCP port 2049 and UDP port 53027.

?
1
2
3
# iptables -I INPUT -p tcp -s 192.168.1.111 --dport nfs -j ACCEPT
# iptables -I INPUT -p udp -s 192.168.1.111 --dport nfs -j ACCEPT
# service iptables save

If you do not want to use iptables, then stop the service and disable it from starting at boot:

?
1
2
# service iptables stop
#/sbin/chkconfig --level 2345 iptables off

SELInux Configuration

RedHat documentation states that all NFS policies are confined to default, meaning that no additional configuration or booleans need to be set.

NFS Server Setup and Configuration

Install the NFS server and client. If you want to have your backup directory some place else, substitute /mnt/backup with the desired location.

?
1
2
3
4
# yum -y install nfs-utils
# mkdir /mnt/backup
# chown nfsnobody:nfsnobody /mnt/backup
# /sbin/chkconfig --level 345 nfs on

On line 3, you're changing the permission of the backup directory so that it can be written to.

?
Add the export to /etc/exports
1
/mnt/backup 192.168.1.111(rw,root_squash)
?
Make the export available to the client
1
exportfs -a

In the exports file, we're telling NFS to not allowing the root user access. Since we're not allowing root access, we will need to setup a user and create a directory in the exported directory with the proper permissions.

?
Add a user
1
2
3
4
# useradd -s /bin/bash -m paulus
# passwd paulus
# mkdir /mnt/backup/paulus
# chmod paulus:paulus /mnt/backup/paulus

Client Configuration

No configuration is required. However, if you want to mount the NFS share on boot you will need to edit the fstab file.

?
/etc/fstab
1
192.168.1.112:/mnt/backup     /mnt/backup     rw,hard,intr     0     0

You should be familiar with the rw mount option. I'm using hard and intr. The hard option prevents corrupt data being written to the remote server. This can happen if there are any network connectivity issues. In the event that the NFS server is unreachable, then the program accessing the file will wait. However, you won't be able to kill the process except for a sure kill. Adding the intr option allows the process to be killed. To see a list of options view the man pages for the mount and nfs commands.

If you don't want to have the NFS share mounted at boot, run the following command.

?
1
mount -t nfs 192.168.1.112:/mnt/backup /mnt/nfs

rsync

Now that the NFS server is up and configured we can start backing up. The example below assumes that we have the NFS share mounted at /mnt/backup.

?
Quick and Dirty
1
2
rsync -az /home /mnt/backup
rsync -az --progress /home/mnt/backup

The above will copy the home directory to the remote server. The resulting tree structure on the remote server will be /mnt/backup/home. If you add a trailing slash, it will copy all the files and directories in /home to /mnt/backup. The -a is saying you want to archive the directory and is equivelent to using -rlptgoD

  • -r, --recursive; recurse into directories
  • -l, --links; copy symlinks as symlinks. (You can use the -L, --copy-links to convert the links into files. Additionally specifying --copy-unsafe-links will trun links pointing to files outside of the tree into files  or --copy-safe-links will ignore links that point outside of the tree.)
  • -p, --perms; preserve permissions
  • -t, --times; preserve modification times
  • -g, --group; preserve group
  • -o, --owner; preserve owner
  • -D; same as --devices (causes rsync to transfer character and block device files to the remote system to recreate these devices.), --specials (causes rsync to transfer special files such as named sockets and fifos.)

Other options that you might find useful are:

  • --progress; Shows the progress of the operation.
  • --delete; Removes any files from the destination that aren't in the source.
  • -n, --dry-run; Shows you what's going to happen with out actually doing anything.
  • --exclude, --exclude-from; List of files and/or directories that you don't want to transfer. The first is the command line version. The second, allows you to create a file with a list of files and directories (one per line)

Troubleshooting

  • If you're unable to see the files, make sure the file system is actually mounted. 

    ?
    1
    2
    # cat /proc/mount
    # mount | grep nfs
  • Remount the file system
  • If the server is returning Permission denied:
    • Make sure that the volumes are exported. Try re-running exportfs on the server. (exportfs -ra)
    • If you are trying to mount the file system as rw and the export file has the volume exported as ro.
    • Check /proc/fs/nfs/exports or /var/lib/nfs/etab making sure the clients are correct.
    • You can not export a child and a parent. For example:

      ?
      1
      2
      /mnt/backup 192.168.1.111(rw)
      /mnt/backup/other_directory 192.168.1.111(rw)
  • Verify that NFS is actually running

    ?
    rpcinfo -p
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    program vers proto   port  service
     100000    4   tcp    111  portmapper
     100000    3   tcp    111  portmapper
     100000    2   tcp    111  portmapper
     100000    4   udp    111  portmapper
     100000    3   udp    111  portmapper
     100000    2   udp    111  portmapper
     100024    1   udp  47994  status
     100024    1   tcp  36487  status
     100005    1   udp  38727  mountd
     100005    1   tcp  50962  mountd
     100005    2   udp  37566  mountd
     100005    2   tcp  39548  mountd
     100005    3   udp  55141  mountd
     100005    3   tcp  46667  mountd
     100003    2   tcp   2049  nfs
     100003    3   tcp   2049  nfs
     100003    4   tcp   2049  nfs
     100227    2   tcp   2049  nfs_acl
     100227    3   tcp   2049  nfs_acl
     100003    2   udp   2049  nfs
     100003    3   udp   2049  nfs
     100003    4   udp   2049  nfs
     100227    2   udp   2049  nfs_acl
     100227    3   udp   2049  nfs_acl
     100021    1   udp  38521  nlockmgr
     100021    3   udp  38521  nlockmgr
     100021    4   udp  38521  nlockmgr
     100021    1   tcp  35931  nlockmgr
     100021    3   tcp  35931  nlockmgr
     100021    4   tcp  35931  nlockmgr
  • On the client, make sure that NFS4 is enabled

    ?
    fgrep nfs4 /proc/kallsyms
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ffffffffa1391900 t nfs4_check_openmode  [nfsd]
    ffffffffa1391a70 t __nfs4_file_get_access       [nfsd]
    ffffffffa1391bc0 t nfs4_set_lock_denied [nfsd]
    ffffffffa1391d80 t nfs4_alloc_stid      [nfsd]
    ffffffffa1391ec0 t nfs4_access_to_omode [nfsd]
    ffffffffa1392300 t __nfs4_file_put_access       [nfsd]
    ffffffffa1392370 t nfs4_file_put_access [nfsd]
    ffffffffa1392490 t nfs4_get_vfs_file.isra.76    [nfsd]
    ffffffffa13928f0 t nfs4_share_conflict.isra.68  [nfsd]
    ffffffffa1393290 t nfs4_preprocess_seqid_op     [nfsd]
    ffffffffa13aa650 d nfs4_disable_idmapping       [nfsd]
    ffffffffa13a7248 r __param_nfs4_disable_idmapping       [nfsd]
    ffffffffa13a2000 r __param_str_nfs4_disable_idmapping   [nfsd]
    ... snip
阅读(829) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~