shell - jenkins备份

2022-08-02
#!/bin/bash
set -x

cat > /tmp/exclude-jenkins.file <<EOF
workspace/*
caches/*
builds/*
backup/*
config-history/*
logs/*
fingerprints/*
build-docker-image-*
plugins/*bak
maven/*
modules/*
builds/*
EOF

delete_day=3
jenkins_backup_exclude_file=/tmp/exclude-jenkins.file
jenkins_backup_path=/opt/backup/jenkins/

if [[ ! -d ${jenkins_backup_path} ]];then
    mkdir -p ${jenkins_backup_path}
fi
date=`date +%Y%m%d-%H`
tar_gz_name="jenkins.tar.gz-"${date}
tar -zcf ${tar_gz_name} ${JENKINS_HOME}/* --exclude-from=${jenkins_backup_exclude_file}
echo "backup jenkins to "${tar_gz_name}

if [[ $? -eq 0 ]];then
#    chattr -i ${jenkins_backup_path}
    mv ${tar_gz_name} ${jenkins_backup_path}
fi
if [[ $? -eq 0 ]];then
    find ${jenkins_backup_path} -type f -mtime +${delete_day} | xargs rm
    cd ${jenkins_backup_path}
    du -sh *
else
    exit 1
fi