Jenkins Pipeline自动化定时执行etcd备份

一、安装etcdctl命令(略)

https://www.cnblogs.com/Hannibal-/articles/18308422

二、编写自动化流水线

流水线解释请查看:https://www.cnblogs.com/Hannibal-/articles/18308631

#!/usr/bin/env groovy

def pipeline_name = "kubernetes etcd备份"
def host_auth = "9e002605-87d1-4ac3-9e9e-b89a84689d05"
def remote_host = "192.168.90.90"
def known_hosts = "/root/.ssh/known_hosts"

pipeline {
    agent any

    environment {
        ETCDCTL_API = '3'
    }

    triggers {
        cron '0 2 */1 * *'
    }

    options {
        retry(5)
    }

    stages {
        stage('设置环境变量') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: host_auth, passwordVariable: 'password', usernameVariable: 'username')]) {
                        def REMOTE_CONFIG = [name: 'remote', host: remote_host, user: env.username, knownHosts: known_hosts]
                        sshCommand(
                            remote: REMOTE_CONFIG + [password: env.password],
                            command: """
                                export ETCDCTL_API=3
                                echo \$ETCDCTL_API
                            """
                        )
                    }
                }
            }
        }

        stage('检查备份文件') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: host_auth, passwordVariable: 'password', usernameVariable: 'username')]) {
                        def REMOTE_CONFIG = [name: 'remote', host: remote_host, user: env.username, knownHosts: known_hosts]
                        sshCommand(
                            remote: REMOTE_CONFIG + [password: env.password],
                            command: """
                                if [ ! -d /opt/etcd-backup ]; then mkdir -p /opt/etcd-backup; fi
                            """
                        )
                    }
                }
            }
        }

        stage('执行备份任务') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: host_auth, passwordVariable: 'password', usernameVariable: 'username')]) {
                        def REMOTE_CONFIG = [name: 'remote', host: remote_host, user: env.username, knownHosts: known_hosts]
                        def timestamp = new Date().format("yyyyMMddHHmmss")
                        def backupFile = "/opt/etcd-backup/etcdbackup-${timestamp}.db"
                        sshCommand(
                            remote: REMOTE_CONFIG + [password: env.password],
                            command: """
                                echo "开始执行备份...."
                                ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key snapshot save ${backupFile}
                                ls -lh  
                                ETCDCTL_API=3 etcdctl --write-out=table snapshot status ${backupFile}
                                echo "备份任务执行完成!!!"
                            """
                        )
                    }
                }
            }
        }

        stage('上传制品文件') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: host_auth, passwordVariable: 'password', usernameVariable: 'username')]) {
                        def REMOTE_CONFIG = [name: 'remote', host: remote_host, user: env.username, knownHosts: known_hosts]
                        sshCommand(
                            remote: REMOTE_CONFIG + [password: env.password],
                            command: """
                                echo "开始上传制品文件,请稍后..."
                                cd /opt/etcd-backup
                                if [ ! -d /opt/etcd-backup/products ]; then mkdir -p /opt/etcd-backup/products; fi
                                ls *.db | xargs mv -t products/
                                echo "制品文件更新完成!!!"
                            """
                        )
                    }
                }
            }
        }

        stage('验证备份信息') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: host_auth, passwordVariable: 'password', usernameVariable: 'username')]) {
                        def REMOTE_CONFIG = [name: 'remote', host: remote_host, user: env.username, knownHosts: known_hosts]
                        sshCommand(
                            remote: REMOTE_CONFIG + [password: env.password],
                            command: """
                                latestBackupFile=\$(ls -t /opt/etcd-backup/products/*.db | head -n 1)
                                ETCDCTL_API=${ETCDCTL_API} etcdctl --write-out=table snapshot status \${latestBackupFile}
                            """
                        )
                    }
                }
            }
        }

        stage('清理备份缓存') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: host_auth, passwordVariable: 'password', usernameVariable: 'username')]) {
                        def REMOTE_CONFIG = [name: 'remote', host: remote_host, user: env.username, knownHosts: known_hosts]
                        sshCommand(
                            remote: REMOTE_CONFIG + [password: env.password],
                            command: '''
                                echo "开始清理历史缓存,请稍后..."
                                find /opt/etcd-backup/products -name "*.db" -type f -mtime +7 -exec rm -f {} \\;
                                echo "历史缓存清理完成!!!"
                            '''
                        )
                    }
                }
            }
        }
    }

    post {
        success {
            script {
                echo "备份成功!"
                currentBuild.description += "构建成功!"
            }
        }
        failure {
            script {
                echo "备份失败!"
                currentBuild.description += "构建失败!"
            }
        }
        aborted {
            script {
                echo "备份取消"
                currentBuild.description += "构建取消"
            }
        }
    }
}

 

 

 

posted @ 2024-07-18 06:57  吕钦扬  阅读(12)  评论(0)    收藏  举报