linux批量备份服务器配置文件和目录的脚本
功能:
1. 只在备份机执行,远程的服务器只需要允许备份机用root通过密钥登录;
2. 在备份机上配置需要备份哪些服务器,在need_backup_servers这个数组增减;
3. 在备份机上配置需要备份服务器上哪些文件,在need_backup_detail这个数组增减,如果需要备份某台服务器上need_backup_detail数组里定义的某个文件就备份,不存在的文件则自动跳过,例如web服务器上没my.cnf , 也没有影响;
4. 如果服务器上产生的新备份和已经存在已往备份内容一样则只保留时间较早的备份。因为服务器的配置文件不是经常改动的;
5. 通过修改定义keep_backup_num为个变量,可以决定远程服务器上保留最近多少份备份,我这里定义的是10份。备份机上保留所有远程服务器的备份。因为配置文件都比较小,不用担心磁盘空间占用问题。
用过下列技术:
1. 获取远程服务器ip,如果某台服务器没有公网ip则使用该服务器的“主机名_ip.备份年月日_备份时间.tar.gz
2. 通过循环、判断,对数组中的元素重新赋值;
下载之后请留意版本号和更新日志。
#!/bin/bash
##################################################################
# backup_config_files_of_remote_servers
# env: centos 5.5 x86_64
# created by hernan on 2011-04-06
# copyright 2010 __chengyongxu.com__. all rights reserved.
#
# version: 1.4.6
# revision history
##################################################################
# local tmp dir of this script
this_script_tmp=/tmp/do_at_remote_server.sh
this_script_tmp_name=`echo $this_script_tmp|awk -f/ '{print $nf}'`
# local backup dir
local_backup_dir=/tmp/idc_config_backup
# need backup servers and files list
remote_server_tmp_dir=/tmp/backup_server_config_files_tmp
need_backup_servers=(\
10.0.0.52
server_a_ip \
server_b_ip \
server_c_ip \
server_d_ip \
chengyongxu.com \
)
need_backup_detail=(\
/etc/hosts \
/etc/httpd/conf/*.conf \
/etc/httpd/conf.d/*.conf \
/etc/keepalived/keepalived.conf
/etc/my.cnf \
/etc/postfix \
/root/bin/*sh \
/usr/local/nagios/etc/nagios.cfg \
/usr/local/nagios/etc/objects/*cfg \
/usr/local/nginx/conf \
/usr/local/php/etc/php.ini \
/usr/local/php/etc/php-fpm.conf \
/usr/local/php-fcgi/etc/php.ini \
/usr/local/php-fcgi/etc/php-fpm.conf \
/usr/local/sphinx/etc/*.conf \
/var/spool/cron \
)
keep_backup_num=10
ssh_port=22
ssh_user=root
##################################################################
#
# global functions
#
##################################################################
#========= !!!! warning ! don't edit next function !!!! =========#
# create script tmp file
create_script (){
cat << eof > $this_script_tmp
#!/bin/bash
need_backup_detail=(${need_backup_detail[*]})
# how many nums the backup needed
baknum=$keep_backup_num
remote_server_tmp_dir=$remote_server_tmp_dir
server_ip=\`/sbin/ifconfig|grep "inet addr"|cut -f 2 -d ":"|cut -f 1 -d " "|grep -ve "^10.|^127|^172.16|^192.168"\`
backup_time=\`date +%y%m%d_%h%m\`
##################################################################
#
# compress need_backup_detail
#
##################################################################
# if ipv4 private address only, use hostname.ip
if [ -z \$server_ip ]
then
server_ip=\`hostname\`_\`/sbin/ifconfig|grep "inet addr"|cut -f 2 -d ":"|cut -f 1 -d " "|grep -ve "^127"|head -n 1\`
fi
# create tpm dir
if [ ! -d \$remote_server_tmp_dir ]
then
mkdir -p \$remote_server_tmp_dir
fi
# compress
for file in \${need_backup_detail[*]}
do
# when you want to backup the file exists,
# assign to an element of the new array
while [ -e \$file ]
do
need_backup_detail_exists[\$num]=\$file
let num++
break
done
done
tar zcfpp \$remote_server_tmp_dir/\$server_ip.\$backup_time.tar.gz \${need_backup_detail_exists[*]}
##################################################################
#
# if files are the same,keep one only
#
##################################################################
new_backup_archive_content=\`tar tvf \$remote_server_tmp_dir/\$server_ip.\$backup_time.tar.gz\`
for backfile in \`ls \$remote_server_tmp_dir | grep tar.gz |grep -v \$server_ip.\$backup_time\`
do
old_backup_archive_content=\`tar tvf \$remote_server_tmp_dir/\$backfile\`
while [ "\$new_backup_archive_content" = "\$old_backup_archive_content" ]
do
rm -rvf \$remote_server_tmp_dir/\$server_ip.\$backup_time.tar.gz 1 >> \$remote_server_tmp_dir/delete_new.log
break
done
done
##################################################################
#
# keep some backup files newest
#
##################################################################
count=0
for name in \`ls \$bakpath/ |grep -e '[0-9]{6,6}' | sort -r\`
do
count=\$((count+1))
if [ \$count -gt \$baknum ] ; then
echo -n "cleaning old backup: \$name... "
if [ "\$bakpath/\$name" = "/" ] ; then
echo "no rm -rf /!"
exit 1
fi
rm -rf \$bakpath/\$name
echo " done."
else
echo "keeping old backup: \$name"
fi
done
exit
eof
}
copy_script_to_remote (){
for server in ${need_backup_servers[*]}
do
ssh -p $ssh_port -tt $ssh_user@$server << eot
mkdir -p $remote_server_tmp_dir
exit
eot
scp -p $ssh_port $this_script_tmp $ssh_user@$server:$remote_server_tmp_dir/
done
}
backup_on_remote_then_copy_back (){
for server in ${need_backup_servers[*]}
do
ssh -p $ssh_port -tt $ssh_user@$server << eop
chmod o+x $remote_server_tmp_dir/$this_script_tmp_name
sh $remote_server_tmp_dir/$this_script_tmp_name
rm -f $remote_server_tmp_dir/$this_script_tmp_name
exit
eop
rsync -e "ssh -p $ssh_port" $ssh_user@$server:$remote_server_tmp_dir/*tar.gz $local_backup_dir/
done
}
##################################################################
#
# main
#
##################################################################
#
create_script
copy_script_to_remote
backup_on_remote_then_copy_back
rm -rf $this_script_tmp
下一篇: bash脚本输入密码不回显问题的解决方法
推荐阅读
-
linux批量备份服务器配置文件和目录的脚本
-
linux下Vps自动备份web和mysql数据库的脚本
-
linux 服务器自动备份脚本的方法(mysql、附件备份)
-
linux批量备份服务器配置文件和目录的脚本
-
Linux下使用RMAN和控制文件备份删除归档日志的SHELL脚本
-
linux下Vps自动备份web和mysql数据库的脚本
-
linux 服务器自动备份脚本的方法(mysql、附件备份)
-
阿里云ACA课程试验—ECS之初体验(Linux)与云服务器ECS的数据恢复和备份
-
Linux下使用RMAN和控制文件备份删除归档日志的SHELL脚本
-
Linux服务器下MySQL自动备份脚本的使用方法