linux日常备份

其实我在备份的策略相当的简单,我并没有想要将整个系统完全的备份下来,因为太耗时间了!而且就我的立场而言,似乎也没有这个必要,所以通常我只备份较为重要的档案而已!不过,由于我需要备份 /home 与网页数据,如果天天都备份,我想,系统迟早会受不了,所以我就将我的备份分为两大部分,一个是每周备份,一个则是每日备份,备份的时间点都选择在凌晨的 3~4 点左右!这个时候我就写了两个简单的 scripts ,分别来储存我的数据:

  1. 使用一颗加挂的硬盘来进行备份的功能,挂在 /disk2 当中;
  2. 每周进行的备份有 /home, /var, /etc, /boot, /usr/local 等目录;
  3. 每日进行的目前仅有 MySQL 数据库;
  4. 利用 /etc/crontab 来自动提供备份的功能;
  5. 在每周或每月定期的将数据分别 (a)烧录到光盘上面 (b)使用网络传输到另一部机器上面。

日常备份行为:
底下提供我的备份的 scripts ,希望对大家有点帮助!
 
[root @test /root]# vi backupweekly.sh
#!/bin/bash
# This file will backup
#        1. username (in /etc, passwd, shadow, group, gshadow, aliases,
#           aerosol510.mail, and /var/spool/mail, total 7 files)
#        2. httpd (in /etc/httpd/conf, httpd.conf and /var/www/html
#           /home/vbird will be tared !)
#        3. smb (in /etc/samba/*, all files will be copied!)
#        4. The safe and driver files
#           /etc files
#           /var/lib/mysql
#
# ===========================================================================
# History
# Make by VBird 2000/12/16 first time!
# ============================================================================

# 建立备份的路径!这样的好处是可以清楚的知道每个档案放置的地点!
usernamep=/disk2/backup/username
httpdp=/disk2/backup/httpd
smbp=/disk2/backup/smb
otherp=/disk2/backup/others
netpara=/disk2/backup/network
ftpp=/disk2/backup/ftp

#1. username, sendmail and their home directories and local setting
cp /etc/passwd $usernamep
cp /etc/shadow $usernamep
cp /etc/group $usernamep
cp /etc/gshadow $usernamep
cp /etc/aliases $usernamep
tar -zcf $usernamep/mail.tar.gz /var/spool/mail
tar -zcf $usernamep/home.tar.gz /home
tar -zcf $usernamep/usr.local.tar.gz /usr/local

#2. httpd and mysql and counter data
cp /etc/httpd/conf/httpd.conf $httpdp
tar -zcf $httpdp/webcgi.tar.gz /var/www
tar -zcf $httpdp/counter.tar.gz /usr/local/Counter/data
/etc/rc.d/init.d/mysqld stop
tar -zcf $httpdp/mysql.lib.tar.gz /var/lib/mysql
/etc/rc.d/init.d/myslqd start

#3. smb
cp /etc/samba/lmhosts $smbp
cp /etc/samba/MACHINE.SID $smbp
cp /etc/samba/smb.conf $smbp
cp /etc/samba/smbpasswd $smbp
cp /etc/samba/smbusers $smbp

#4. safe and drivers
tar -zcf $otherp/etc.tar.gz /etc
tar -zcf $otherp/mrtg.cfg.tar.gz /usr/local/mrtg-2/bin
tar -zcf $otherp/virus.tar.gz /usr/local/virus
cp /etc/cron.weekly/backup /$otherp

#5. network parameters
cp /etc/hosts            $netpara
cp /etc/hosts.allow      $netpara
cp /etc/hosts.deny       $netpara
cp /etc/sysconfig/network                $netpara
cp /etc/modules.conf                     $netpara
cp /etc/sysconfig/network-scripts/ifcfg-eth0     $netpara

#6. ftp services
cp /etc/ftp*     $ftpp


