Hello, habrauzery!
I want to share with you the experience of organizing backup using
standard FreeNas tools .
To begin with, I would like to introduce you to the course of the matter so that the reader can more easily navigate the circumstances of my case, so.
')
We have in our AD network, with all the associated services, many users and a file server running on FreeNas (aka freeBSD), which is friends with the domain.
FreeNas itself has the option of moving deleted files to a hidden folder.
Recycle There are deleted files that are grouped into folders, where the folder name is
% USERNAME% deleted . This solution is enough, but what if the user does not delete the information, but changes it?
In this case, the rollback of the file itself is necessary, which our standard file server cannot do.
Therefore, we write scripts that we run through cron.
I decided to split the script for myself into 2 parts. The first part makes a backup, the second deletes =) In the second I will explain a little lower.
The pepper part of the script, the usual backup, nothing complicated.
We backup, add to where we want and name the archive by creation date to avoid duplication
pax -wvzf /mnt/data/backup/Hartmann_backup/Hartmann-`date "+%Y%m%d"`.tar.gz /mnt/data/Users_data
Thus, you will have to create archives until the end of free space. This is bad, you need to delete the oldest copies of the archives. This is the second part of the script.
But here begins a non-trivial solution.
How do we figure out which archive is the last? The first thing that comes to mind is to look at the archive property, take a date from there and delete the oldest ones. But he will compare this date with the FreeNas system date. Ie, if the time on the file server is suddenly lost, we can lose all the archives, so this option is not suitable.
Another option is to take the name of the archive and compare the largest numbers from the names ... and keep, say, the 3 largest numbers, that is, leave the 3 most recent archives. This option suits us. I implemented this solution as follows.
#search_to_bd
filename=/mnt/data/backup/Hartmann_backup/bd.txt
filename2=/mnt/data/backup/Hartmann_backup/sort.txt
echo "" > $filename2
ls /mnt/data/backup/Hartmann_backup/ | grep .gz > $filename
declare -a array1
array1=( `cat "$filename" | tr '\n' ' '`)
#search_to_bd/
kolel=${#array1[*]}
var0=1
#Cut_name_to_bd
while [ "$var0" -lt "$kolel" ]
do
eval var$var0= echo ${array1[$var0]:9:8} >> $filename2
var=array1[$var0]
var0=`expr $var0 + 1`
done
#Cut_name_to_bd/ from_var0_to_kolel
#Take_sort_from_bd
ARRAY=( `cat "$filename2" | tr '\n' ' '`)
#Take_sort_from_bd/
#Comperison
maxi=0
for ((i=0; i < "${#ARRAY[@]}"; i++));do [ "0${ARRAY[i]}" -gt "0${ARRAY[maxi]}" ] && maxi=$i; done
maxi1=${ARRAY[maxi]}
ARRAY[maxi]=
maxi=0
for ((i=0; i < "${#ARRAY[@]}"; i++));do [ "0${ARRAY[i]}" -gt "0${ARRAY[maxi]}" ] && maxi=$i; done
maxi2=${ARRAY[maxi]}
ARRAY[maxi]=
maxi=0
for ((i=0; i < "${#ARRAY[@]}"; i++));do [ "0${ARRAY[i]}" -gt "0${ARRAY[maxi]}" ] && maxi=$i; done
maxi3=${ARRAY[maxi]}
#Comperison/
#shopt -s extglob
#rm !(@("backup/Hartmann_backup/Hartmann-$maxi1.tar.gz"|"backup/Hartmann_backup/Hartmann-#$maxi2.tar.gz"|"backup/Hartmann_backup/Hartmann-$maxi3.tar.gz"))
cd /mnt/data/backup/Hartmann_backup
ls -1 | egrep -v "(Hartmann-$maxi1.tar.gz|Hartmann-$maxi2.tar.gz|Hartmann-$maxi3.tar.gz)" | xargs rm
In 2 words, I create 2 files, something DB, where I bring the entire contents of the folder. Further I throw everything in an array. Further, in order to compare the numbers, I need to remove the letter from the name, well, and there I already sranivaet the numbers and delete the old ones. Waiting for objective criticism!