You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
1.8 KiB
102 lines
1.8 KiB
#!/bin/sh |
|
|
|
#version 2 |
|
#iansun2 2022/7/14 |
|
|
|
|
|
#config (without "/" eg. /DISKI/Backup) |
|
source_dir= |
|
output_dir= |
|
backup_count_max= |
|
|
|
|
|
#init |
|
year=$(date +"%Y") #get year |
|
month=$(date +"%m") #get month |
|
date=$(date +"%d") #get date |
|
|
|
file_name=${year}"_"${month}"_"${date}".7z" #new backup file name eg. 2022_2_2.7z |
|
backup_count=$(find ${output_dir} -type f -iname "*.7z" -print | wc -l) #count file in $output_dir with name *.7z as $backup_count |
|
|
|
|
|
#check src/out dir |
|
if [ -d $source_dir ] |
|
then |
|
echo "source dir found, ok" |
|
else |
|
echo "source dir not found, exit" |
|
exit 0 |
|
fi |
|
|
|
|
|
if [ -d $output_dir ] |
|
then |
|
echo "output dir found, enter" |
|
cd $output_dir |
|
else |
|
echo "output dir not found, exit" |
|
exit 0 |
|
fi |
|
|
|
|
|
#log dir |
|
if [ -d "log" ] #check log dir exist? |
|
then |
|
echo "log dir exist" |
|
else |
|
mkdir "log" |
|
echo "log dir not exist, create" |
|
fi |
|
|
|
echo ${year}"_"${month}"_"${date} >> ${output_dir}"/log/backup_record" #save backup time in ${output_dir}/log/backup_record |
|
|
|
|
|
#clear old backup |
|
echo "check old backup" |
|
echo "old backup count: "${backup_count} |
|
while [ ${backup_count} -gt ${backup_count_max} ] #while $backup_count > $backup_count_max |
|
do |
|
oldest_file=$(find ${output_dir} -type f -iname "*.7z" -print | sort -n | head -n 1) #find oldest *.7z file by name |
|
rm $oldest_file |
|
echo "remove "${oldest_file} |
|
backup_count=$((backup_count-1)) |
|
done |
|
|
|
find ${output_dir} -empty -type d -delete #delete empty dir |
|
|
|
|
|
#year dir |
|
if [ -d $year ] |
|
then |
|
cd $year |
|
echo "year dir exist, enter!" |
|
else |
|
mkdir $year |
|
cd $year |
|
echo "year dir not exist, create!" |
|
fi |
|
|
|
|
|
#month dir |
|
if [ -d $month ] |
|
then |
|
cd $month |
|
echo "month dir exist, enter!" |
|
else |
|
mkdir $month |
|
cd $month |
|
echo "month dir not exist, create!" |
|
fi |
|
|
|
|
|
|
|
echo "create backup" |
|
7z a ${file_name} ${source_dir}"/" -mx9 |
|
|
|
|
|
#echo "[debug]" |
|
#find ${output_dir} -type f -print | sort -n |
|
|
|
#echo $backup_count |
|
#echo $source_dir |
|
#echo $file_name
|
|
|