当然啰,上面的 script 是适合我的状态,所以,你要使用的话,还得要修修改改呦!不要照着使用,会有问题的!

  • 每日备份资料 scripts:

  • 再来,继续提供一下每日备份的数据:
     
    [root @test /root]# vi backupdaily.sh
    #!/bin/bash
    #
    # This program is created by VBird 2002/06/13
    #
    # What is this program?
    #        This program will backup the following messages:
    #        1. MySQL data files ( /var/lib/mysql );
    #        2. HTTP's CGI-directory ( /var/www/cgi-bin )
    #
    # HOW TO RUN THIS PROGRAM?
    #        Just put the file into /etc/crontab job,
    #        or put this file's link file to /etc/cron.daily!
    #
    ###############################################################
    # History
      Date          What                                     Who
    #==============================================================
    # 2002/06/13     First time to run this program
    #                The only backup files are MySQL and CGI VBird
    #==============================================================
    # 0. Get the date messages and backup directory
    day=`date +%Y-%m-%d`
    basedir="/disk2/backup/daily"

    # 1. MySQL   ( PATH = /var/lib/mysql )
    /etc/rc.d/init.d/mysqld stop
    cd /var/lib
    tar -zcf "$basedir"/mysql."$day".tar.gz mysql 2> /dev/null
    /etc/rc.d/init.d/mysqld start

    # 2. CGI ( PATH = /var/www/cgi-bin )
    cd /var/www
    tar -zcf "$basedir"/cgi-bin."$day".tar.gz cgi-bin 2> /dev/null


    好啦!这样一来每天的 MySQL 数据库就可以自动的被记录在 /disk2/backup/daily 里头啦!而且还是文件名称会自动的改变的呦!呵呵!我很喜欢!OK!再来就是开始让系统自己跑啦!怎么跑?!就是 /etc/crontab 呀!提供一下我的相关设定呦!感谢网友 duncanlo 兄提供的好主意!在备份之前将 MySQL 数据库的服务先 stop ,这样一来在在线备份的时候会比较 OK !!
     
    [root @test /root]# vi /etc/crontab
    # 加入这两行即可 ( 请注意您的档案目录!不要照抄呦! )
    # backup scripts
    30 3 * * 0 root /etc/root/backupweekly.sh
    30 2 * * * root /etc/root/backupdaily.sh

    这样系统就会自动的在每天的 2:30 进行 MySQL 的备份,而在每个星期日的 3:30 进行重要档案的备份!呵呵!您说,是不是很容易呢!?但是请千万记得呦!还要将 /disk2 当中的资料 copy 出来才行耶!否则整部系统死掉的时候....那可不是闹着玩的!

     


  • 远程备援系统:

  • 除此之外,那么还有没有保险的方式呢?呵呵!刚刚前面不是提过远程备援吗?最简单的说法就是『使用因特网的方法,将你的数据送到远程主机去备份!』那样就 OK 啦!那么我们怎么使用远程备份的方法呢?那就用最简易的 FTP 吧!不过,在此之前,你必需要了解的是:
     
    1. 备份的资料最好『越精简越好』;
    2. 远程主机必需提供 FTP 服务(当然,其它的服务例如 sftp 也是可以的,只要能够将数据传上去就好了!)
    3. 远程主机必需要可以符合你上传的属性设定,例如 quota 容量、储存目录的属性等等!
     
    好了,那么我们就用最简单的自动FTP scripts来帮我们达成自动档案上传的功能吧!
     
    #!/bin/bash
    #
    # WHAT IS THIS:
    # This program will automatically put the backup file
    # from this host to another hosts
    #
    # HISTORY
    # When           Who      What
    # 2002/10/14     VBird    First time to release
    #
    ###################################################################
    # 1. input your FTP's ID and PassWord
       host="192.168.1.100"
       id="testing"
       pw='your.passwd'

    # 2. what is the correct and remote working directory
       basedir="/disk2/backup"             # 本机上面的欲上传档案路径
       remodir="/disk2/backup_testinghost"       # 远程主机欲备份的目录

    # 3. the tar file
       filename="backup.testinghost.tar"

    # 4. starting tar work
    # 因为我将所有预备被传送的数据都分门别类的放置在 /disk2/backup 这个目录中,
    # 但是FTP传送档案的时候,毕竟一个档案送完会比较容易与快速,
    # 所以我先将好几个档案 tar 成一个!
       cd $basedir
       tar -cvf $filename *

    ###################################################################
    # 5. 底下就是 ftp 自动联机并操作上传手续的 script !
       ftp -n "$host" > "$basedir"/"$filename".log 2> "$basedir"/"$filename".log <<EOC
       user     $id          $pw
       binary
       cd       $remodir
       put      $filename
       bye
       EOC

    # 6. End of this file
    #     Please remenber, change permission of this file!
    #     Because there are many personal secure in this file!


    好了!大家赶紧写一个适合自己的备份 script 来进行备份的行为吧!重要重要喔!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值