Skip to content

Commit

Permalink
Merge pull request #116 from Muhammad-Abdullah012/bugfix/delete-older…
Browse files Browse the repository at this point in the history
…-backups

fix: only keep last uploaded backup and delete others
  • Loading branch information
kashikhan1 authored Oct 3, 2024
2 parents 664a195 + 10cdf26 commit 465eb9f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions scripts/backup_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ async function uploadToDrive(auth, dumpStream) {
console.log('File uploaded successfully.');
} catch (error) {
console.error('Error uploading file:', error);
throw error;
}
}

async function deleteOldBackups(auth) {
const drive = google.drive({ version: 'v3', auth });
const threeDaysAgo = new Date();
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);
const today = new Date();
today.setHours(0, 0, 0, 0);

const listFiles = async () => {
const response = await drive.files.list({
Expand All @@ -124,7 +125,8 @@ async function deleteOldBackups(auth) {

for (const { id, name, createdTime } of files) {
const fileCreatedTime = new Date(createdTime);
if (fileCreatedTime < threeDaysAgo) {
fileCreatedTime.setHours(0, 0, 0, 0);
if (fileCreatedTime < today) {
await deleteFile(id);
console.log(`Deleted old backup file: ${name}`);
}
Expand Down

0 comments on commit 465eb9f

Please sign in to comment.