Linux虚拟机
需要先安装下:apt install expect
#!/usr/bin/expect -f
spawn docker system prune -a
expect "Are you sure you want to continue?"
send "Y
"
expect eof
#!/bin/bash
#Define Variables 参数定义
SOURCE_DIR=($*) # 以数组的形式存放需要备份的目录,执行脚本时需要手动指定
TARGET_DIR=/test # 指定备份路径
YEAR=`date +%Y` # 年
MONTH=`date +%m` # 月
DAY=`date +%d` # 日
WEEK=`date +%u` # 礼拜
NAME=`date +%H%M` # 时分
FILES=${NAME}_system_backup.tgz #定义压缩文件格式
CODE=$? #判断是否有错
if [ -z $SOURCE_DIR ]; then #如果没有指定参数则提示
echo -e "Please Enter a File or Directory You Need to Backup:\n---------------------------------------------------------------\nExample $0 /boot /etc ..."
exit
fi
# Determine Whether the Target Directory Exists 判断当天的备份目录是否存在
if [ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ]; then
mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY # 不存在则创建目录
echo "This $TARGET_DIR Created Successfully!" # 提示信息
fi
# EXEC Full_Backup Function Command 以下是全备份
Full_Backup(){
if [ "$WEEK" -eq "7" ];then # 指定周日进行完整备份
rm -rf $TARGET_DIR/snapshot # 先删除原本目录下的快照
cd $TARGET_DIR/$YEAR/$MONTH/$DAY ; tar -g $TARGET_DIR/snapshot -cvzf $FILES `echo ${SOURCE_DIR[@]}` #完整备份的命令,echo前面不是单引号,${SOURCE_DIR[@]}是指定的一个或多个需要备份的目录
[ "$CODE" == "0" ]&&echo -e "---------------------------------------------------------------\nFull_Backup System Files Backup Successfully!" #如果以上命令没有问题则提示成功
fi
}
# Perform incremental BACKUP Function Command 以下是增量备份
Add_Backup(){
cd $TARGET_DIR/$YEAR/$MONTH/$DAY;
if [ -f $TARGET_DIR/$TEAR/$MONTH/$DAY/$FILES ]; then # 判断增量备份文件是否存在
read -p "$FILES Already Exists, overwrite confirmation yes or no? : " SURE # 存在则询问是否重新创建
if [ $SURE == "no" -o $SURE == "n"]; then # 不需要则退出
sleep 1; exit 0
fi
# Add_Backup Files System
if [ "$WEEK" -ne "7" ];then #重新创建,除周日进行增量备份
cd $TARGET_DIR/$YEAR/$MONTH/$DAY; tar -g $TARGET_DIR/snapshot -czvf $$_$FILES `echo ${SOURCE_DIR[@]}` #与上条命令的区别是文件名不同,$$是程序的进程号
[ "$CODE" == "0" ]&&echo -e "---------------------------------------------------------------\nAdd_Backup System Files Backup Successfully!" # 如果以上命令没问题则提示成功
fi
else #文件不存在,增量备份
if [ "$WEEK" -ne "7" ];then
cd $TARGET_DIR/$YEAR/$MONTH/$DAY; tar -g $TARGET_DIR/snapshot -cvzf $FILES `echo ${SOURCE_DIR[@]}` #与上条命令的区别是文件名不同
[ "$CODE" == "0" ]&&echo -e "---------------------------------------------------------------\nAdd_Backup System Files Bakcup Successfully!" #如果以上命令没问题则提示成功
fi
fi
}
Full_Backup;Add_Backup
mkdir -p /root/backup/`date "+%Y-%m-%d"`
tar -zcvPf /root/backup/`date "+%Y-%m-%d"`/test2023.tar.gz /root/test
#!/bin/bash
source_folder=/root/test2023
backup_folder=/root/backup/`date "+%Y-%m-%d"`
source_folder_name=`echo $source_folder |awk -F "/" '{print $3}'`.tar.gz
echo "source_folder:$source_folder"
echo "backup_folder: $backup_folder"
echo "source_folder_name: $source_folder_name"
#新建立文件夹
mkdir -p /root/backup/`date "+%Y-%m-%d"`
#压缩命令
tar -zcvPf $backup_folder/$source_folder_name $source_folder
echo "$source_folder_name文件备份成功"
添加到定时列表
0 0 * * * sh /root/backup/compress_backup.sh 2>&1 > /root/backup/log_$(date +\%Y-\%m-\%d)
#!/usr/bin/expect -f
# 获取当前日期 例如:2023-10-12
set date [ clock format [ clock seconds ] -format "%Y-%m-%d" ]
# 传输用户名
set username test
# 对端hostIP地址
set host 192.168.3.66
# 密码
set password test2099
# 设置本地传输路径-前缀
set source_folder /root/backup
# 设置兑换传输路径
set backup_folder /root/back_up_offsite
# expect默认timeout为30S 超过30s未传输完会停止,设置为 -1 则不限制超时时间
set timeout -1
# 压缩 递归 本地绝对路径 用户名 目标IP 目标路径
spawn /usr/bin/scp -C -r $source_folder/$date/ $username@$host:$backup_folder
expect {
"*yes/no" {send "yes\r";exp_continue}
"*password:" {send "$password\r"}
}
expect eof
0 1 * * * expect /root/backup/offsite_backup.expect 2>&1 > /root/backup/offsite_log_$(date +\%Y-\%m-\%d)
find /root/backup/ -mtime +30 | xargs rm -rf {} #删除30天前的文件与文件夹的信息
#!/bin/bash
#待清除目录
dir=/root/backup/
#文件的过期周期
day_expireDay=10
#删除最终为day_expireDay前的备份文件与文件夹信息
find $dir -mtime +$day_expireDay | xargs rm -rf {}
echo "$dir下的$day_expireDay天前的文件清理成功"
0 0 * * * sh /root/backup/delete_expire_backupfile.sh 2>&1 > /root/backup/delete_log__$(date +\%Y-\%m-\%d)
date -s "2022-10-12 23:59:50"
service crond restart
tail -5 /var/log/cron
#!/bin/bash
# 当前已使用的内存大小
used=`free -m | awk 'NR==2' | awk '{print $3}'`
# 当前剩余的内存大小
free=`free -m | awk 'NR==2' | awk '{print $4}'`
echo "===========================" >> /var/log/mem.log
date >> /var/log/mem.log
echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >> /var/log/mem.log
# if [ $free -le 100 ] ; then
sync && echo 1 > /proc/sys/vm/drop_caches
sync && echo 2 > /proc/sys/vm/drop_caches
sync && echo 3 > /proc/sys/vm/drop_caches
echo "OK" >> /var/log/mem.log
# else
# echo "Not required" >> /var/log/mem.log
# fi
#!/bin/bash
log_path=/test/apache-tomcat-9.0..M22/logs
d=`date +%Y-%m-%d`
d90=`date -d'30 day ago' +%Y-%m-%d`
#cd ${log_path} && cp catalina.out $log_path/cron/catalina.out.$d.log
#echo > catalina.out
rm -rf $log_path/catalina.out.${d90}*.log
rm -rf $log_path/localhost.${d90}*.log
rm -rf $log_path/student/talkilla-student*.${d90}*.log
rm -rf $log_path/wechat/talkilla-wechat*..${d90}*.log
rm -rf $log_path/teacher/talkilla-teacher*.${d90}*.log
rm -rf $log_path/consultant/talkilla-consultant*..${d90}*.log
rm -rf $log_path/admin/talkilla-admin*.${d90}*.log
删除脚本
crontab -e
* * * root sh /mnt/software/shell/clear-tomcat-log.sh
def device_online_state():
"""
查询pro中数据
:return: dict
"""
base_url = 'http://ip/api/v1/query'
resp = {
"current_device_online": "未获取到数据",
"max_device_online_1d": "未获取到数据"
}
t = yesterday_unix()
req_url_list = {
"max_device_online": '{}?query=max_over_time(device_status{{state_type="formal_online"}}[1d])'.format(base_url),
"current_device_online": '{}?query=device_status{{state_type="formal_online"}}'.format(base_url)
}
for req_url_type in req_url_list:
r = requests.get(
url=req_url_list[req_url_type],
auth=HTTPBasicAuth('343', ')+U(fdfsfQWSQsd'),
timeout=10,
).json()
if r["status"] == "success":
resp[req_url_type] = r["data"]["result"][0]["value"][1]
return resp