2016-04-19 22:34:25 +02:00
|
|
|
/*
|
2016-04-26 23:02:26 +02:00
|
|
|
* Copyright (C) 2016 Didier Clermonté <dclermonte@april.org>
|
2016-04-19 22:34:25 +02:00
|
|
|
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
|
|
|
|
*
|
|
|
|
* This file is part of Siba.
|
|
|
|
*
|
|
|
|
* Siba is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-03-29 23:25:18 +02:00
|
|
|
package org.dclermonte.siba.model;
|
2016-03-26 23:26:19 +01:00
|
|
|
|
2016-04-26 23:02:26 +02:00
|
|
|
import java.io.BufferedOutputStream;
|
2016-03-27 22:32:35 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2016-04-01 00:08:32 +02:00
|
|
|
import java.time.LocalDateTime;
|
2016-04-07 00:54:03 +02:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2016-05-19 18:40:00 +02:00
|
|
|
import java.util.ResourceBundle;
|
2016-03-27 22:32:35 +02:00
|
|
|
|
2016-04-26 23:02:26 +02:00
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
2016-04-12 00:41:43 +02:00
|
|
|
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
|
2016-03-27 22:32:35 +02:00
|
|
|
import org.apache.commons.compress.utils.IOUtils;
|
2016-05-18 23:20:29 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2016-05-19 18:40:00 +02:00
|
|
|
import org.dclermonte.siba.SibaException;
|
2016-03-27 22:32:35 +02:00
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
|
|
|
*
|
2016-05-31 16:25:35 +02:00
|
|
|
* @author Didier Clermonté (dclermonte@april.org)
|
2016-05-18 23:20:29 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-29 23:25:18 +02:00
|
|
|
public class SibaManager
|
2016-03-26 23:26:19 +01:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.model.messages"); //$NON-NLS-1$
|
2016-05-19 18:40:00 +02:00
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method perform the backup.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param fileToSave
|
|
|
|
* @return
|
2016-05-22 21:47:41 +02:00
|
|
|
* @throws SibaException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-05-31 16:25:35 +02:00
|
|
|
public static File backup(final File fileToSave) throws SibaException
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-05-25 18:10:34 +02:00
|
|
|
File result;
|
2016-05-31 16:25:35 +02:00
|
|
|
File target = new File(System.getProperty("user.dir"));
|
|
|
|
result = backup(fileToSave, target);
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
2016-05-26 22:37:55 +02:00
|
|
|
|
2016-05-31 16:25:35 +02:00
|
|
|
/**
|
|
|
|
* This method perform the backup.
|
|
|
|
*
|
|
|
|
* @param fileToSave
|
|
|
|
* @param target
|
|
|
|
* @return
|
|
|
|
* @throws SibaException
|
|
|
|
*/
|
|
|
|
public static File backup(final File fileToSave, final File target) throws SibaException
|
|
|
|
{
|
|
|
|
File result;
|
|
|
|
TarArchiveOutputStream out = null;
|
2016-06-02 23:18:20 +02:00
|
|
|
OutputStream gzipOutputStream = null;
|
2016-05-22 21:47:41 +02:00
|
|
|
try
|
2016-05-16 23:40:07 +02:00
|
|
|
{
|
2016-05-31 16:25:35 +02:00
|
|
|
|
2016-05-25 18:10:34 +02:00
|
|
|
if ((fileToSave == null) || !(fileToSave.exists()))
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("sourceNotExist.text"));
|
|
|
|
}
|
2016-05-31 16:25:35 +02:00
|
|
|
else if (!(fileToSave.isDirectory()))
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("NotDirectorySource.text"));
|
|
|
|
}
|
|
|
|
else if (!(target.isDirectory()))
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("NotDirectoryTarget.text"));
|
|
|
|
}
|
2016-05-25 18:10:34 +02:00
|
|
|
else
|
2016-05-19 18:40:00 +02:00
|
|
|
{
|
2016-05-31 16:25:35 +02:00
|
|
|
int pathLength = fileToSave.getParentFile().getAbsolutePath().length();
|
|
|
|
LocalDateTime date = LocalDateTime.now();
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH'h'mm'mn'ss's'");
|
|
|
|
String textDate = date.format(formatter);
|
|
|
|
String outputFileNameWithoutExtension = fileToSave.getName() + "-" + textDate;
|
|
|
|
result = new File(target + "/" + outputFileNameWithoutExtension + ".tgz");
|
2016-06-02 23:18:20 +02:00
|
|
|
gzipOutputStream = new GzipCompressorOutputStream(
|
2016-05-31 16:25:35 +02:00
|
|
|
new BufferedOutputStream(new FileOutputStream(result)));
|
|
|
|
out = new TarArchiveOutputStream(gzipOutputStream);
|
2016-06-14 16:21:19 +02:00
|
|
|
SibaUtils.tarDirectoryTree(fileToSave, out, pathLength);
|
2016-05-31 16:25:35 +02:00
|
|
|
out.close();
|
2016-06-14 16:21:19 +02:00
|
|
|
createMD5File(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5");
|
2016-05-19 18:40:00 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
catch (IOException ioExceptionBackup)
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("ioExceptionBackup.text"), ioExceptionBackup); //$NON-NLS-1$
|
|
|
|
}
|
2016-05-31 16:25:35 +02:00
|
|
|
catch (NoSuchAlgorithmException noSuchAlgorithmException)
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("noSuchAlgorithm.text"), noSuchAlgorithmException); //$NON-NLS-1$
|
|
|
|
}
|
2016-05-25 18:10:34 +02:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
IOUtils.closeQuietly(out);
|
|
|
|
}
|
2016-05-26 22:37:55 +02:00
|
|
|
|
2016-05-25 18:10:34 +02:00
|
|
|
//
|
2016-05-22 21:47:41 +02:00
|
|
|
return result;
|
2016-05-02 22:28:35 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method is used to check the file.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param choosenFile
|
|
|
|
* @return
|
2016-05-22 21:47:41 +02:00
|
|
|
* @throws SibaException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-05-22 21:47:41 +02:00
|
|
|
public static boolean check(final File choosenFile) throws SibaException
|
2016-05-02 22:28:35 +02:00
|
|
|
{
|
2016-05-25 18:10:34 +02:00
|
|
|
boolean result;
|
2016-05-31 16:25:35 +02:00
|
|
|
|
2016-05-22 21:47:41 +02:00
|
|
|
try
|
2016-05-02 22:28:35 +02:00
|
|
|
{
|
2016-06-02 23:18:20 +02:00
|
|
|
String md5String = SibaUtils.loadMD5Sum(choosenFile);
|
2016-06-14 16:21:19 +02:00
|
|
|
File fileToCheck1 = SibaUtils.loadFileToCheck(choosenFile);
|
2016-06-02 23:18:20 +02:00
|
|
|
if (StringUtils.equals(md5String, SibaUtils.md5(fileToCheck1)))
|
2016-05-22 21:47:41 +02:00
|
|
|
{
|
2016-05-31 16:25:35 +02:00
|
|
|
result = true;
|
2016-05-22 21:47:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-31 16:25:35 +02:00
|
|
|
result = false;
|
2016-05-22 21:47:41 +02:00
|
|
|
}
|
2016-05-02 22:28:35 +02:00
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
catch (IOException ioExceptionCheck)
|
2016-05-02 22:28:35 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
throw new SibaException(BUNDLE.getString("ioExceptionCheck.text"), ioExceptionCheck); //$NON-NLS-1$
|
2016-05-02 22:28:35 +02:00
|
|
|
}
|
2016-05-31 16:25:35 +02:00
|
|
|
catch (NoSuchAlgorithmException noSuchAlgorithmException)
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("noSuchAlgorithm.text"), noSuchAlgorithmException); //$NON-NLS-1$
|
|
|
|
}
|
2016-05-25 18:10:34 +02:00
|
|
|
//
|
2016-04-19 22:34:25 +02:00
|
|
|
return result;
|
2016-04-01 00:08:32 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
*
|
|
|
|
* This method Generate the file with MD5.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param directoryToSave
|
|
|
|
* @param destination
|
|
|
|
* @return
|
2016-05-31 16:25:35 +02:00
|
|
|
* @throws IOException
|
|
|
|
* @throws NoSuchAlgorithmException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-06-14 16:21:19 +02:00
|
|
|
public static File createMD5File(final File directoryToSave, final String destination)
|
2016-05-31 16:25:35 +02:00
|
|
|
throws IOException, NoSuchAlgorithmException
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-04-23 17:50:13 +02:00
|
|
|
File result;
|
2016-05-31 16:25:35 +02:00
|
|
|
|
|
|
|
result = new File(destination);
|
|
|
|
FileWriter fileWriter = new FileWriter(result);
|
2016-06-02 23:18:20 +02:00
|
|
|
fileWriter.write(SibaUtils.md5(directoryToSave));
|
2016-05-31 16:25:35 +02:00
|
|
|
String newLine = System.getProperty("line.separator");
|
2016-06-14 16:21:19 +02:00
|
|
|
fileWriter.append(" ");
|
|
|
|
fileWriter.append(directoryToSave.getName());
|
|
|
|
fileWriter.append(newLine);
|
2016-05-31 16:25:35 +02:00
|
|
|
fileWriter.close();
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|