⬆️ ⬇️

Data backup with btrfs and LVM bash scripts

image



There have already been a lot of posts about backups, especially a lot for Linux. I also took care of setting up a backup.

It was necessary to create system backups, data from the mounted partition and LVM volumes (virtual machine disks). There were thoughts to use Bacula, because familiar with it, but since at home only 1 computer client-server architecture would only create additional difficulties when restoring in case of damage to the system. It means that we simply copy the system and data, we create the image of the LVM partition using dd. I wanted to make a backup every day (at least data) and store at least 14 days. But the search for ready-made and simple solutions that satisfy all needs were not crowned with success. So take the bash and write your bike. In this article, I share what happened.



Disclaimer: when writing scripts there was no goal to write monsters that do everything. I needed a simple and reliable way to backup. I would be grateful for pointing out inaccuracies and bottlenecks of scripts (those where errors may occur). The article is intended more for newbies in Linux, who are looking for a ready-made solution and for lazy people, :) who are too lazy to write themselves.



Initial conditions:




It was decided once a week to make a full copy of everything and create snapshots of btrfs sections every day. You can also create snapshots of LVM volumes, but for me the loss of data for the week is not critical, so weekly copies will suffice.

')

So, script number 1, creates a copy of the root partition files in / mnt / backup / root / "day number" /.



Script number 1
#!/bin/bash set -e echo "Date_start: `date +%Y-%m-%d-%H-%M-%S`" #     ### Vars ### #     (1970 ) day=$((`date +%s` / (60*60*24))) #    dayexp=21 #  path="/" #  (  ) spath="/snapshots/backup_script/" #  dpath="/mnt/backup/root/" ### Delete Old Backups ### #   find $dpath -maxdepth 1 -type d -mtime +$dayexp -exec rm -rf {} \; ### Check exist snapshot ### #  , ,   if (( "`btrfs subvolume list $path | grep backup_script |wc -l`" > 0 )) then echo "Warning: Snapshot exist, deleting" btrfs subvolume delete $spath fi ### Create snapshot ### #  btrfs subvolume snapshot $path $spath ### Rsync ### #    rsync -aAXv $spath $dpath$day ### Delete snapshot ### #  btrfs subvolume delete $spath echo "Backup succesful complete" echo "Date_end: `date +%Y-%m-%d-%H-%M-%S`" exit 0 






Script No. 2, creates root files snapshots (the script is very similar in logic to the 1st, so I will only comment on the differences). Snapshot is named auto_ "number of the day."

Script number 2
 #!/bin/bash set -e echo "Date_start: `date +%Y-%m-%d-%H-%M-%S`" ### Vars ### day=$((`date +%s` / (60*60*24))) dayexp=14 path="/" spath="/snapshots/" ### Delete Old Backups / Check existing snapshot ### #  ,      btrfs subvolume list $path |grep auto |sed -e '1,$ s/.*_//g'| while read ONE_OF_LIST do if [[ "$ONE_OF_LIST" -lt "$day - $dayexp" ]] then echo "remove: $spath"auto_"$ONE_OF_LIST" btrfs subvolume delete $spath"auto_"$ONE_OF_LIST fi if [[ "$ONE_OF_LIST" -eq "$day" ]] then echo "Eroor: snapshot auto_$ONE_OF_LIST exist. Stop script execution." exit 1 fi done ### Create snapshot ### btrfs subvolume snapshot $path $spath"auto_"$day ### End ### echo "Snapshot succesful created" echo "Date_end: `date +%Y-%m-%d-%H-%M-%S`" exit 0 






Script No. 3, creates a copy of the LVM volume:



Script number 3
 #!/bin/bash set -e echo "Date_start: `date +%Y-%m-%d-%H-%M-%S`" ### Vars ### day=$((`date +%s` / (60*60*24))) dayexp=21 #  LVM  path="/dev/virt_image_array/win_home_system" spath="/dev/virt_image_array/backup_lvm1" dpath="/mnt/backup/lvm1/" ### Delete Old Backups ### find $dpath -maxdepth 1 -type f -mtime +$dayexp -exec rm -rf {} \; ### Check exist snapshot ### #  , ,   if (( "`ls /dev/virt_image_array |grep backup_lvm1|wc -l`" > 0 )) then echo "Warning: Snapshot exist, deleting" lvremove --autobackup y -f $spath fi ### Create snapshot ### # ,      ,      .   10 Gb. lvcreate --size 10G --snapshot --name backup_lvm1 $path #              echo #echo "y"| lvcreate --size 10G -A y --snapshot --name backup_lvm1 $path ### DD ### #   bs,       16M dd if=$spath of=$dpath$day bs=16M ### Delete snapshot ### lvremove --autobackup y -f $spath echo "Backup succesful complete" echo "Date_end: `date +%Y-%m-%d-%H-%M-%S`" exit 0 






Since the scripts backing up data from / mnt / data are similar to scripts 1 and 2 I think there is no need to write them.



Add to crontab and determine how often to create backups (in my example, backup is created once a week, snapshots once a day).

 20 01 * * 1 /usr/bin/backup_root.sh >> /var/log/backup_root.log 2>&1 50 01 * * 1 /usr/bin/backup_lvm1.sh >> /var/log/backup_lvm.log 2>&1 20 01 * * * /usr/bin/snapshot_root.sh >> /var/log/snapshot_root.log 2>&1 




What else can be screwed:






UPD. According to the advice of onix74, the scripts were corrected.

Source: https://habr.com/ru/post/211917/



All Articles