|
|
|
@ -3,7 +3,7 @@ import { exportDatabaseValidation } from './validation.js';
|
|
|
|
|
import fs from 'fs'; |
|
|
|
|
import path from 'path'; |
|
|
|
|
|
|
|
|
|
const getBackups = async () => { |
|
|
|
|
const getBackupsRequest = async () => { |
|
|
|
|
try { |
|
|
|
|
const { data } = await axiosWrapper({ |
|
|
|
|
method: 'get', |
|
|
|
@ -16,12 +16,18 @@ const getBackups = async () => {
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const exportDatabase = async (tag) => { |
|
|
|
|
const exportDatabaseRequest = async (tag) => { |
|
|
|
|
try { |
|
|
|
|
const { value: validValue, error } = exportDatabaseValidation({ tag }); |
|
|
|
|
const timestampedTag = `${tag}_${Date.now()}`; |
|
|
|
|
console.log(timestampedTag); |
|
|
|
|
const { value: validValue, error } = exportDatabaseValidation({ |
|
|
|
|
tag: timestampedTag, |
|
|
|
|
}); |
|
|
|
|
if (error) throw error?.details[0]?.message; |
|
|
|
|
|
|
|
|
|
const { data } = await axiosWrapper({ |
|
|
|
|
const { |
|
|
|
|
data: { export_path }, |
|
|
|
|
} = await axiosWrapper({ |
|
|
|
|
method: 'post', |
|
|
|
|
url: '/backups/export/database', |
|
|
|
|
payload: { |
|
|
|
@ -38,14 +44,15 @@ const exportDatabase = async (tag) => {
|
|
|
|
|
templates: ['recipes.md'], |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
return data; |
|
|
|
|
console.log(`Database backed up to ${export_path}`); |
|
|
|
|
return true; |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error(error); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const getFileToken = async (filename) => { |
|
|
|
|
const getFileTokenRequest = async (filename) => { |
|
|
|
|
try { |
|
|
|
|
const { data } = await axiosWrapper({ |
|
|
|
|
method: 'get', |
|
|
|
@ -58,9 +65,19 @@ const getFileToken = async (filename) => {
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const downloadBackup = async (fileToken, fileName) => { |
|
|
|
|
const downloadBackupRequest = async (fileToken, fileName) => { |
|
|
|
|
try { |
|
|
|
|
const target_path = path.resolve(`backups/${fileName}`); |
|
|
|
|
const subFolder = new Date().toLocaleDateString('en-CA', { |
|
|
|
|
year: 'numeric', |
|
|
|
|
month: 'numeric', |
|
|
|
|
}); |
|
|
|
|
const base_path = path.resolve('backups/'); |
|
|
|
|
const subfolderExists = fs.existsSync(`${base_path}/${subFolder}`); |
|
|
|
|
if (!subfolderExists) |
|
|
|
|
fs.mkdirSync(`${base_path}/${subFolder}`, { recursive: false }, (err) => { |
|
|
|
|
if (err) throw err; |
|
|
|
|
}); |
|
|
|
|
const target_path = `${base_path}/${subFolder}/${fileName}`; |
|
|
|
|
const writer = fs.createWriteStream(target_path, 'binary'); |
|
|
|
|
const streamResponse = await axiosWrapper({ |
|
|
|
|
method: 'get', |
|
|
|
@ -68,7 +85,7 @@ const downloadBackup = async (fileToken, fileName) => {
|
|
|
|
|
responseType: 'stream', |
|
|
|
|
}); |
|
|
|
|
streamResponse.data.pipe(writer); |
|
|
|
|
writer.on('finish', () => console.log(`Downloaded: ${fileName}`)); |
|
|
|
|
writer.on('finish', () => console.log(`Downloaded to ${target_path}`)); |
|
|
|
|
writer.on('error', () => |
|
|
|
|
console.error(`[ERROR] while dowloading ${fileName}`) |
|
|
|
|
); |
|
|
|
@ -79,4 +96,24 @@ const downloadBackup = async (fileToken, fileName) => {
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export { getBackups, exportDatabase, getFileToken, downloadBackup }; |
|
|
|
|
const backupDeleteRequest = async (filename) => { |
|
|
|
|
try { |
|
|
|
|
await axiosWrapper({ |
|
|
|
|
method: 'delete', |
|
|
|
|
url: `/backups/${filename}/delete`, |
|
|
|
|
}); |
|
|
|
|
console.log(`Deleted ${filename} from mealie`); |
|
|
|
|
return true; |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error(error); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export { |
|
|
|
|
getBackupsRequest, |
|
|
|
|
exportDatabaseRequest, |
|
|
|
|
getFileTokenRequest, |
|
|
|
|
downloadBackupRequest, |
|
|
|
|
backupDeleteRequest, |
|
|
|
|
}; |
|
|
|
|