0

How I can compress files in this scenario:

I have folder structure like this:

User1:

/home/user1/websites/website1
/home/user1/websites/website2

User2:

/home/user2/websites/website1
/home/user2/websites/website2
/home/user2/websites/website3

And I try now (need) to do backup like this:

Folders for backups per user:

/backup/date/websites/user1/
/backup/date/websites/user1/

And I need backup tar in user directory separately per website like this:

/backup/date/websites/user1/website1.tar.gz
/backup/date/websites/user1/website2.tar.gz
/backup/date/websites/user2/website1.tar.gz
/backup/date/websites/user2/website2.tar.gz
/backup/date/websites/user2/website3.tar.gz

I have script like this one to do half of this work:

#VARIABLES
BKP_DATE=`date +"%F"`
BKP_DATETIME=`date +"%H-%M"`

#BACKUPS FOLDERS
BKP_DEST=/backup/users
BKP_DEST_DATE=$BKP_DEST/$BKP_DATE
BKP_DEST_TIME=$BKP_DEST_DATE/$BKP_DATETIME
BACKUP_DIR=$BKP_DEST_TIME

#NUMBER OF DAYS TO KEEP ARCHIVES IN BACKUP DIRECTORY
KEEP_DAYS=7

#Create folders
mkdir -p $BKP_DEST_DATE
mkdir -p $BKP_DEST_TIME
mkdir -p $BACKUP_DIR

#DELETE FILES OLDER THAN {*} DAYS IN BACKUP SERVER DIRECTORY
#echo 'Deleting backup folder older than '${KEEP_DAYS}' days'
find $BKP_DEST/* -type d -ctime +${KEEP_DAYS} -exec rm -rf {} \;

#Do backups
#List only names available users data directories
usersdirectories=`cd /home && find * -maxdepth 0 -type d | grep -Ev "(tmp|root)"`

#Creating directories per user name
for i in $usersdirectories; do
  mkdir -p $BACKUP_DIR/$i/websites
done

But if u see, i haven't how to do tar this for separately archives. In my half script I have done:

  1. Create folder structure for backup by datetime (/backup/users/day/hour-minutes)
  2. Create folder structure for backup by users names (/backup/users/day/hour-minutes/user1)

Thanks for all users who try to help me!

hock
  • 159
  • 1
  • 8
  • You can have a look here: [https://stackoverflow.com/questions/40990371/add-a-directory-when-creating-tar-archive](https://stackoverflow.com/questions/40990371/add-a-directory-when-creating-tar-archive) – Snorik Apr 10 '20 at 10:25

3 Answers3

0

I wish I could comment to ask for more clarification but I will attempt to help you.

#!/bin/bash

# example for user1
archive_path=/backup/data/websites/user1
file_path=/home/user1/websites/*

for i in $file_path
do
  tar czf $archive_path/$(basename $i).tar.gz $file_path
done
fxdeaway
  • 165
  • 8
  • Thanks, but your's proposition not working yet. Script try to package all directories from /home/user1/websites/ into one package multiple times as many directories as there are in /home/user1/websites/ folder. – hock Apr 14 '20 at 12:12
0

I will try to complete your script, but I can't debug it because your environment is hard to reproduce. It is better in the future that you provide a minimal reproducible example.

#VARIABLES
BKP_DATE=$(date +"%F")
BKP_DATETIME=$(date +"%H-%M")

#BACKUPS FOLDERS
BKP_DEST=/backup/users
BKP_DEST_DATE=$BKP_DEST/$BKP_DATE
BKP_DEST_TIME=$BKP_DEST_DATE/$BKP_DATETIME
BACKUP_DIR=$BKP_DEST_TIME

#NUMBER OF DAYS TO KEEP ARCHIVES IN BACKUP DIRECTORY
KEEP_DAYS=7

#Create folders
mkdir -p $BKP_DEST_DATE
mkdir -p $BKP_DEST_TIME
mkdir -p $BACKUP_DIR

#DELETE FILES OLDER THAN {*} DAYS IN BACKUP SERVER DIRECTORY
#echo 'Deleting backup folder older than '${KEEP_DAYS}' days'
find $BKP_DEST/* -type d -ctime +${KEEP_DAYS} -exec rm -rf {} \;

#Do backups
#List only names available users data directories
usersdirectories=$(cd /home && find * -maxdepth 0 -type d | grep -Ev "(tmp|root)")

#Creating directories per user name
for i in $usersdirectories; do
  for w in $(/home/$i/websites/*); do
    ws=$(basename $w)
    mkdir -p $BACKUP_DIR/$i/websites/$ws
    tar -czvf $BACKUP_DIR/$i/websites/$ws.tar.gz /home/$i/websites/$ws
  done
done

I suppose there are no blanks inside the directory names website1, etc...

I also replaced the deprecated backticks operators of your code by $(...).

Pierre François
  • 5,850
  • 1
  • 17
  • 38
  • Thanks, but when I try execute this script I have error like this, and this error repeat on all folders in /home directory: ./users-data-backup.sh: line 30: /home/admin/domains/default: Is a directory I do not know, but probably does not detect all directories used in the user directory /home/ user/websites/*. On this error (up) not all pages that are available in user directories. – hock Apr 10 '20 at 20:50
  • What is the command to be executed at line 30 of your `users-data-backup.sh` script? – Pierre François Apr 11 '20 at 08:36
  • This is: `for i in $usersdirectories; do` – hock Apr 11 '20 at 16:17
  • I think is a problem with usersdirectories variable, beacuse I get comment off all in for, and problem is the same. Any ideas? :) – hock Apr 14 '20 at 12:42
0

Okay, after small fix. Version from Pierre working now. If u want, u can adjust it for yours need.

#VARIABLES
BKP_DATE=$(date +"%F")
BKP_DATETIME=$(date +"%H-%M")

#BACKUPS FOLDERS
BKP_DEST=/backup
BKP_DEST_DATE=$BKP_DEST/$BKP_DATE
BKP_DEST_TIME=$BKP_DEST_DATE/$BKP_DATETIME
BACKUP_DIR=$BKP_DEST_TIME

#NUMBER OF DAYS TO KEEP ARCHIVES IN BACKUP DIRECTORY
KEEP_DAYS=7

#Create folders
mkdir -p $BKP_DEST_DATE
mkdir -p $BKP_DEST_TIME
mkdir -p $BACKUP_DIR

#DELETE FILES OLDER THAN {*} DAYS IN BACKUP SERVER DIRECTORY
#echo 'Deleting backup folder older than '${KEEP_DAYS}' days'
find $BKP_DEST/* -type d -ctime +${KEEP_DAYS} -exec rm -rf {} \;

#Do backups
#List only names available users data directories
usersdirectories=$(cd /home/user && find * -maxdepth 0 -type d | grep -Ev "(tmp|root)")

#Creating directories per user name
for i in $usersdirectories; do
  for w in /home/user/$i/*; do
    #echo ok
    ws=$(basename $w)
    echo mkdir -p $BACKUP_DIR/$i
    echo tar -czvf $BACKUP_DIR/$i/$ws.tar.gz /home/user/$i/$ws
  done
done
hock
  • 159
  • 1
  • 8