目的:
1、将/data01/log/10.175.253.245/ 以原文件夹名+当天日期命名压缩至打包至/data02/log-bakcup/目录下
2、查询上一个压缩复制动作是否执行完成,如果执行完成,删除原文件夹
3、将以上操作追加至日志文件/data02/10.175.253.245-autobak.log
4、每周五下午15:30执行该操作,并将执行情况进行记录
实现:
python
1、代码:
javascript
#!/usr/bin/python3
#-------songp--------#
#-----2024-03-09-----#
#----autobackup.py---#
#---------V1---------#
import os
import tarfile
import time
from datetime import datetime
import shutil
# 定义源文件夹、目标文件夹和日志文件路径
SOURCE_DIR = "/data01/log/10.175.253.245/"
BACKUP_DIR = "/data02/log-backup/"
LOG_FILE = "/data02/10.175.253.245-autobak.log"
# 确保日志文件存在
if not os.path.exists(LOG_FILE):
with open(LOG_FILE, 'w'):
pass
# 获取当前日期并格式化为YYYYMMDD
current_date = datetime.now().strftime("%Y%m%d")
# 定义压缩文件的名称
backup_file = f"10.175.253.245-{current_date}.tar.gz"
backup_path = os.path.join(BACKUP_DIR, backup_file)
# 记录压缩过程开始到日志文件
with open(LOG_FILE, 'a') as log:
log.write(f"{datetime.now()}: The compression process is being initiated\n")
# 压缩源文件夹至目标文件夹
try:
with tarfile.open(backup_path, 'w:gz') as tar:
tar.add(SOURCE_DIR, arcname=os.path.basename(SOURCE_DIR))
# 检查压缩文件是否确实存在
if os.path.exists(backup_path):
# 遍历原始文件夹并删除其中的文件
for filename in os.listdir(SOURCE_DIR):
file_path = os.path.join(SOURCE_DIR, filename)
try:
# 如果是文件,则删除
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Removed file: {file_path}")
# 如果是目录,则跳过(在这个例子中,我们仅处理文件)
except OSError as e:
print(f"Error: {e.strerror} : {file_path}")
with open(LOG_FILE, 'a') as log:
log.write(f"{datetime.now()}: Compressed {SOURCE_DIR} to {backup_path}\n")
log.write(f"{datetime.now()}: Deleted files from original directory {SOURCE_DIR}.\n")
else:
with open(LOG_FILE, 'a') as log:
log.write(f"{datetime.now()}: The backup file {backup_path} was not created, skipping deletion of files from {SOURCE_DIR}.\n")
except Exception as e:
with open(LOG_FILE, 'a') as log:
log.write(f"{datetime.now()}: Compression failed with error: {e}\n")
log.write(f"{datetime.now()}: Did not delete original directory due to previous failure.\n")
# 记录文件删除完成(如果删除操作执行了)
if not os.path.exists(SOURCE_DIR):
with open(LOG_FILE, 'a') as log:
log.write(f"{datetime.now()}: File deletion completed.\n")
定时执行
javascript
chmod +x autobackup.py
crontab -e
0 15 * * 5 /path/to/autobackup.py
shell
代码:
javascript
#!/bin/bash
#-------songp--------#
#-----2024-03-09-----#
#----autobackup.sh---#
#---------V1---------#
# 定义源文件夹和目标文件夹
SOURCE_DIR="/data01/log/10.175.253.245/"
BACKUP_DIR="/data02/log-backup/"
LOG_FILE="/data02/10.175.253.245-autobak.log"
if [ -f "$LOG_FILE" ]; then
echo "$(date): $LOG_FILE Already exist" >> "$LOG_FILE"
else
touch "$LOG_FILE"
fi
# 获取当前日期,并格式化为YYYYMMDD
CURRENT_DATE=$(date +"%Y%m%d")
# 定义压缩文件的名称
BACKUP_FILE="10.175.253.245-$CURRENT_DATE.tar.gz"
echo "$(date): The compression process is being initiated" >> "$LOG_FILE"
# 压缩源文件夹至目标文件夹
tar -czvf "$BACKUP_DIR$BACKUP_FILE" "$SOURCE_DIR"
# 记录压缩操作到日志文件
echo "$(date): Compressed $SOURCE_DIR to $BACKUP_DIR$BACKUP_FILE" >> "$LOG_FILE"
# 检查压缩操作是否成功
if [ $? -eq 0 ]; then
echo "$(date): Compression successful." >> "$LOG_FILE"
else
echo "$(date): Compression failed." >> "$LOG_FILE"
exit 1
fi
# 检查压缩操作是否成功
if [ $? -eq 0 ]; then
# 检查目标目录下是否确实生成了新的压缩文件
if [ -f "$BACKUP_FILE" ]; then
rm -rf "$SOURCE_DIR"/
echo "$(date): Deleted original directory $SOURCE_DIR." >> "$LOG_FILE"
else
echo "$(date): The backup file $BACKUP_FILE was not created, skipping deletion of $SOURCE_DIR." >> "$LOG_FILE"
fi
else
echo "$(date): Did not delete original directory due to previous failure." >> "$LOG_FILE"
fi
echo "$(date): File deletion completed " >> "$LOG_FILE"
定时执行
javascript
chmod +x autobackup.sh
crontab -e
0 15 * * 5 /path/to/autobackup.sh