0

I am trying to make a backups of some files on my server using tar but I cant seem to get it to do what i want. I've Googled for a solution but i cant seem to find the right one. I want files in /home/me/mysqlDumpFiles to be included only if they are newer than 1 hour ago but I want all files in /var/www to be include no matter when they were modified.

dateNow=$(date +%m_%d_%y-%H.%M.%S) tar czf /home/me/backups/backup-$dateNow.tar.gz /home/me/mysqlDumpFiles --newer-mtime "1 hour ago" /var/www

I appreciate any help i get.

EDIT: I ended up making my own shell script and running that with two separate tar commands

#!/bin/bash
set -e
currentDate=$(date +%Y-%m-%d_%H.%M.%S)

printf "currentDate is $currentDate\n========================================\n"
mkdir /home/me/backupTemp;
mysqldump -uroot -p[CENSORED] [DATABSE_NAME]> /home/me/backupTemp/site.net.sql;
tar -jcf /home/me/mysqlDumpFiles/site.net-$currentDate.tar.bz2  -C /home/me/backupTemp .;
tar -jcf /home/me/backups/backup-$currentDate.tar.bz2 -C /home/me/backupTemp . -C /var/www .;
rm -rf /home/me/backupTemp;
printf "========================================\n"
Dani
  • 33
  • 5

2 Answers2

0

You will need to perform two separate tar commends to achieve this.

EEAA
  • 109,363
  • 18
  • 175
  • 245
  • 1
    If the goal is to produce a single `.tar.gz` file or stream the output as a single `tar` file to be used for `stdin` on another process, then this is not a particular useful answer. It is also not clear from your answer what those two commands would need to be. – kasperd Apr 30 '17 at 14:32
0

You could have separated archives:

dateNow=$(date +%m_%d_%y-%H.%M.%S)
tar czf ~/backups/mysql-$dateNow.tar.gz ~/mysqlDumpFiles/ --newer-mtime "1 hour ago" 
tar czf ~/backups/www-$dateNow.tar.gz /var/www/

Or you can use -r (--append, append files to the end of an archive) to add folder into same archive:

dateNow=$(date +%m_%d_%y-%H.%M.%S)
tar cf ~/backups/backup-$dateNow.tar ~/mysqlDumpFiles/ --newer-mtime "1 hour ago"
tar rf ~/backups/backup-$dateNow.tar /var/www/
gzip ~/backups/backup-$dateNow.tar
Esa Jokinen
  • 46,944
  • 3
  • 83
  • 129