shell自动备份文件或目录到本地和远程主机
来源:互联网 发布:新浪短链接数据分析 编辑:程序博客网 时间:2024/05/15 09:09
#!/bin/bash#Function:Automatically backup a given list of directories #or files, and can retain the N day backup files.#Can be back to the directory specified by the remote host,# but the need for free password login to the remote host#This script contains two files:Executable file "backup.sh" and# backup project file "bakfile.lst" ,two files must be in the same directory#Author:JianJie#Date:2016/8/26#Usage: sh backup.sh &#Version:v2###############################################################LOG_FILE=/backup/local/log/`date +"%Y-%m"`.log #Log fileLOG_DIR=/backup/local/logDATE_FORMAT=`date +"%Y-%m-%d"` BAK_FILE_LST=bakfile.lst #Need to backup the file or directory to this fileBAK_DIR=/backup/local #Backup directoryKEEP_DAYS=5 #File retention daysREMOTE_HOST="" #Split between multiple hosts with ':'REMOTE_DIR=/backup/$HOSTNAME #Remote host backup directoryCURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #The directory where the current script is runningFORMAT="tar.gz" #The compression format can be:tar.xz,tar.gz,tar.bz2###############################################################if [ "$REMOTE_HOST" != "" ];then #Determine whether there is a backup directory on the remote host ssh -t -q -n "$REMOTE_HOST" "[ ! -d "$REMOTE_DIR" ] && mkdir -p "$REMOTE_DIR""fi#Determine whether log directory and log file existsif [ ! -d $LOG_DIR ];then mkdir -p $LOG_DIR touch $LOG_FILEelse if [ ! -e $LOG_FILE ];then touch $LOG_FILE fifi#LogStartecho "Backup start at $(date +"%Y-%m-%d %H:%M:%S")" >> $LOG_FILEecho "--------------------------------------------------------" >> $LOG_FILEecho " " >> $LOG_FILE#Determine whether the file list 'bakfile.lst' existsif [ ! -e $CURRENT_DIR/$BAK_FILE_LST ];then echo "Error:Important file [$CURRENT_DIR/$BAK_FILE_LST] has been lost,now creating..." >> $LOG_FILE touch $CURRENT_DIR/$BAK_FILE_LST echo "The file [$CURRENT_DIR/$BAK_FILE_LST] has been created,but you still need to write data in it!" >> $LOG_FILE echo " " >> $LOG_FILE echo "--------------------------------------------------------" >> $LOG_FILE echo "Backup end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LOG_FILE echo " " >> $LOG_FILE exit 3fifor BACK_FILE in $(cat $CURRENT_DIR/$BAK_FILE_LST) do BASE_NAME=`echo "$BACK_FILE" | xargs basename` OLD_FILE=$BAK_DIR/$BASE_NAME.$(date --date="$KEEP_DAYS days ago" +"%Y-%m-%d").$FORMAT if [ -e "$BACK_FILE" ];then cd $(dirname $BACK_FILE) #If a backup file already exists, it will no longer be backed up if [ -f $BAK_DIR/$BASE_NAME.$DATE_FORMAT.$FORMAT ];then echo "Backup file [$BAK_DIR/$BASE_NAME.$DATE_FORMAT.$FORMAT] already exists!" >> $LOG_FILE else cp -RHpf $BASE_NAME $BAK_DIR cd $BAK_DIR case $FORMAT in tar.xz) tar -cpJf $BASE_NAME.$DATE_FORMAT.$FORMAT $BASE_NAME ;; tar.gz) tar -cpzf $BASE_NAME.$DATE_FORMAT.$FORMAT $BASE_NAME ;; tar.bz2) tar -cpjf $BASE_NAME.$DATE_FORMAT.$FORMAT $BASE_NAME ;; *) echo "Compress FORMAT error,default compression FORMAT:tar.gz" >> $LOG_FILE tar -cpzf $BASE_NAME.$DATE_FORMAT.tar.gz $BASE_NAME ;; esac #Backup files back to the remote host via scp if [ "$REMOTE_HOST" != "" ];then for host in $(echo "$REMOTE_HOST" | sed 's/:/ /g') do scp -o ConnectTimeout=10 $BASE_NAME.$DATE_FORMAT.$FORMAT $host:$REMOTE_DIR >>$LOG_FILE 2>/dev/null && \ echo "Successful backup [$BACK_FILE] to the remote host:[$host:$REMOTE_DIR]" >>$LOG_FILE || \ echo "Backup [$BACK_FILE] to the remote host:[$host:$REMOTE_DIR] failed,please check it" >>$LOG_FILE done fi #Backup complete, delete the copy of the source file rm -rf $BASE_NAME echo "Backup [$BACK_FILE] done into [$BAK_DIR] successfuly!" >> $LOG_FILE #Existing N days old file, delete it if [ -f $OLD_FILE ];then rm -rf $OLD_FILE echo "[$OLD_FILE],delete old file successfuly!" >> $LOG_FILE fi fi else echo "Wrong directory or file:[$BACK_FILE],please check it" >>$LOG_FILE fi doneecho " " >> $LOG_FILEecho "--------------------------------------------------------" >> $LOG_FILEecho "Backup end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LOG_FILE#LogEndecho " " >> $LOG_FILEexit 0
0 0
- shell自动备份文件或目录到本地和远程主机
- shell自动备份文件或目录到本地和远程主机
- Git学习-->如何通过Shell脚本自动定时将Gitlab备份文件复制到远程服务器?
- 批处理完成FTP下载远程备份文件到本地
- SQL实现远程备份本保存备份文件到本地
- SQL实现远程备份本保存备份文件到本地
- CentOS Linux自动备份文件到远程FTP服务器并删除指定日期前的备份Shell脚本
- sshfs把远程主机的文件系统映射到本地的目录中
- sshfs把远程主机的文件系统映射到本地的目录中
- sshfs把远程主机的文件系统映射到本地的目录
- ubuntu用ssh挂载远程主机上的目录到本地
- ubuntu用ssh挂载远程主机上的目录到本地
- sshfs把远程主机的文件系统映射到本地的目录中
- DOS批量拷贝本地目录到远程主机(定时执行)
- sshfs把远程主机的文件系统映射到本地的目录中
- shell实现ssh自动登录远程主机
- linux通过sshfs把远程主机的文件系统映射到本地的目录中(构建文件共享目录)
- 使用expect自动ssh到远程主机
- poj1236(强连通分量)
- java学习日记_16:面向对象之匿名对象。07.03
- 智能指针(一)
- Python使用paramiko进行SSH和SFTP连接
- 7_11_C题 Zhuge Liang's Password(模拟)
- shell自动备份文件或目录到本地和远程主机
- PHP中header的用法详解
- 7_11_ F题 Infinite Go(并查集)
- Add Two Numbers(medium)
- Hibernate缓存
- 安卓智慧上海学习笔记系列——新闻详情页ViewPager轮播图无法显示
- 浅析智能指针一
- NCPC 2014 H clock pictures
- 7_11_ H题 Rabbit Kingdom(容斥+树状数组)